Class: StageI

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

Overview

range i1, i2, i3, i4

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

#runObject

Stage I: process every image from <def> tag



11
12
13
14
15
16
17
18
19
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
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
# File 'lib/asker/ai/stages/stage_i.rb', line 11

def run
  questions = []
  return questions unless concept.type == "text"

  lang = concept.lang
  # for every <image> do this
  concept.images.each do |image|
    url = image[:text]
    s = Set.new [name, lang.text_for(:none)]
    concept.neighbors.each { |n| s.add n[:concept].name }
    a = s.to_a

    # Question type <i1>: choose between 4 options
    if s.count > 3
      q = Question.new(:choice)
      q.name = "#{name}-#{num}-i1choice"
      q.text = lang.text_for(:i1, url)
      q.encode = image[:file]
      q.good = name
      q.bads << lang.text_for(:none)
      q.bads << a[2]
      q.bads << a[3]
      questions << q
    end

    # Question type <i1>: choose between 4 options, good none (Syntax error)
    if s.count > 3
      q = Question.new(:choice)
      q.name = "#{name}-#{num}-i1misspelling"
      q.text = lang.text_for(:i1, url)
      q.encode = image[:file]
      q.good = lang.text_for(:none)
      q.bads << lang.do_mistake_to(name)
      q.bads << a[2]
      q.bads << a[3]
      questions << q
    end

    s.delete(name)
    a = s.to_a

    # Question type <i1>: choose between 4 options, good none
    if s.count > 3
      q = Question.new(:choice)
      q.name = "#{name}-#{num}-i1none"
      q.text = lang.text_for(:i1, url)
      q.encode = image[:file]
      q.good = lang.text_for(:none)
      q.bads << a[1]
      q.bads << a[2]
      q.bads << a[3]
      questions << q
    end

    # Question type <i2>: boolean => TRUE
    q = Question.new(:boolean)
    q.name = "#{name}-#{num}-i2true"
    q.text = lang.text_for(:i2, url, name)
    q.encode = image[:file]
    q.good = "TRUE"
    questions << q

    # Question type <i2>: boolean => FALSE
    if concept.neighbors.count.positive?
      q = Question.new(:boolean)
      q.name = "#{name}-#{num}-i2false"
      q.text = lang.text_for(:i2, url, concept.neighbors[0][:concept].name)
      q.encode = image[:file]
      q.good = "FALSE"
      questions << q
    end

    # Question type <i3>: hidden name questions
    q = Question.new(:short)
    q.name = "#{name}-#{num}-i3short"
    q.text = lang.text_for(:i3, url, lang.hide_text(name))
    q.encode = image[:file]
    q.shorts << name
    q.shorts << name.tr("-", " ").tr("_", " ")
    questions << q

    # Question filtered text questions
    concept.texts.each do |t|
      filtered = lang.text_with_connectors(t)
      next if filtered[:words].size < 4

      indexes = filtered[:indexes]
      groups = indexes.combination(4).to_a.shuffle
      max = (indexes.size / 4).to_i
      groups[0, max].each do |e|
        q = Question.new(:match)
        q.shuffle_off
        q.name = "#{name}-#{num}-i4filtered"
        q.encode = image[:file]
        e.sort!
        s = lang.build_text_from_filtered(filtered, e)
        q.text = lang.text_for(:i4, url, s)
        e.each_with_index do |value, index|
          q.matching << [index.next.to_s, filtered[:words][value][:word].downcase]
        end
      end
      questions << q
    end
  end
  questions
end