Class: Opener::PropertyTagger::FileAspectsCache
- Inherits:
-
Object
- Object
- Opener::PropertyTagger::FileAspectsCache
- Includes:
- MonitorMixin
- Defined in:
- lib/opener/property_tagger/file_aspects_cache.rb
Overview
Thread-safe cache for storing the contents of aspect files.
Instance Method Summary collapse
-
#[](path) ⇒ Object
(also: #get)
Returns the aspects for the given file path.
-
#initialize ⇒ FileAspectsCache
constructor
A new instance of FileAspectsCache.
-
#load_aspects(path) ⇒ Object
Loads the aspects of the given path.
Constructor Details
#initialize ⇒ FileAspectsCache
Returns a new instance of FileAspectsCache.
10 11 12 13 14 |
# File 'lib/opener/property_tagger/file_aspects_cache.rb', line 10 def initialize super @cache = {} end |
Instance Method Details
#[](path) ⇒ Object Also known as: get
Returns the aspects for the given file path. If the aspects don’t exist they are first loaded into the cache.
22 23 24 25 26 |
# 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 |
#load_aspects(path) ⇒ Object
Loads the aspects of the given path.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# 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 |