Class: LogStash::Filters::Sentimentalizer

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/filters/sentimentalizer.rb

Overview

This plugin will analyze sentiment of a specified field and enrich the event with sentiment probability values.

Instance Method Summary collapse

Instance Method Details

#filter(event) ⇒ Object

def register



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
62
# File 'lib/logstash/filters/sentimentalizer.rb', line 34

def filter(event)
  return unless filter?(event)

  source = event.get(@source)
  source.gsub!(/\B#(\S+)\b/, '\1') if @scrub

  unless source.nil?
    begin
      sentiment = Sentimentalizer.analyze(source)
    rescue NoMethodError => e
      @logger.error(
        'Error parsing sentiment for field',
        :exception => e,
        :field => source
      )
    end

    unless sentiment.nil?
      event.set(
        @target,
        'probability' => sentiment.overall_probability,
        'polarity'    => sentiment.sentiment
      )
    end
  end

  # filter_matched should go in the last line of our successful code
  filter_matched(event)
end

#registerObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/logstash/filters/sentimentalizer.rb', line 22

def register
  require 'sentimentalizer'

  # Monkey patch the weird defaults for positive/negative string values
  %w[POSITIVE NEGATIVE NEUTRAL].each do |s|
    Sentiment.send(:remove_const, s)
    Sentiment.const_set(s, s.downcase)
  end

  Sentimentalizer.setup
end