Class: SentimentLib::Analysis::Strategy

Inherits:
Object
  • Object
show all
Defined in:
lib/sentiment_lib/analysis/strategy.rb

Instance Method Summary collapse

Instance Method Details

#normalize(str) ⇒ Object



5
6
7
# File 'lib/sentiment_lib/analysis/strategy.rb', line 5

def normalize(str)
  str.gsub(/[^[:alnum:]]/, ' ').downcase
end

#tokenize(str) ⇒ Object



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

def tokenize(str)
  str.split.map(&:stem)
end

#weigh(tokens) ⇒ Object

default weight is an average of per-word weight. Optionally override in derived strategies.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sentiment_lib/analysis/strategy.rb', line 15

def weigh(tokens)
  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]
  }

  avg = sum / valid_tokens.length

  avg          
end