module QuickExam::Analyst::Common

Public Instance Methods

answer(str) click to toggle source

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

# 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) click to toggle source
# 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_object() click to toggle source
# File lib/quick_exam/analyst/common.rb, line 37
def clean_object
  @object.question.strip!
  @object.answers.map(&:strip!)
  @object
end
collect_object_ticket() click to toggle source
# File lib/quick_exam/analyst/common.rb, line 32
def collect_object_ticket
  @records << clean_object
  reset_object_ticket
end
correct_answer?(str) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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

# 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) click to toggle source
# 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_mark() click to toggle source

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

# File lib/quick_exam/analyst/common.rb, line 107
def regex_answer_mark
  /(^\w[\.|\)|\/])/
end
regex_answer_sentence() click to toggle source

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

?<= : positive lookbehind

# File lib/quick_exam/analyst/common.rb, line 100
def regex_answer_sentence
  /(?<=#{regex_answer_mark}).*/
end
regex_question_mark() click to toggle source

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

# 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_ticket() click to toggle source
# File lib/quick_exam/analyst/common.rb, line 43
def reset_object_ticket
  @object = QuickExam::Record.new()
end
rid_non_ascii!(str) click to toggle source

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)
# 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