class Sentimental

Attributes

influencers[RW]
neutral_regexps[RW]
ngrams[RW]
threshold[RW]
word_scores[RW]

Public Class Methods

new(threshold: 0, word_scores: nil, neutral_regexps: [], ngrams: 1, influencers: nil) click to toggle source
# 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

Public Instance Methods

classify(string) click to toggle source
# File lib/sentimental.rb, line 40
def classify(string)
  sentiment(string) == :positive
end
load_defaults() click to toggle source
# 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) click to toggle source
# File lib/sentimental.rb, line 51
def load_from(filename)
  load_to(filename, word_scores)
end
Also aliased as: load_senti_file, load_senti_file
load_from_json(filename) click to toggle source
# File lib/sentimental.rb, line 63
def load_from_json(filename)
  word_scores.merge!(hash_from_json(filename))
end
Also aliased as: load_senti_json
load_influencers(filename) click to toggle source
# File lib/sentimental.rb, line 55
def load_influencers(filename)
  load_to(filename, influencers)
end
load_influencers_from_json(filename) click to toggle source
# File lib/sentimental.rb, line 67
def load_influencers_from_json(filename)
  influencers.merge!(hash_from_json(filename))
end
load_senti_file(filename)
Alias for: load_from
load_senti_json(filename)
Alias for: load_from_json
load_to(filename, hash) click to toggle source
# File lib/sentimental.rb, line 59
def load_to(filename, hash)
  hash.merge!(hash_from_txt(filename))
end
score(string) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

extract_words(string) click to toggle source
# File lib/sentimental.rb, line 88
def extract_words(string)
  string.to_s.downcase.scan(/([\w']+|\S{2,})/).flatten
end
extract_words_with_n_grams(string) click to toggle source
# File lib/sentimental.rb, line 92
def extract_words_with_n_grams(string)
  words = extract_words(string)
  1.upto(ngrams).map do |number|
    words.each_cons(number).to_a
  end.flatten(1).map { |word| word.join(" ") }
end
influence_score() click to toggle source
# File lib/sentimental.rb, line 99
def influence_score
  @total_score < 0.0 ? -@influence : +@influence
end
process_word(scoring, word) click to toggle source
# File lib/sentimental.rb, line 78
def process_word(scoring, word)
  if influencers[word] > 0
    scoring[:current_influencer] *= influencers[word]
  else
    scoring[:score] += word_scores[word] * scoring[:current_influencer]
    scoring[:current_influencer] = 1.0
  end
  scoring
end