class LogStash::Filters::Sentimentalizer

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

Public Instance Methods

filter(event) click to toggle source
# File lib/logstash/filters/sentimentalizer.rb, line 34
def filter(event)
  return unless filter?(event)

  unless event.get(@source).nil?
    source = event.get(@source).dup
    begin
      sentiment = Sentimentalizer.analyze(
        @scrub ? source.gsub(/\B#(\S+)\b/, '\1') : 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
register() click to toggle source
# 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