Class: AtomicAssessmentsImport::ExamSoft::Extractor::FeedbackDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/atomic_assessments_import/exam_soft/extractor/feedback_detector.rb

Constant Summary collapse

TILDE_PATTERN =
/~\s*(.+)/m
LABEL_PATTERN =
/\A\s*(?:Explanation|Rationale):\s*(.+)/im
OPTION_PATTERN =
/\A\s*\*?[a-oA-O]\s*[.)]/

Instance Method Summary collapse

Constructor Details

#initialize(nodes) ⇒ FeedbackDetector

Returns a new instance of FeedbackDetector.



11
12
13
# File 'lib/atomic_assessments_import/exam_soft/extractor/feedback_detector.rb', line 11

def initialize(nodes)
  @nodes = nodes
end

Instance Method Details

#detectObject



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
# File 'lib/atomic_assessments_import/exam_soft/extractor/feedback_detector.rb', line 15

def detect
  feedback_parts = []
  collecting = false

  @nodes.each do |node|
    text = node.text.strip

    if collecting
      # Stop collecting if we hit an option line
      break if text.match?(OPTION_PATTERN)
      feedback_parts << text unless text.empty?
      next
    end

    match = text.match(TILDE_PATTERN)
    if match
      first_part = match[1].strip
      feedback_parts << first_part unless first_part.empty?
      collecting = true
    end
  end

  return feedback_parts.join(" ").gsub(/\s+/, " ").strip unless feedback_parts.empty?

  @nodes.each do |node|
    text = node.text.strip
    match = text.match(LABEL_PATTERN)
    return match[1].gsub(/\s+/, " ").strip if match
  end

  nil
end