Module: TextActions

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

Instance Method Summary collapse

Instance Method Details

#build_text_from_filtered(pStruct, pIndexes) ⇒ Object



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
91
# File 'lib/asker/lang/text_actions.rb', line 64

def build_text_from_filtered(pStruct, pIndexes)
  lines    = pStruct[:lines]
  lIndexes = pIndexes.sort
  counter  = 1
  lText    = ''

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

#count_words(pInputText) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/asker/lang/text_actions.rb', line 93

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

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

#do_mistake_to(pText = '') ⇒ Object



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
132
# File 'lib/asker/lang/text_actions.rb', line 107

def do_mistake_to(pText = '')
  lText = pText.dup
  keys = @mistakes.keys

  # Try to do mistake with one item from the key list
  keys.shuffle!
  keys.each do |key|
    if lText.include? key.to_s
       values = @mistakes[key].split(',')
       values.shuffle!
       lText = lText.sub(key.to_s,values[0].to_s)
       return lText
    end
  end

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

  lText + 's'
end

#hide_text(input_text) ⇒ Object



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

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(pText, pFilter) ⇒ Object



20
21
22
23
24
25
26
27
28
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
# File 'lib/asker/lang/text_actions.rb', line 20

def text_filter_connectors(pText, pFilter)
  input_lines = pText.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 pFilter) || (!flag and !pFilter 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

 result={ :lines => output_lines, :words => output_words, :indexes => indexes }
 return result
end

#text_for(pOption, pText1 = "", pText2 = "", pText3 = "", pText4 = "", pText5 = "", pText6 = "", pText7 = "") ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/asker/lang/text_actions.rb', line 5

def text_for(pOption, pText1="",pText2="",pText3="",pText4="",pText5="",pText6="",pText7="")
  text1=pText1
  text2=pText2
  text3=pText3
  text4=pText4
  text5=pText5
  text6=pText6
  text7=pText7

 # TODO: check if exists pOption before use it
  renderer = ERB.new(@templates[pOption])
  output = renderer.result(binding)
  return output
end

#text_with_connectors(text) ⇒ Object



56
57
58
# File 'lib/asker/lang/text_actions.rb', line 56

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

#text_without_connectors(text) ⇒ Object



60
61
62
# File 'lib/asker/lang/text_actions.rb', line 60

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