Class: Opener::PolarityTagger::LexiconsCache
- Inherits:
-
Object
- Object
- Opener::PolarityTagger::LexiconsCache
- Includes:
- MonitorMixin
- Defined in:
- lib/opener/polarity_tagger/lexicons_cache.rb
Instance Method Summary collapse
- #[](**params) ⇒ Object (also: #get)
- #cache_update(existing = nil, **params) ⇒ 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.
7 8 9 10 11 12 13 |
# File 'lib/opener/polarity_tagger/lexicons_cache.rb', line 7 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
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/opener/polarity_tagger/lexicons_cache.rb', line 15 def [] **params synchronize do if existing = @cache[params] existing.tap do Thread.new{ @cache[params] = cache_update existing, **params } end else @cache[params] = cache_update **params end end end |
#cache_update(existing = nil, **params) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/opener/polarity_tagger/lexicons_cache.rb', line 28 def cache_update existing = nil, **params from = Time.now lexicons = load_lexicons cache: existing, **params return existing if existing and lexicons.blank? Hashie::Mash.new( lexicons: lexicons, from: from, ) end |
#load_from_path(lang:, **params) ⇒ Object
55 56 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 |
# File 'lib/opener/polarity_tagger/lexicons_cache.rb', line 55 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
45 46 47 48 49 50 51 52 53 |
# File 'lib/opener/polarity_tagger/lexicons_cache.rb', line 45 def load_from_url lang:, cache:, **params url = "#{@url}&language_code=#{lang}&#{params.to_query}" url += "&if_updated_since=#{cache.from.iso8601}" if cache puts "#{lang}: loading lexicons from url #{url}" lexicons = JSON.parse HTTPClient.new.get(url).body lexicons = lexicons['data'].map{ |l| Hashie::Mash.new l } lexicons end |
#load_lexicons(lang:, **params) ⇒ Object
39 40 41 42 43 |
# File 'lib/opener/polarity_tagger/lexicons_cache.rb', line 39 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 |