lib/banktools-se/ocr.rb in banktools-se-2.6.1 vs lib/banktools-se/ocr.rb in banktools-se-2.6.2

- old
+ new

@@ -71,11 +71,11 @@ digit_string = string.gsub(/\D/, "") digit_string_length = digit_string.length candidates = [] - # Then find all substrings of min_length, and of all other lengths, up to max_length. + # Then find all substrings ("n-grams") of min_length, and of all other lengths, up to max_length. # So e.g. find all four-digit substrings ("1234", "2345", …), all five-digit substrings and so on. 0.upto(digit_string.length - min_length) do |start_pos| min_end_pos = start_pos + min_length - 1 max_end_pos = [ start_pos + max_length, digit_string_length ].min - 1 @@ -83,33 +83,21 @@ min_end_pos.upto(max_end_pos) do |end_pos| candidates << digit_string.slice(start_pos..end_pos) end end + candidates.uniq! + # Finally, limit these substrings to ones that are actually valid OCRs. candidates.select { |candidate| begin to_number(candidate, length_digit: length_digit, pad: pad) true rescue InvalidOCR false end - }.uniq - end - - private - - private_class_method \ - def self.with_numbers_found_by_removing_prefix_and_postfix(numbers) - numbers + numbers.flat_map { |number| - 0.upto(number.size).flat_map { |i| - [ - number[0...i], - number[i...number.size], - ] - } - } + } end end end end