Class: Agents::NaiveBayesAgent

Inherits:
Agent
  • Object
show all
Defined in:
lib/huginn_naive_bayes_agent/naive_bayes_agent.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_yml(yml_data) ⇒ Object



142
143
144
145
146
# File 'lib/huginn_naive_bayes_agent/naive_bayes_agent.rb', line 142

def self.from_yml(yml_data)
  nbayes = YAML.load(yml_data)
  nbayes.reset_after_import()  # yaml does not properly set the defaults on the Hashes
  nbayes
end

Instance Method Details

#default_optionsObject



47
48
49
50
51
52
53
54
55
# File 'lib/huginn_naive_bayes_agent/naive_bayes_agent.rb', line 47

def default_options
  {
    'min_value' => "0.5",
    'propagate_training_events' => 'true',
    'expected_update_period_in_days' => "7",
    'strip_punctuation' => 'false',
    'stem' => 'false'
  }
end

#load(dat) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/huginn_naive_bayes_agent/naive_bayes_agent.rb', line 132

def load(dat)
  if dat.nil?
    nbayes = NBayes::Base.new
  else
    nbayes = self.class.from_yml(dat)
  end
  nbayes
end

#receive(incoming_events) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/huginn_naive_bayes_agent/naive_bayes_agent.rb', line 66

def receive(incoming_events)
  incoming_events.each do |event|
    nbayes = load(memory['data'])
    # validate incoming payload
    if !event.payload['nb_cats']
      error("Missing `nb_cats` field in the event payload. #{event.payload.to_s}")
raise 'Missing `nb_cats` field in the event payload.'
    end
    if !event.payload['nb_content']
      error("Missing `nb_content` field in the event payload. #{event.payload.to_s}")
      raise 'Missing `nb_content` field in the event payload.'
    end 
    # train or modify existing classifier
    if event.payload['nb_cats'].length > 0 and not event.payload['nb_cats'].include?("=class")
      cats = event.payload['nb_cats'].split(/\s+/)
      if cats[0] == "=loadYML"
        memory['data'] = event.payload['nb_content']
      elsif cats[0] == "=delCat"
        ca = event.payload['nb_content'].split(/\s+/)
        ca.each do |c|
          nbayes.delete_category(c)
        end
        memory['data'] = YAML.dump(nbayes)
      elsif cats[0] == "=purgeTokens"
        nbayes.purge_less_than(event.payload['nb_content'].to_i)
        memory['data'] = YAML.dump(nbayes)
      else
        nb_content = event.payload['nb_content']
        if interpolated['strip_punctuation'] == "true"
          nb_content = nb_content.gsub(/[^[:word:]\s]/, '') #https://stackoverflow.com/a/10074271
        end
        if interpolated['stem'] == "true"
          nb_content = nb_content.split(/\s+/).map{|word| word.stem}.join(" ")
        end
        cats.each do |c|
          c.starts_with?('-') ? nbayes.untrain(nb_content.split(/\s+/), c[1..-1]) : nbayes.train(nb_content.split(/\s+/), c)
        end
        memory['data'] = YAML.dump(nbayes)
        if interpolated['propagate_training_events'] == "true"
          create_event payload: event.payload
        end
      end
    # classify new data
    else
      nb_content = event.payload['nb_content']
      if interpolated['strip_punctuation'] == "true"
        nb_content = nb_content.gsub(/[^[:word:]\s]/, '') #https://stackoverflow.com/a/10074271
      end
      if interpolated['stem'] == "true"
        nb_content = nb_content.split(/\s+/).map{|word| word.stem}.join(" ")
      end
      result = nbayes.classify(nb_content.split(/\s+/))
      if interpolated['min_value'].to_f == 1
        event.payload['nb_cats'] << (event.payload['nb_cats'].length == 0 ? result.max_class : " "+result.max_class)
      else
        result.each do |cat, val|
          if val > interpolated['min_value'].to_f
            event.payload['nb_cats'] << (event.payload['nb_cats'].length == 0 ? cat : " "+cat)
          end
        end
      end
      create_event payload: event.payload
    end
  end
end

#validate_optionsObject



57
58
59
60
# File 'lib/huginn_naive_bayes_agent/naive_bayes_agent.rb', line 57

def validate_options
  errors.add(:base, "expected_update_period_in_days must be present") unless options['expected_update_period_in_days'].present?
  errors.add(:base, "minimum value must be greater than 0 and less than or equal to 1, e.g. 0.5") unless (0 < options['min_value'].to_f && options['min_value'].to_f <= 1)
end

#working?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/huginn_naive_bayes_agent/naive_bayes_agent.rb', line 62

def working?
  received_event_without_error?
end