Class: StageB

Inherits:
BaseStage show all
Defined in:
lib/asker/ai/stages/stage_b.rb

Instance Method Summary collapse

Methods inherited from BaseStage

#concept, #images, #initialize, #lang, #name, #names, #neighbors, #num, #random_image_for, #texts, #type

Constructor Details

This class inherits a constructor from BaseStage

Instance Method Details

#process_table_match2fields(p_table, list1, list2, index1, index2) ⇒ Object

Process table data to generate match questions

Parameters:

  • p_table (Table)
  • list1 (Array)

    Rows that belong to this table

  • list2 (Array)

    List with similar rows (same table name) from the neighbours tables

  • index1 (Integer)

    Use this field number

  • index2 (Integer)

    Use this field number



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
91
92
93
94
95
96
97
98
99
100
101
102
103
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/asker/ai/stages/stage_b.rb', line 40

def process_table_match2fields(p_table, list1, list2, index1, index2)
  questions = []
  lang = concept.lang

  if list1.count > 3
    list1.each_cons(4) do |e1, e2, e3, e4|
      e = [e1, e2, e3, e4]

      # Question type <b1match>: match 4 items from the same table
      e.shuffle!
      q = Question.new(:match)
      q.name = "#{name}-#{num}-b1match4x4-#{p_table.name}"
      q.tags << "match"
      q.tags << "random"
      q.text = random_image_for(name) \
               + lang.text_for(:b1, name, p_table.fields[index1].capitalize, p_table.fields[index2].capitalize)
      q.matching << [e[0][:data][index1], e[0][:data][index2]]
      q.matching << [e[1][:data][index1], e[1][:data][index2]]
      q.matching << [e[2][:data][index1], e[2][:data][index2]]
      q.matching << [e[3][:data][index1], e[3][:data][index2]]
      questions << q

      q = Question.new(:ddmatch)
      q.name = "#{name}-#{num}-b1ddmatch4x4-#{p_table.name}"
      q.text = random_image_for(name) \
               + lang.text_for(:b1, name, p_table.fields[index1].capitalize, p_table.fields[index2].capitalize)
      q.matching << [e[0][:data][index1], e[0][:data][index2]]
      q.matching << [e[1][:data][index1], e[1][:data][index2]]
      q.matching << [e[2][:data][index1], e[2][:data][index2]]
      q.matching << [e[3][:data][index1], e[3][:data][index2]]
      questions << q

      if list2.count.positive?
        # Add an extra line
        e.shuffle!
        q = Question.new(:match)
        q.name = "#{name}-#{num}-b1match4x5-#{p_table.name}"
        q.tags << "match"
        q.tags << "random"
        q.text = random_image_for(name) \
                 + lang.text_for(:b1, name, p_table.fields[index1].capitalize, p_table.fields[index2].capitalize)
        q.matching << [e[0][:data][index1], e[0][:data][index2]]
        q.matching << [e[1][:data][index1], e[1][:data][index2]]
        q.matching << [e[2][:data][index1], e[2][:data][index2]]
        q.matching << [e[3][:data][index1], e[3][:data][index2]]
        q.matching << ["", list2[0][:data][index2]]
        questions << q
      else
        e.shuffle!
        q = Question.new(:match)
        q.name = "#{name}-#{num}-b1match4x5_1misspelled-#{p_table.name}"
        q.tags << "match"
        q.tags << "random"
        q.text = random_image_for(name) \
                 + lang.text_for(:b1, name, p_table.fields[index1].capitalize, p_table.fields[index2].capitalize)
        q.matching << [e[0][:data][index1], e[0][:data][index2]]
        q.matching << [e[1][:data][index1], e[1][:data][index2]]
        q.matching << [e[2][:data][index1], e[2][:data][index2]]
        q.matching << [e[3][:data][index1], e[3][:data][index2]]
        q.matching << ["", lang.do_mistake_to(e[0][:data][index2])]
        questions << q
      end

      # Question type <b1match>: match 3 items from table-A and 1 item with error
      e.shuffle!
      q = Question.new(:match)
      q.name = "#{name}-#{num}-b1match4x4_1misspelled-#{p_table.name}"
      q.tags << "match"
      q.tags << "random"
      q.text = random_image_for(name) \
               + lang.text_for(:b1, name, p_table.fields[index1].capitalize, p_table.fields[index2].capitalize)
      q.matching << [e[0][:data][index1], e[0][:data][index2]]
      q.matching << [e[1][:data][index1], e[1][:data][index2]]
      q.matching << [e[2][:data][index1], e[2][:data][index2]]
      q.matching << [lang.do_mistake_to(e[3][:data][index1]), lang.text_for(:misspelling)]
      questions << q

      q = Question.new(:ddmatch)
      q.name = "#{name}-#{num}-b1ddmatch4x4_1misspelled-#{p_table.name}"
      q.tags << "match"
      q.tags << "random"
      q.text = random_image_for(name) \
               + lang.text_for(:b1, name, p_table.fields[index1].capitalize, p_table.fields[index2].capitalize)
      q.matching << [e[0][:data][index1], e[0][:data][index2]]
      q.matching << [e[1][:data][index1], e[1][:data][index2]]
      q.matching << [e[2][:data][index1], e[2][:data][index2]]
      q.matching << [lang.do_mistake_to(e[3][:data][index1]), lang.text_for(:misspelling)]
      questions << q

      if list2.count.positive?
        # Add an extra line error
        e.shuffle!
        q = Question.new(:match)
        q.name = "#{name}-#{num}-b1match4x5_1misspelled_1error-#{p_table.name}"
        q.tags << "match"
        q.tags << "random"
        q.text = random_image_for(name) \
                 + lang.text_for(:b1, name, p_table.fields[index1].capitalize, p_table.fields[index2].capitalize)
        q.matching << [e[0][:data][index1], e[0][:data][index2]]
        q.matching << [e[1][:data][index1], e[1][:data][index2]]
        q.matching << [e[2][:data][index1], e[2][:data][index2]]
        q.matching << [lang.do_mistake_to(e[3][:data][index1]), lang.text_for(:misspelling)]
        q.matching << ["", list2[0][:data][index2]]
        questions << q
      else
        # Add an extra line misspelled
        e.shuffle!
        q = Question.new(:match)
        q.name = "#{name}-#{num}-b1match4x5_2misspelled-#{p_table.name}"
        q.tags << "match"
        q.tags << "random"
        q.tags << "misspelled"
        q.text = random_image_for(name) \
                 + lang.text_for(:b1, name, p_table.fields[index1].capitalize, p_table.fields[index2].capitalize)
        q.matching << [e[0][:data][index1], e[0][:data][index2]]
        q.matching << [e[1][:data][index1], e[1][:data][index2]]
        q.matching << [e[2][:data][index1], e[2][:data][index2]]
        q.matching << [lang.do_mistake_to(e[3][:data][index1]), lang.text_for(:misspelling)]
        q.matching << ["", lang.do_mistake_to(e[0][:data][index2])]
        questions << q
      end
    end
  end

  if list1.count > 2 && list2.count.positive?
    s = Set.new
    list1.each do |i|
      s.add(i[:data][index1] + "<=>" + i[:data][index2])
    end
    s.add(list2[0][:data][index1] + "<=>" + list2[0][:data][index2])

    # Question 3 items from table-A, and 1 item from table-B
    if s.count > 3
      q = Question.new(:match)
      q.name = "#{name}-#{num}-b1match4x4_1error-#{p_table.name}"
      q.tags << "match"
      q.tags << "random"
      q.text = random_image_for(name) \
               + lang.text_for(:b1, name, p_table.fields[index1].capitalize, p_table.fields[index2].capitalize)
      q.matching << [list1[0][:data][index1], list1[0][:data][index2]]
      q.matching << [list1[1][:data][index1], list1[1][:data][index2]]
      q.matching << [list1[2][:data][index1], list1[2][:data][index2]]
      q.matching << [list2[0][:data][index1], lang.text_for(:error)]
      questions << q

      q = Question.new(:ddmatch)
      q.name = "#{name}-#{num}-b1ddmatch4x4_1error-#{p_table.name}"
      q.tags << "match"
      q.tags << "random"
      q.text = random_image_for(name) \
               + lang.text_for(:b1, name, p_table.fields[index1].capitalize, p_table.fields[index2].capitalize)
      q.matching << [list1[0][:data][index1], list1[0][:data][index2]]
      q.matching << [list1[1][:data][index1], list1[1][:data][index2]]
      q.matching << [list1[2][:data][index1], list1[2][:data][index2]]
      q.matching << [list2[0][:data][index1], lang.text_for(:error)]
      questions << q

      q = Question.new(:match)
      q.name = "#{name}-#{num}-b1match4x5_1error_1misspelled-#{p_table.name}"
      q.tags << "match"
      q.tags << "random"
      q.text = random_image_for(name) \
               + lang.text_for(:b1, name, p_table.fields[index1].capitalize, p_table.fields[index2].capitalize)
      q.matching << [list1[0][:data][index1], list1[0][:data][index2]]
      q.matching << [list1[1][:data][index1], list1[1][:data][index2]]
      q.matching << [list1[2][:data][index1], list1[2][:data][index2]]
      q.matching << [list2[0][:data][index1], lang.text_for(:error)]
      q.matching << ["", lang.do_mistake_to(list1[0][:data][index2])]
      questions << q
    end
  end

  questions
end

#run(table, list1, list2) ⇒ Object

range b1 Process table data to generate match questions

Parameters:

  • table (Table)
  • list1 (Array)

    Rows that belong to this table

  • list2 (Array)

    List with similar rows (same table name) from the neighbours tables



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/asker/ai/stages/stage_b.rb', line 13

def run(table, list1, list2)
  questions = []
  return questions if table.fields.count < 2

  return questions unless concept.type == "text"

  if table.fields.count == 2
    questions += process_table_match2fields(table, list1, list2, 0, 1)
  elsif table.fields.count == 3
    questions += process_table_match2fields(table, list1, list2, 0, 1)
    questions += process_table_match2fields(table, list1, list2, 0, 2)
  elsif table.fields.count == 4
    questions += process_table_match2fields(table, list1, list2, 0, 1)
    questions += process_table_match2fields(table, list1, list2, 0, 2)
    questions += process_table_match2fields(table, list1, list2, 0, 3)
  end

  questions
end