Class: SentimentLib::Analysis::Strategies::FinancialDictStrategy

Inherits:
SentimentLib::Analysis::Strategy show all
Defined in:
lib/sentiment_lib/analysis/strategies/financial_dict_strategy.rb

Constant Summary collapse

@@mappings =
nil

Instance Method Summary collapse

Methods inherited from SentimentLib::Analysis::Strategy

#normalize, #tokenize

Constructor Details

#initializeFinancialDictStrategy

Returns a new instance of FinancialDictStrategy.



9
10
11
# File 'lib/sentiment_lib/analysis/strategies/financial_dict_strategy.rb', line 9

def initialize
  self.class._load_mappings if !@@mappings
end

Instance Method Details

#mappingsObject



13
14
15
# File 'lib/sentiment_lib/analysis/strategies/financial_dict_strategy.rb', line 13

def mappings
  @@mappings
end

#weigh(tokens, opts = {}) ⇒ Object

weight is pure sum (not average)



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sentiment_lib/analysis/strategies/financial_dict_strategy.rb', line 18

def weigh(tokens, opts={})
  valid_tokens = tokens.delete_if { |token|
    mappings[token] == nil
  }

  return 0 if valid_tokens.length == 0

  sum = 0

  valid_tokens.each { |token| 
    sum += mappings[token]
  }

  if opts[:average] == true
    sum / valid_tokens.length
  else
    sum
  end
end