Class: DerivativeRodeo::Services::ExtractWordCoordinatesFromHocrSgmlService::WordCoordinates

Inherits:
Object
  • Object
show all
Defined in:
lib/derivative_rodeo/services/extract_word_coordinates_from_hocr_sgml_service.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(words:, width:, height:) ⇒ WordCoordinates

Returns a new instance of WordCoordinates.



210
211
212
213
214
# File 'lib/derivative_rodeo/services/extract_word_coordinates_from_hocr_sgml_service.rb', line 210

def initialize(words:, width:, height:)
  @words = words
  @width = width
  @height = height
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



215
216
217
# File 'lib/derivative_rodeo/services/extract_word_coordinates_from_hocr_sgml_service.rb', line 215

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



215
216
217
# File 'lib/derivative_rodeo/services/extract_word_coordinates_from_hocr_sgml_service.rb', line 215

def width
  @width
end

#wordsObject (readonly)

Returns the value of attribute words.



215
216
217
# File 'lib/derivative_rodeo/services/extract_word_coordinates_from_hocr_sgml_service.rb', line 215

def words
  @words
end

Class Method Details

.to_json(words:, width: nil, height: nil) ⇒ String

Returns a JSON encoded string.

Parameters:

  • words (Array<Hash>)

    an array of hash objects that have the keys ‘:word` and `:coordinates`.

  • width (Integer) (defaults to: nil)

    the width of the “canvas” on which the words appear.

  • height (Integer) (defaults to: nil)

    the height of the “canvas” on which the words appear.

Returns:

  • (String)

    a JSON encoded string.



206
207
208
# File 'lib/derivative_rodeo/services/extract_word_coordinates_from_hocr_sgml_service.rb', line 206

def self.to_json(words:, width: nil, height: nil)
  new(words: words, width: width, height: height).to_json
end

Instance Method Details

#to_jsonString

Output JSON flattened word coordinates

Returns:

  • (String)

    JSON serialization of flattened word coordinates



220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/derivative_rodeo/services/extract_word_coordinates_from_hocr_sgml_service.rb', line 220

def to_json
  coordinates = {}
  words.each do |word|
    word_chars = word[:word]
    word_coords = word[:coordinates]
    if coordinates[word_chars]
      coordinates[word_chars] << word_coords
    else
      coordinates[word_chars] = [word_coords]
    end
  end
  payload = { width: width, height: height, coords: coordinates }
  JSON.generate(payload)
end