Class: Opener::PolarityTagger::LexiconsCache

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/opener/polarity_tagger/lexicons_cache.rb

Constant Summary collapse

UPDATE_INTERVAL =
(ENV['CACHE_EXPIRE_MINS']&.to_i || 5).minutes

Instance Method Summary collapse

Constructor Details

#initializeLexiconsCache

Returns a new instance of LexiconsCache.



9
10
11
12
13
14
15
# File 'lib/opener/polarity_tagger/lexicons_cache.rb', line 9

def initialize
  super #MonitorMixin

  @url   = ENV['POLARITY_LEXICON_URL']
  @path  = ENV['POLARITY_LEXICON_PATH']
  @cache = {}
end

Instance Method Details

#[](**params) ⇒ Object Also known as: get



17
18
19
20
21
22
23
24
# File 'lib/opener/polarity_tagger/lexicons_cache.rb', line 17

def [] **params
  existing = @cache[params]
  return existing if existing and existing.from > UPDATE_INTERVAL.ago

  synchronize do
    @cache[params] = cache_update @cache[params], **params
  end
end

#cache_update(existing = nil, **params) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/opener/polarity_tagger/lexicons_cache.rb', line 27

def cache_update existing = nil, **params
  from     = Time.now
  lexicons = load_lexicons cache: existing, **params

  if existing and lexicons.blank?
    existing.from = from
    return existing
  end

  Hashie::Mash.new(
    lexicons: lexicons,
    from:     from,
  )
end

#httpObject



89
90
91
92
93
94
95
96
97
# File 'lib/opener/polarity_tagger/lexicons_cache.rb', line 89

def http
  return @http if @http

  @http = HTTPClient.new
  @http.send_timeout    = 120
  @http.receive_timeout = 120
  @http.connect_timeout = 120
  @http
end

#load_from_path(lang:, **params) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/opener/polarity_tagger/lexicons_cache.rb', line 58

def load_from_path lang:, **params
  @path  ||= 'core/general-lexicons'
  dir      = "#{@path}/#{lang.upcase}-lexicon"
  config   = Nokogiri::XML File.read "#{dir}/config.xml"
  lexicons = []

  config.css(:lexicon).each do |cl|
    filename = cl.at(:filename).text
    resource = cl.at(:resource).text
    xml      = Nokogiri::XML File.read "#{dir}/#{filename}"
    puts "#{lang}: loading lexicons from the file #{filename}"

    lexicons.concat(xml.css(:LexicalEntry).map do |le|
      Hashie::Mash.new(
        resource:   resource,
        identifier: le.attr(:id),
        type:       le.attr(:type),
        lemma:      le.at(:Lemma).attr(:writtenForm).downcase,
        pos:        le.attr(:partOfSpeech)&.downcase,
        aspect:     le.at(:Domain)&.attr(:aspect)&.downcase,
        polarity:   le.at(:Sentiment).attr(:polarity),
        strength:   le.at(:Sentiment).attr(:strength),
        confidence_level:   le.at(:Confidence)&.attr(:level),
        domain_conditional: le.at(:Domain)&.attr(:conditional) == 'yes',
      )
    end)
  end

  lexicons
end

#load_from_url(lang:, cache:, **params) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/opener/polarity_tagger/lexicons_cache.rb', line 48

def load_from_url lang:, cache:, **params
  url  = "#{@url}&language_code=#{lang}&#{params.to_query}"
  url += "&if_updated_since=#{cache.from.utc.iso8601}" if cache
  puts "#{lang}: loading lexicons from url #{url}"

  lexicons = JSON.parse http.get(url).body
  lexicons = lexicons['data'].map{ |l| Hashie::Mash.new l }
  lexicons
end

#load_lexicons(lang:, **params) ⇒ Object



42
43
44
45
46
# File 'lib/opener/polarity_tagger/lexicons_cache.rb', line 42

def load_lexicons lang:, **params
  lexicons = if @url then load_from_url lang: lang, **params else load_from_path lang: lang, **params end

  LexiconMap.new lang: lang, lexicons: lexicons
end