Class: AtomicAssessmentsImport::ExamSoft::Extractor::QuestionStemDetector

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

Constant Summary collapse

OPTION_PATTERN =
/\A\s*\*?[a-oA-O]\s*[.)]/

Instance Method Summary collapse

Constructor Details

#initialize(nodes) ⇒ QuestionStemDetector

Returns a new instance of QuestionStemDetector.



9
10
11
# File 'lib/atomic_assessments_import/exam_soft/extractor/question_stem_detector.rb', line 9

def initialize(nodes)
  @nodes = nodes
end

Instance Method Details

#detectObject



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

def detect
  stem_node = @nodes.find do |node|
    text = node.text.strip
    next if text.empty?
    next if text.match?(OPTION_PATTERN)

    true
  end

  return nil unless stem_node

  text = stem_node.text.strip

  # Strip metadata prefixes and numbered prefix together
  # e.g. "Folder: Geo Title: Q1 Category: Test 1) What is the capital?"
  text = if text.match?(/\d+[.)]/m)
           text.sub(/\A.*?(?<!\S)\d+[.)]\s*/m, "")
         else
           # Strip standalone metadata labels if present (Folder:, Title:, Category:, Type:)
           text.sub(/\A\s*(?:(?:Folder|Title|Category|Type):\s*\S+\s*)*/, "")
         end

  # Split on tilde and take the first part (remove explanation)
  text = text.split("~").first

  text = text&.gsub(/\s+/, " ")&.strip
  text.nil? || text.empty? ? nil : text
end