Module: TextActions

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

Instance Method Summary collapse

Instance Method Details

#build_text_from_filtered(input_struct, input_indexes) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/asker/lang/text_actions.rb', line 104

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



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/asker/lang/text_actions.rb', line 137

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



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/asker/lang/text_actions.rb', line 155

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



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/asker/lang/text_actions.rb', line 182

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

#text1Object



26
27
28
# File 'lib/asker/lang/text_actions.rb', line 26

def text1
  @_text1
end

#text2Object



30
31
32
# File 'lib/asker/lang/text_actions.rb', line 30

def text2
  @_text2
end

#text3Object



34
35
36
# File 'lib/asker/lang/text_actions.rb', line 34

def text3
  @_text3
end

#text4Object



38
39
40
# File 'lib/asker/lang/text_actions.rb', line 38

def text4
  @_text4
end

#text5Object



42
43
44
# File 'lib/asker/lang/text_actions.rb', line 42

def text5
  @_text5
end

#text6Object



46
47
48
# File 'lib/asker/lang/text_actions.rb', line 46

def text6
  @_text6
end

#text7Object



50
51
52
# File 'lib/asker/lang/text_actions.rb', line 50

def text7
  @_text7
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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/asker/lang/text_actions.rb', line 59

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

Set of functions used by Lang class Return text indicated by lang code…



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

def text_for(option, *input)
  @_text1 = input[0] # FIXME: done to avoid linter complaints.
  @_text2 = input[1]
  @_text3 = input[2]
  @_text4 = input[3]
  @_text5 = input[4]
  @_text6 = input[5]
  @_text7 = input[6]

  if @templates[option].nil?
    Logger.error "TextActions: Unkown template (#{option})"
    exit 1
  end
  renderer = ERB.new(@templates[option])
  renderer.result(binding)
end

#text_with_connectors(text) ⇒ Object

Return text with connectors



94
95
96
# File 'lib/asker/lang/text_actions.rb', line 94

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

#text_without_connectors(text) ⇒ Object

Return text without connectors



100
101
102
# File 'lib/asker/lang/text_actions.rb', line 100

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