Sha256: 882c5c335f204fe23a16d175e46f507c235c7cd1b7d69e904a8eb09772a815f3

Contents?: true

Size: 1.26 KB

Versions: 6

Compression:

Stored size: 1.26 KB

Contents

module IiifPrint
  # Module for text extraction (OCR or otherwise)
  module TextExtraction
    class WordCoordsBuilder
      # @params words [Array<Hash>] an array of hash objects that have the keys `:word` and `:coordinates`.
      # @params width [Integer] the width of the "canvas" on which the words appear.
      # @params height [Integer] the height of the "canvas" on which the words appear.
      # @return [String] a JSON encoded string.
      def self.json_coordinates_for(words:, width: nil, height: nil)
        new(words, width, height).to_json
      end

      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

6 entries across 6 versions & 1 rubygems

Version Path
iiif_print-3.0.1 lib/iiif_print/text_extraction/word_coords_builder.rb
iiif_print-3.0.0 lib/iiif_print/text_extraction/word_coords_builder.rb
iiif_print-2.0.1 lib/iiif_print/text_extraction/word_coords_builder.rb
iiif_print-2.0.0 lib/iiif_print/text_extraction/word_coords_builder.rb
iiif_print-1.1.0 lib/iiif_print/text_extraction/word_coords_builder.rb
iiif_print-1.0.0 lib/iiif_print/text_extraction/word_coords_builder.rb