Class: TextMood
- Inherits:
-
Object
- Object
- TextMood
- Defined in:
- lib/textmood.rb
Instance Method Summary collapse
-
#analyze(text) ⇒ Object
(also: #analyse)
analyzes the sentiment of the provided text.
-
#initialize(options = {}) ⇒ TextMood
constructor
A new instance of TextMood.
Constructor Details
#initialize(options = {}) ⇒ TextMood
Returns a new instance of TextMood.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/textmood.rb', line 16 def initialize( = {}) [:max_threshold] ||= 0.5 [:min_threshold] ||= -0.5 [:start_ngram] ||= 1 [:end_ngram] ||= 1 @options = if [:lang] if [:alias_file] aliases = load_alias_file([:alias_file]) if aliases file = aliases[[:lang]] unless file raise ArgumentError, "Language tag not found in alias file" end else raise ArgumentError, "Alias file not found" end else file = File.dirname(__FILE__) + "/../lang/#{[:lang]}.txt" end @sentiment_values = load_sentiment_file(file) 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
#analyze(text) ⇒ Object Also known as: analyse
analyzes the sentiment of the provided text.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/textmood.rb', line 55 def analyze(text) sentiment_total = 0.0 scores_added = 0 (@options[:start_ngram]..@options[:end_ngram]).each do |i| ngrams(i, text.to_s).each do |token| score = score_token(token) unless score.nil? sentiment_total += score scores_added += 1 end end end if @options[:normalize_score] sentiment_total = normalize_score(sentiment_total, scores_added) end if @options[:ternary_output] if sentiment_total > @options[:max_threshold] 1 elsif sentiment_total < @options[:min_threshold] -1 else 0 end else sentiment_total end end |