Sha256: eaae9da8a7d35f43b514b97b455fb6d0b51f72cd8afc6d05d0e000af309510f3

Contents?: true

Size: 969 Bytes

Versions: 4

Compression:

Stored size: 969 Bytes

Contents

module Opener
  class PropertyTagger
    ##
    # Thread-safe cache for storing the contents of aspect files.
    #
    class FileAspectsCache

      include MonitorMixin

      def initialize
        super

        @cache = {}
      end

      ##
      # Returns the aspects for the given file path. If the aspects don't exist
      # they are first loaded into the cache.
      #
      # @param [String] path
      #
      def [](path)
        synchronize do
          @cache[path] = load_aspects(path) unless @cache.key?(path)
        end
      end

      alias_method :get, :[]

      ##
      # Loads the aspects of the given path.
      #
      # @param [String] path
      #
      def load_aspects(path)
        mapping = Hash.new { |hash, key| hash[key] = [] }

        File.foreach(path) do |line|
          lemma, pos, aspect = line.chomp.split("\t")

          mapping[lemma.to_sym] << aspect
        end

        return mapping
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
opener-property-tagger-3.3.4 lib/opener/property_tagger/file_aspects_cache.rb
opener-property-tagger-3.3.3 lib/opener/property_tagger/file_aspects_cache.rb
opener-property-tagger-3.3.2 lib/opener/property_tagger/file_aspects_cache.rb
opener-property-tagger-3.3.1 lib/opener/property_tagger/file_aspects_cache.rb