Class: TextMood
- Inherits:
-
Object
- Object
- TextMood
- Defined in:
- lib/textmood.rb
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ TextMood
constructor
A new instance of TextMood.
-
#score_text(text) ⇒ Object
analyzes the sentiment of the provided text.
Constructor Details
#initialize(options = {}) ⇒ TextMood
Returns a new instance of TextMood.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/textmood.rb', line 12 def initialize( = {}) [:max_threshold] ||= 0.5 [:min_threshold] ||= -0.5 [:start_ngram] ||= 1 [:end_ngram] ||= 1 = if [:lang] @sentiment_values = load_sentiment_file(File.dirname(__FILE__) + "/../lang/#{options[:lang]}.txt") unless [:include_symbols] == false # load the symbols file (emoticons and other symbols) @sentiment_values.merge!(load_sentiment_file(File.dirname(__FILE__) + "/../lang/symbols.txt")) end else if [:files].empty? raise ArgumentError, "No language or files provided" else @sentiment_values = {} [:files].each do |file| @sentiment_values.merge!(load_sentiment_file(file)) end end end end |
Instance Method Details
#score_text(text) ⇒ Object
analyzes the sentiment of the provided text.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/textmood.rb', line 38 def score_text(text) sentiment_total = 0.0 ([:start_ngram]..[:end_ngram]).each do |i| ngrams(i, text.to_s).each do |token| sentiment_total += score_token(token) end end if [:normalize] if sentiment_total > [:max_threshold] 1 elsif sentiment_total < [:min_threshold] -1 else 0 end else sentiment_total end end |