Module: QuickExam::Analyst::Common

Included in:
BaseDocx, BaseHTML, BaseText
Defined in:
lib/quick_exam/analyst/common.rb

Instance Method Summary collapse

Instance Method Details

#answer(str) ⇒ Object

TODO: Regex get clean answer i: case insensitive x: ignore whitespace in regex ?= : positive lookahead



67
68
69
70
71
72
# File 'lib/quick_exam/analyst/common.rb', line 67

def answer(str)
  corr_mark = correct_mark(@f_corr, safe: true)
  ans_with_mark_correct = /(#{regex_answer_sentence}(?=#{corr_mark}))/
  ans_without_mark_correct = regex_answer_sentence
  str[(/#{ans_with_mark_correct}|#{regex_answer_sentence}/ix)].__presence || str
end

#answer?(str) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
# File 'lib/quick_exam/analyst/common.rb', line 57

def answer?(str)
  str = rid_non_ascii!(str)
  str = Sanitize.fragment(str).__squish
  str[(regex_answer_mark)].__present?
end

#clean_objectObject



37
38
39
40
41
# File 'lib/quick_exam/analyst/common.rb', line 37

def clean_object
  @object.question.strip!
  @object.answers.map(&:strip!)
  @object
end

#collect_object_ticketObject



32
33
34
35
# File 'lib/quick_exam/analyst/common.rb', line 32

def collect_object_ticket
  @records << clean_object
  reset_object_ticket
end

#correct_answer?(str) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/quick_exam/analyst/common.rb', line 47

def correct_answer?(str)
  str.downcase.include?(correct_mark(@f_corr).downcase)
end

#end_of_line?(num_row) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/quick_exam/analyst/common.rb', line 28

def end_of_line?(num_row)
  num_row == @total_line
end

#end_of_one_ticket_for_next_question?(str) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/quick_exam/analyst/common.rb', line 23

def end_of_one_ticket_for_next_question?(str)
  str = Sanitize.fragment(str)
  @object.answers.__present? && @object.question.__present? && question?(str)
end

#get_answer(str) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/quick_exam/analyst/common.rb', line 9

def get_answer(str)
  return if @object.question.__blank?
  return unless answer?(str)

  # Get answer
  @object.answers << answer(str)
  get_correct_indexes_answer(str)
end

#get_correct_indexes_answer(str) ⇒ Object



18
19
20
21
# File 'lib/quick_exam/analyst/common.rb', line 18

def get_correct_indexes_answer(str)
  return unless correct_answer?(str)
  @object.correct_indexes << @object.answers.size - 1
end

#get_question(str) ⇒ Object



4
5
6
7
# File 'lib/quick_exam/analyst/common.rb', line 4

def get_question(str)
  return if @object.answers.__present?
  @object.question += question(str)
end

#question(str) ⇒ Object

TODO: Regex get clean question i: case insensitive m: make dot match newlines ?<= : positive lookbehind



78
79
80
81
# File 'lib/quick_exam/analyst/common.rb', line 78

def question(str)
  letter_question = Regexp.quote(str.match(regex_question_mark).to_a.last.to_s)
  str[(/(?<=#{letter_question}).+/im)].__presence || str
end

#question?(str) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/quick_exam/analyst/common.rb', line 51

def question?(str)
  str = rid_non_ascii!(str)
  str = Sanitize.fragment(str).__squish
  str[(regex_question_mark)].__present?
end

#regex_answer_markObject

TODO: Regex match answer mark Format question: A) , a. , 1/ @return: Answer mark



107
108
109
# File 'lib/quick_exam/analyst/common.rb', line 107

def regex_answer_mark
  /(^\w[\.|\)|\/])/
end

#regex_answer_sentenceObject

TODO: Regex match answer sentence Format question: A) , a. , 1/ @return: Answer sentence without answer mark

?<= : positive lookbehind



100
101
102
# File 'lib/quick_exam/analyst/common.rb', line 100

def regex_answer_sentence
  /(?<=#{regex_answer_mark}).*/
end

#regex_question_markObject

TODO: Regex match question mark Format question: Q1: , q1. , q1) , Q1/ @return: Question mark

i: case insensitive m: make dot match newlines x: ignore whitespace in regex



90
91
92
93
# File 'lib/quick_exam/analyst/common.rb', line 90

def regex_question_mark
  ques_mark = question_mark(@f_ques, safe: true)
  /(^#{ques_mark}[\s]*\d+[:|\)|\.|\/])/ixm
end

#reset_object_ticketObject



43
44
45
# File 'lib/quick_exam/analyst/common.rb', line 43

def reset_object_ticket
  @object = QuickExam::Record.new()
end

#rid_non_ascii!(str) ⇒ Object

TODO: Remove non-unicode character Solutions:

Ref: https://stackoverflow.com/a/26411802/14126700
Ref: https://www.regular-expressions.info/posixbrackets.html
[:print:] : Visible characters and spaces (anything except control characters)


116
117
118
119
120
121
122
# File 'lib/quick_exam/analyst/common.rb', line 116

def rid_non_ascii!(str)
  # Solution 1: str.chars.reject { |char| char.ascii_only? and (char.ord < 32 or char.ord == 127) }.join
  non_utf8 = str.slice(str[/[^[:print:]]/].to_s)
  return str if non_utf8 == "\n" || non_utf8 == "\t"
  str.slice!(str[/[^[:print:]]/].to_s)
  str
end