Class: VaderSentimentRuby::SentimentScoresSifter

Inherits:
Object
  • Object
show all
Defined in:
lib/vader_sentiment_ruby/sentiment_scores_sifter.rb

Overview

Separates positive versus negative sentiment scores

Instance Method Summary collapse

Constructor Details

#initialize(sentiments) ⇒ SentimentScoresSifter

Returns a new instance of SentimentScoresSifter.

Parameters:

  • sentiments (Array<Float>)

    Array of sentiments generated from words



7
8
9
10
11
12
# File 'lib/vader_sentiment_ruby/sentiment_scores_sifter.rb', line 7

def initialize(sentiments)
  @sentiments = sentiments
  @pos_sum = 0.0
  @neg_sum = 0.0
  @neu_count = 0
end

Instance Method Details

#callArray<Float, Float, Integer>

Examples:

[2.3, -3.2, 3]

Returns:

  • (Array<Float, Float, Integer>)


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/vader_sentiment_ruby/sentiment_scores_sifter.rb', line 17

def call
  @sentiments.each do |sentiment_score|
    # compensates for neutral words that are counted as 1
    @pos_sum += sentiment_score.to_f + 1 if sentiment_score.positive?

    # when used with .abs, compensates for neutrals
    @neg_sum += sentiment_score.to_f - 1 if sentiment_score.negative?

    @neu_count += 1 if sentiment_score.zero?
  end

  [@pos_sum, @neg_sum, @neu_count]
end