Class: Sentimental

Inherits:
Object
  • Object
show all
Includes:
FileReader
Defined in:
lib/sentimental.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FileReader

#hash_from_json, #hash_from_txt

Constructor Details

#initialize(threshold: 0, word_scores: nil, neutral_regexps: [], ngrams: 1, influencers: nil) ⇒ Sentimental



8
9
10
11
12
13
14
15
16
# File 'lib/sentimental.rb', line 8

def initialize(threshold: 0, word_scores: nil, neutral_regexps: [], ngrams: 1, influencers: nil)
  @ngrams = ngrams.to_i.abs if ngrams.to_i >= 1
  @word_scores = word_scores || {}
  @influencers = influencers || {}
  @word_scores.default = 0.0
  @influencers.default = 0.0
  @threshold = threshold
  @neutral_regexps = neutral_regexps
end

Instance Attribute Details

#influencersObject

Returns the value of attribute influencers.



6
7
8
# File 'lib/sentimental.rb', line 6

def influencers
  @influencers
end

#neutral_regexpsObject

Returns the value of attribute neutral_regexps.



6
7
8
# File 'lib/sentimental.rb', line 6

def neutral_regexps
  @neutral_regexps
end

#ngramsObject

Returns the value of attribute ngrams.



6
7
8
# File 'lib/sentimental.rb', line 6

def ngrams
  @ngrams
end

#thresholdObject

Returns the value of attribute threshold.



6
7
8
# File 'lib/sentimental.rb', line 6

def threshold
  @threshold
end

#word_scoresObject

Returns the value of attribute word_scores.



6
7
8
# File 'lib/sentimental.rb', line 6

def word_scores
  @word_scores
end

Instance Method Details

#classify(string) ⇒ Object



40
41
42
# File 'lib/sentimental.rb', line 40

def classify(string)
  sentiment(string) == :positive
end

#load_defaultsObject



44
45
46
47
48
49
# File 'lib/sentimental.rb', line 44

def load_defaults
  %w(slang en_words).each do |filename|
    load_from_json(File.dirname(__FILE__) + "/../data/#{filename}.json")
  end
  load_influencers_from_json(File.dirname(__FILE__) + '/../data/influencers.json')
end

#load_from(filename) ⇒ Object Also known as: load_senti_file



51
52
53
# File 'lib/sentimental.rb', line 51

def load_from(filename)
  load_to(filename, word_scores)
end

#load_from_json(filename) ⇒ Object Also known as: load_senti_json



63
64
65
# File 'lib/sentimental.rb', line 63

def load_from_json(filename)
  word_scores.merge!(hash_from_json(filename))
end

#load_influencers(filename) ⇒ Object



55
56
57
# File 'lib/sentimental.rb', line 55

def load_influencers(filename)
  load_to(filename, influencers)
end

#load_influencers_from_json(filename) ⇒ Object



67
68
69
# File 'lib/sentimental.rb', line 67

def load_influencers_from_json(filename)
  influencers.merge!(hash_from_json(filename))
end

#load_to(filename, hash) ⇒ Object



59
60
61
# File 'lib/sentimental.rb', line 59

def load_to(filename, hash)
  hash.merge!(hash_from_txt(filename))
end

#score(string) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/sentimental.rb', line 18

def score(string)
  return 0 if neutral_regexps.any? { |regexp| string =~ regexp }

  initial_scoring = {score: 0, current_influencer: 1.0}

  extract_words_with_n_grams(string).inject(initial_scoring) do |current_scoring, word|
    process_word(current_scoring, word)
  end[:score]
end

#sentiment(string) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sentimental.rb', line 28

def sentiment(string)
  score = score(string)

  if score < (-1 * threshold)
    :negative
  elsif score > threshold
    :positive
  else
    :neutral
  end
end