Method: Birdwatcher::Modules::Statuses::Sentiment#run

Defined in:
lib/birdwatcher/modules/statuses/sentiment.rb

#runObject



29
30
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
# File 'lib/birdwatcher/modules/statuses/sentiment.rb', line 29

def run
  statuses = current_workspace.statuses_dataset.where(:sentiment => nil)
  if statuses.empty?
    error("There are no statuses to analyze")
    return false
  end
  analyser = Sentimental.new
  threads  = thread_pool(option_setting("THREADS").to_i)
  task("Training the sentiment analyzer...") do
    analyser.load_defaults
  end
  statuses.each do |status|
    threads.process do
      begin
        text = sanitize_text(status.text)
        sentiment = analyser.sentiment(text)
        case sentiment
        when :positive
          info("Positive: ".bold.light_green + Birdwatcher::Util.excerpt(status.text, 80))
        when :negative
          info("Negative: ".bold.light_red + Birdwatcher::Util.excerpt(status.text, 80))
        else
          info(" Neutral: ".bold + Birdwatcher::Util.excerpt(status.text, 80))
        end
        status.sentiment = sentiment.to_s
        status.save
      rescue => e
        error("Sentiment analysis for status #{status.id.bold} failed (#{e.class})")
      end
    end
  end
  threads.shutdown
end