Sha256: 84fbaddff1d4b2666ab826b28efa136a81133a2b056cf717d6cac3fbea64618e

Contents?: true

Size: 1004 Bytes

Versions: 2

Compression:

Stored size: 1004 Bytes

Contents

module Opener
  class PropertyTagger
    ##
    # Thread-safe cache for storing the contents of aspect files.
    #
    class AspectsCache
      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 # AspectsCache
  end # PropertyTagger
end # Opener

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
opener-property-tagger-3.2.1 lib/opener/property_tagger/aspects_cache.rb
opener-property-tagger-3.2.0 lib/opener/property_tagger/aspects_cache.rb