Class: PythonCodeAI

Inherits:
BaseCodeAI show all
Defined in:
lib/asker/ai/code/python_code_ai.rb

Instance Attribute Summary

Attributes inherited from BaseCodeAI

#questions

Instance Method Summary collapse

Methods inherited from BaseCodeAI

#clone_array, #filename, #lines, #lines_to_html, #lines_to_s, #make_questions, #name, #num, #process?, #type

Constructor Details

#initialize(code) ⇒ PythonCodeAI

Returns a new instance of PythonCodeAI.



6
7
8
9
# File 'lib/asker/ai/code/python_code_ai.rb', line 6

def initialize(code)
  @lang = LangFactory.instance.get("python")
  super code
end

Instance Method Details

#make_comment_errorObject



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
# File 'lib/asker/ai/code/python_code_ai.rb', line 11

def make_comment_error
  questions = []
  # error_lines = []
  @lines.each_with_index do |line, index|
    if line.strip.start_with?("#")
      lines = clone_array @lines
      lines[index].sub!("#", "").strip!

      q = Question.new(:short)
      q.name = "#{name}-#{num}-code1uncomment"
      q.text = @lang.text_for(:code1, lines_to_html(lines))
      q.shorts << (index + 1)
      q.feedback = "Comment symbol removed"
      questions << q
    elsif line.strip.size > 0
      lines = clone_array @lines
      lines[index] = "# " + lines[index]

      q = Question.new(:short)
      q.name = "#{name}-#{num}-code1comment"
      q.text = @lang.text_for(:code1, lines_to_html(lines))
      q.shorts << (index + 1)
      q.feedback = "Comment symbol added"
      questions << q
    end
  end
  questions
end

#make_no_error_changesObject



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
# File 'lib/asker/ai/code/python_code_ai.rb', line 40

def make_no_error_changes
  questions = []
  empty_lines = []
  used_lines = []
  @lines.each_with_index do |line, index|
    if line.strip.size.zero?
      empty_lines << index
    else
      used_lines << index
    end
  end

  used_lines.each do |index|
    lines = clone_array(@lines)
    lines.insert(index, " " * (rand(4).to_i + 1))
    if @lines.size < 4 || rand(2) == 0
      q = Question.new(:short)
      q.name = "#{name}-#{num}-code1ok"
      q.text = @lang.text_for(:code1, lines_to_html(lines))
      q.shorts << "0"
      q.feedback = "Code is OK"
      questions << q
    else
      q = Question.new(:choice)
      q.name = "#{name}-#{num}-code1ok"
      q.text = @lang.text_for(:code2, lines_to_html(lines))
      others = (1..@lines.size).to_a.shuffle!
      q.good = "0"
      q.bads << others[0].to_s
      q.bads << others[1].to_s
      q.bads << others[2].to_s
      q.feedback = "Code is OK"
    end
  end

  questions
end

#make_syntax_errorObject



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
# File 'lib/asker/ai/code/python_code_ai.rb', line 78

def make_syntax_error
  questions = []

  @lang.mistakes.each_pair do |key, values|
    error_lines = []
    @lines.each_with_index do |line, index|
      error_lines << index if line.include?(key.to_s)
    end

    v = values.split(",")
    v.each do |value|
      error_lines.each do |index|
        lines = clone_array(@lines)
        lines[index].sub!(key.to_s, value)
        if @lines.size < 4 || rand(2) == 0
          q = Question.new(:short)
          q.name = "#{name}-#{num}-code1syntaxerror"
          q.text = @lang.text_for(:code1, lines_to_html(lines))
          q.shorts << (index + 1)
          q.feedback = "Syntax error: '#{value}' must be '#{key}'"
        else
          q = Question.new(:choice)
          q.name = "#{name}-#{num}-code1syntaxerror"
          q.text = @lang.text_for(:code2, lines_to_html(lines))
          others = (1..@lines.size).to_a.shuffle!
          others.delete(index + 1)
          q.good = (index + 1).to_s
          q.bads << others[0].to_s
          q.bads << others[1].to_s
          q.bads << others[2].to_s
          q.feedback = "Syntax error: '#{value}' must be '#{key}'"
        end
        questions << q
      end
    end
  end
  questions
end

#make_variable_errorObject



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
# File 'lib/asker/ai/code/python_code_ai.rb', line 117

def make_variable_error
  questions = []
  # error_lines = []
  @lines.each_with_index do |line, index|
    # Search Variable assignment
    m = /\s*(\w*)\s*=\w*/.match(line)
    i = []
    unless m.nil?
      varname = (m.values_at 1)[0]
      # Search used Variable
      @lines.each_with_index do |line2, index2|
        next if index >= index2
        i << index2 if line2.include?(varname)
      end
    end
    next if i.size == 0
    i.shuffle!
    i.each do |k|
      lines = clone_array @lines
      temp = lines[index]
      lines[index] = lines[k]
      lines[k] = temp

      if rand(2) == 0
        q = Question.new(:short)
        q.name = "#{name}-#{num}-code1variable"
        q.text = @lang.text_for(:code1, lines_to_html(lines))
        q.shorts << (index + 1)
        q.feedback = "Variable error! Swapped lines #{index + 1} with #{k + 1}"
      else
        q = Question.new(:choice)
        q.name = "#{name}-#{num}-code1variable"
        q.text = @lang.text_for(:code2, lines_to_html(lines))
        others = (1..@lines.size).to_a.shuffle!
        others.delete(index + 1)
        q.good = (index + 1).to_s
        q.bads << others[0].to_s
        q.bads << others[1].to_s
        q.bads << others[2].to_s
        q.feedback = "Variable error! Swapped lines #{index + 1} with #{k + 1}"
      end
      questions << q
    end
  end
  questions
end