Sha256: 8fdfbf698e65f63b426aa510c2f415ff9ab89be78ef9e5d2d9882d6e20357087

Contents?: true

Size: 846 Bytes

Versions: 3

Compression:

Stored size: 846 Bytes

Contents

module NewspaperWorks
  # Module for text extraction (OCR or otherwise)
  module TextExtraction
    class WordCoordsBuilder
      def initialize(words, width = nil, height = nil)
        @words = words
        @width = width
        @height = height
      end

      # Output JSON flattened word coordinates
      #
      # @return [String] JSON serialization of flattened word coordinates
      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
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
newspaper_works-1.0.1 lib/newspaper_works/text_extraction/word_coords_builder.rb
newspaper_works-1.0.0 lib/newspaper_works/text_extraction/word_coords_builder.rb
newspaper_works-0.1.0 lib/newspaper_works/text_extraction/word_coords_builder.rb