Class: Opener::PropertyTagger::FileAspectsCache

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeFileAspectsCache

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.

Parameters:

  • path (String)


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.

Parameters:

  • path (String)


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