class NewspaperWorks::TextExtraction::WordCoordsBuilder

Public Class Methods

new(words, width = nil, height = nil) click to toggle source
# File lib/newspaper_works/text_extraction/word_coords_builder.rb, line 5
def initialize(words, width = nil, height = nil)
  @words = words
  @width = width
  @height = height
end

Public Instance Methods

to_json() click to toggle source

Output JSON flattened word coordinates

@return [String] JSON serialization of flattened word coordinates

# File lib/newspaper_works/text_extraction/word_coords_builder.rb, line 14
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