Class: Sentiments::Classifier

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

Constant Summary collapse

MIN_TOKEN_LENGTH =
1
MAX_TOKEN_LENGTH =
15
PRIOR_SCORE =
0.33

Instance Method Summary collapse

Constructor Details

#initializeClassifier

Returns a new instance of Classifier.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sentiments.rb', line 8

def initialize
  # @neg_words    = load_json('neg')
  # @pos_words    = load_json('pos')
  # @neu_words    = load_json('neu')
  @prefix_words = load_json('prefix')
  @ignore_words = load_json('ignore')

  @categories = ['pos', 'neg', 'neu']

  @dictionary = {}
  @doc_count = 0
  @token_count = 0
  @category_tok_count = {'pos' => 0, 'neg' => 0, 'neu' => 0}
  @category_doc_count = {'pos' => 0, 'neg' => 0, 'neu' => 0}

  @categories.each do |category|
    if (!set_dictionary(category))
      raise "Unable to Set Dictionaries"
    end #if
  end

end

Instance Method Details

#score(sentence) ⇒ Object

initialize



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sentiments.rb', line 31

def score(sentence)
  scores = {}
  @prefix_words.each do |pw|
    # Search if that prefix is in the sentence
    if sentence.index(pw)
      # Remove the white space after the negative prefix
      sentence.sub! pw+" ", pw
    end
  end
  tokens = tokenize(sentence)
  total_score = 0

  @categories.each do |category|
    scores[category] = 1
    tokens.each do |token|
      if (token.length > MIN_TOKEN_LENGTH && token.length < MAX_TOKEN_LENGTH && !(@ignore_words.include? token))

        # If Token is not in our dictionary, don't do anything
        if(@dictionary[token].nil?)
          break
        end #if

        if (@dictionary[token][category].nil?)
          count = 0
        else
          count = @dictionary[token][category]
        end # elseif
        scores[category] *= (count + 1)
      end #if
    end #tokens
    scores[category] = PRIOR_SCORE * scores[category]
  end #categories

  @categories.each do |category|
    total_score += scores[category]
  end #categories

  @categories.each do |category|
    scores[category] = scores[category]/total_score
  end #categories

  return scores
end