Class: IiifPrint::TextExtraction::WordCoordsBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/iiif_print/text_extraction/word_coords_builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(words, width = nil, height = nil) ⇒ WordCoordsBuilder

Returns a new instance of WordCoordsBuilder.



13
14
15
16
17
# File 'lib/iiif_print/text_extraction/word_coords_builder.rb', line 13

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

Class Method Details

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

Returns a JSON encoded string.

Returns:

  • (String)

    a JSON encoded string.



9
10
11
# File 'lib/iiif_print/text_extraction/word_coords_builder.rb', line 9

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

Instance Method Details

#to_jsonString

Output JSON flattened word coordinates

Returns:

  • (String)

    JSON serialization of flattened word coordinates



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/iiif_print/text_extraction/word_coords_builder.rb', line 22

def to_json
  coordinates = {}
  @words.each do |w|
    word_chars = w[:word]
    word_coords = w[: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