Class: Mindee::V1::Parsing::Common::OCR::OCRPage

Inherits:
Object
  • Object
show all
Defined in:
lib/mindee/v1/parsing/common/ocr/ocr.rb,
sig/mindee/v1/parsing/common/ocr/ocr.rbs

Overview

OCR extraction for a single page.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prediction) ⇒ OCRPage

Returns a new instance of OCRPage.

Parameters:



71
72
73
74
75
76
77
# File 'lib/mindee/v1/parsing/common/ocr/ocr.rb', line 71

def initialize(prediction)
  @lines = [] # : Array[Mindee::V1::Parsing::Common::OCR::OCRLine]
  @all_words = [] # : Array[Mindee::V1::Parsing::Common::OCR::OCRWord]
  prediction['all_words'].each do |word_prediction|
    @all_words.push(OCRWord.new(word_prediction))
  end
end

Instance Attribute Details

#all_wordsArray<OCRWord> (readonly)

All the words on the page, in semi-random order.

Returns:



66
67
68
# File 'lib/mindee/v1/parsing/common/ocr/ocr.rb', line 66

def all_words
  @all_words
end

#linesArray<OCRLine> (readonly)

Returns:



68
69
70
# File 'lib/mindee/v1/parsing/common/ocr/ocr.rb', line 68

def lines
  @lines
end

Instance Method Details

#all_linesArray<OCRLine>

All the words on the page, ordered in lines.

Returns:



81
82
83
84
# File 'lib/mindee/v1/parsing/common/ocr/ocr.rb', line 81

def all_lines
  @lines = to_lines if @lines.empty?
  @lines
end

#parse_one(sorted_words, current, indexes, lines) ⇒ Array[OCRLine]?

Helper function that iterates through all the words and compares them to a candidate

Parameters:

Returns:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/mindee/v1/parsing/common/ocr/ocr.rb', line 105

def parse_one(sorted_words, current, indexes, lines)
  line = OCRLine.new(nil, [])
  sorted_words.each_with_index do |word, idx|
    next if indexes.include?(idx)

    if current.nil?
      current = word
      indexes.push(idx)
      line = OCRLine.new([])
      line.push(word)
    elsif words_on_same_line?(current, word)
      line.push(word)
      indexes.push(idx)
    end
  end
  lines.push(line.sort_on_x) if line.any?
end

#to_linesArray<OCRLine>

Order all the words on the page into lines.

Returns:



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/mindee/v1/parsing/common/ocr/ocr.rb', line 125

def to_lines
  current = nil
  indexes = [] # : Array[Integer]
  lines = [] # : Array[Mindee::V1::Parsing::Common::OCR::OCRLine]

  # make sure words are sorted from top to bottom
  all_words = @all_words.sort_by { |word| Geometry.get_min_max_y(word.polygon).min }
  all_words.each do
    parse_one(all_words, current, indexes, lines)
    current = nil
  end
  lines
end

#to_sString

Returns:



87
88
89
90
91
92
93
94
95
96
# File 'lib/mindee/v1/parsing/common/ocr/ocr.rb', line 87

def to_s
  lines = all_lines
  return '' if lines.empty?

  out_str = String.new
  lines.map do |line|
    out_str << "#{line}\n" unless line.to_s.strip.empty?
  end
  out_str.strip
end

#words_on_same_line?(current_word, next_word) ⇒ bool

Determine if two words are on the same line.

Parameters:

Returns:



143
144
145
146
147
# File 'lib/mindee/v1/parsing/common/ocr/ocr.rb', line 143

def words_on_same_line?(current_word, next_word)
  current_in_next = current_word.polygon.point_in_y?(next_word.polygon.centroid)
  next_in_current = next_word.polygon.point_in_y?(current_word.polygon.centroid) unless current_word.nil?
  current_in_next || next_in_current
end