class SadPanda::Polarity

Polarity calculation logic in here

Attributes

polarities[R]
words[R]

Public Class Methods

new(text) click to toggle source
# File lib/sad_panda/polarity.rb, line 10
def initialize(text)
  @words = words_in(text)
  @polarities = []
end

Public Instance Methods

call() click to toggle source

Main method that initiates calculating polarity

# File lib/sad_panda/polarity.rb, line 16
def call
  words = stems_for(remove_stopwords_in(@words))

  score_polarities_for(frequencies_for(words))

  polarities.empty? ? 5.0 : (polarities.inject(0){ |sum, polarity| sum + polarity } / polarities.length)
end

Private Instance Methods

score_emoticon_polarity() click to toggle source

Checks if words has happy or sad emoji and adds polarity for it

# File lib/sad_panda/polarity.rb, line 27
def score_emoticon_polarity
  happy = happy_emoticon?(words)
  sad = sad_emoticon?(words)

  polarities << 5.0 if happy && sad
  polarities << 8.0 if happy
  polarities << 2.0 if sad
end
score_polarities_for(word_frequencies) click to toggle source

Appends polarities of words to array polarities

# File lib/sad_panda/polarity.rb, line 37
def score_polarities_for(word_frequencies)
  word_frequencies.each do |word, frequency|
    polarity = SadPanda::Bank::POLARITIES[word.to_sym]
    polarities << (polarity * frequency.to_f) if polarity
  end

  score_emoticon_polarity
end