Sha256: 695e902648c17449962f0b3c180d609e70a8c1037f312afbd2c2447b765a8b25

Contents?: true

Size: 1.06 KB

Versions: 4

Compression:

Stored size: 1.06 KB

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")
          l = Hashie::Mash.new(
            lemma:  lemma,
            pos:    pos,
            aspect: aspect,
          )

          mapping[l.lemma.to_sym] << l
        end

        return mapping
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
opener-property-tagger-3.4.3 lib/opener/property_tagger/file_aspects_cache.rb
opener-property-tagger-3.4.2 lib/opener/property_tagger/file_aspects_cache.rb
opener-property-tagger-3.4.1 lib/opener/property_tagger/file_aspects_cache.rb
opener-property-tagger-3.4.0 lib/opener/property_tagger/file_aspects_cache.rb