Module: TextActions

Included in:
Lang
Defined in:
lib/asker/lang/text_actions.rb

Overview

Set of functions used by Lang class

Instance Method Summary collapse

Instance Method Details

#build_text_from_filtered(input_struct, input_indexes) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/asker/lang/text_actions.rb', line 74

def build_text_from_filtered(input_struct, input_indexes)
  lines = input_struct[:lines]
  indexes = input_indexes.sort
  counter = 1
  text = ''

  lines.each do |line|
    line.each do |value|
      if value.class == String
        text += (' ' + value)
      elsif value == value.to_i
        # INFO: ruby 2.4 unifies Fixnum and Bignum into Integer
        #       Avoid using deprecated classes.
        if indexes.include? value
          text += " [#{counter}]"
          counter += 1
        else
          word = input_struct[:words][value][:word]
          text += (' ' + word)
        end
      end
    end
  end
  text.gsub!(' .', '.')
  text.gsub!(' ,', ',')
  text = text[1, text.size] if text[0] == ' '
  text
end

#count_words(input) ⇒ Object

Count words

Parameters:

  • input (String)

Returns:

  • Integer



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/asker/lang/text_actions.rb', line 107

def count_words(input)
  return 0 if input.nil?

  t = input.clone
  t.gsub!("\n", ' ')
  t.gsub!('/', ' ')
  # t.gsub!("-"," ")
  t.gsub!('.', ' ')
  t.gsub!(',', ' ')
  t.gsub!('   ', ' ')
  t.gsub!('  ', ' ')
  t.split(' ').count
end

#do_mistake_to(input = '') ⇒ Object

Do mistake to input

Parameters:

  • input (String) (defaults to: '')

Returns:

  • String



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/asker/lang/text_actions.rb', line 125

def do_mistake_to(input = '')
  text = input.dup
  keys = @mistakes.keys

  # Try to do mistake with one item from the key list
  keys.shuffle!
  keys.each do |key|
    next unless text.include? key.to_s

    values = @mistakes[key].split(',')
    values.shuffle!
    text = text.sub(key.to_s, values[0].to_s)
    return text
  end

  # Force mistake by swapping letters
  if text.size > 1
    i = rand(text.size - 2)
    aux = text[i]
    text[i] = text[i + 1]
    text[i + 1] = aux
  end
  return text if text != input

  text + 's'
end

#hide_text(input_text) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/asker/lang/text_actions.rb', line 152

def hide_text(input_text)
  input = input_text.clone
  if count_words(input) < 2 && input.size < 10
    output = '[*]'
  else
    output = ''
    input.each_char do |char|
      if ' !|"@#$%&/()=?¿¡+*(){}[],.-_<>'.include? char
        output += char
      else
        output += '?'
      end
    end
  end
  output
end

#text_filter_connectors(input, filter) ⇒ Object

Convert input text into output text struct

Parameters:

  • input (String)

    Input text

  • filter (Boolean)

    true => apply filter, false => dont filter

Returns:

  • Array



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/asker/lang/text_actions.rb', line 29

def text_filter_connectors(input, filter)
  input_lines = input.split('.')
  output_lines = []
  output_words = []
  input_lines.each_with_index do |line, rowindex|
   row = []
    line.split(' ').each_with_index do |word, colindex|
      flag = @connectors.include? word.downcase

     # if <word> is a conector and <pFilter>==true Then Choose this <word>
     # if <word> isn't a conector and <pFilter>==true and <word>.length>1 Then Choose this <word>
      if (flag and filter) || (!flag and !filter and word.length > 1)
      output_words << {:word => word, :row => rowindex, :col => colindex }
      row << (output_words.size-1)
     else
      row << word
      end
   end
   row << '.'
   output_lines << row
  end

  indexes = []
  exclude = ['[', ']', '(', ')', '"']
  output_words.each_with_index do |item, index|
    flag = true
    exclude.each { |e| flag = false if item[:word].include? e }
    indexes << index if flag
  end

  { lines: output_lines, words: output_words, indexes: indexes }
end

#text_for(option, *input) ⇒ Object

Return text indicated by lang code…



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/asker/lang/text_actions.rb', line 8

def text_for(option, *input)
  text1 = input[0]
  text2 = input[1]
  text3 = input[2]
  text4 = input[3]
  text5 = input[4]
  text6 = input[5]
  text7 = input[6]

  # Check if exists option before use it
  raise "[ERROR] Unkown template #{option}" if @templates[option].nil?

  renderer = ERB.new(@templates[option])
  renderer.result(binding)
end

#text_with_connectors(text) ⇒ Object

Return text with connectors



64
65
66
# File 'lib/asker/lang/text_actions.rb', line 64

def text_with_connectors(text)
 text_filter_connectors(text, false)
end

#text_without_connectors(text) ⇒ Object

Return text without connectors



70
71
72
# File 'lib/asker/lang/text_actions.rb', line 70

def text_without_connectors(text)
  text_filter_connectors(text, true)
end