Class: Opener::PolarityTagger::LexiconsCache
- Inherits:
-
Object
- Object
- Opener::PolarityTagger::LexiconsCache
- 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
- #[](**params) ⇒ Object (also: #get)
- #cache_update(existing = nil, **params) ⇒ Object
- #http ⇒ Object
-
#initialize ⇒ LexiconsCache
constructor
A new instance of LexiconsCache.
- #load_from_path(lang:, **params) ⇒ Object
- #load_from_url(lang:, cache:, **params) ⇒ Object
- #load_lexicons(lang:, **params) ⇒ Object
Constructor Details
#initialize ⇒ LexiconsCache
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 |
# File 'lib/opener/polarity_tagger/lexicons_cache.rb', line 17 def [] **params synchronize do existing = @cache[params] break existing if existing and existing.from > UPDATE_INTERVAL.ago @cache[params] = cache_update existing, **params end end |
#cache_update(existing = nil, **params) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/opener/polarity_tagger/lexicons_cache.rb', line 26 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 |
#http ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/opener/polarity_tagger/lexicons_cache.rb', line 88 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
57 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 |
# File 'lib/opener/polarity_tagger/lexicons_cache.rb', line 57 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
47 48 49 50 51 52 53 54 55 |
# File 'lib/opener/polarity_tagger/lexicons_cache.rb', line 47 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
41 42 43 44 45 |
# File 'lib/opener/polarity_tagger/lexicons_cache.rb', line 41 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 |