class Opener::PropertyTagger::FileAspectsCache

Thread-safe cache for storing the contents of aspect files.

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/opener/property_tagger/file_aspects_cache.rb, line 10
def initialize
  super

  @cache = {}
end

Public Instance Methods

[](path) click to toggle source

Returns the aspects for the given file path. If the aspects don't exist they are first loaded into the cache.

@param [String] path

# File lib/opener/property_tagger/file_aspects_cache.rb, line 22
def [](path)
  synchronize do
    @cache[path] = load_aspects(path) unless @cache.key?(path)
  end
end
Also aliased as: get
get(path)
Alias for: []
load_aspects(path) click to toggle source

Loads the aspects of the given path.

@param [String] path

# File lib/opener/property_tagger/file_aspects_cache.rb, line 35
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