huginn_naive_bayes_agent 0.0.04 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cfd0247b049ebc6b6ea3db9769cda3c359659710
4
- data.tar.gz: d603bd6696576b73f76508f6fdc0150a0590dae2
3
+ metadata.gz: d109e936add2768720a9449441a5cc178e0410c3
4
+ data.tar.gz: 6153de43815a514561531248aae4ac0a2a15224d
5
5
  SHA512:
6
- metadata.gz: 2c425fdf225f131daf867976ceaf5e83cf78843e41997e62218390d110a6adade8ea853061c9ca8ffde77256a63b0045ab2042ed56563271d732e552854d246d
7
- data.tar.gz: 5f19faa6e2064f54c9982655ef635f6556a8378a85924b4082d73ae7d7cfc5533592838e288c5e8e2059d764ae3639a734b42fdf25ded349c6fa09ee753dcf0e
6
+ metadata.gz: df773ad2a00bce7947a47ecd3bc9ebf0c3b0c647c92912a6ef86c24fb84b20dee537f1b81d18a1a5b3fb47a9b1203022be2f5061091adb19797130b921953053
7
+ data.tar.gz: 62b156d2b093b51e794a1518363d35cd43f4ff9b34fa39a42344d6eeebb030f2cd26fc1a2f75109b20a54be737c2558e3fd11a404bae113d8b7a47f6347df588
data/LICENSE.txt CHANGED
@@ -1,21 +1,7 @@
1
- The MIT License (MIT)
2
-
3
1
  Copyright (c) 2017 TODO: Write your name
4
2
 
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
11
4
 
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14
6
 
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,136 @@
1
+ require 'nbayes'
2
+ require'yaml'
3
+
4
+ module Agents
5
+ class NaiveBayesAgent < Agent
6
+ cannot_be_scheduled!
7
+ can_dry_run!
8
+
9
+ description <<-MD
10
+ The Naive Bayes Agent uses incoming Events from certain sources as a training set for Naive Bayes Machine Learning. Then it classifies Events from other sources and adds category tags to them accordingly.
11
+
12
+ All incoming events should have these two fields in their payloads, likely via the Event Formatting or Javascript Agent:
13
+
14
+ * `nb_content` for the content used for classification, space separated.
15
+ * `nb_cats` for the classification categories, space separated.
16
+
17
+ If `nb_cats` is empty, then the content from `nb_content` will be classified according to the training data. The categories will be added to `nb_cats` and then a new event is created with that payload.
18
+
19
+ However, if `nb_cats` is already populated, then the content from `nb_content` will be used as training data for the categories listed in `nb_cats`. For instance, say `nb_cats` consists of `trees`. Then `nb_content` will be used as training data for the category `trees`. The data is saved to the agent memory.
20
+
21
+ When an event is received for classification, the Naive Bayes Agent will assign a value between 0 and 1 representing the likelihood that it falls under a category. The `min_value` option lets you choose the minimum threshold that must be reached before the event is labeled with that category. If `min_value` is set to 1, then the event is labeled with whichever category has the highest value.
22
+
23
+ The option `propagate_training_events` lets you choose whether the training events are emitted along with the classified events. If it is set to false, then no new event will be created from events that already had categories when they were received.
24
+
25
+ To load trained data into an agent's memory, create a Manual Agent with `nb_cats : =loadYML` and `nb_content : your-well-formed-training-data-here`. Use the text input box, not the form view, by clicking "Toggle View" when inputting your training data else whitespace errors occur in the YML. Then submit this to your Naive Bayes Agent.
26
+
27
+ #### Advanced Features
28
+
29
+ ##### Only works if the nbayes dependency was installed from Github, version => .1.2. Rubygems is still .1.1
30
+
31
+ *Be carefull with these functions: see the documentation linked below.*
32
+
33
+ If a category has a `-` in front of it, eg. `-trees`, then the category `trees` will be UNtrained according to that content.
34
+
35
+ Low frequency words that increase processing time and may overfit - tokens with a count less than x (measured by summing across all classes) - can be removed: Set `nb_cats : =purgeTokens` and `nb_content : integer-value`.
36
+
37
+ Categories can be similarly deleted by `nb_cats : =delCat` and `nb_content : categories to delete`.
38
+
39
+ **See [the NBayes ruby gem](https://github.com/oasic/nbayes) and [this blog post](http://blog.oasic.net/2012/06/naive-bayes-for-ruby.html) for more information about the Naive Bayes implementation used here.**
40
+ MD
41
+
42
+ def default_options
43
+ {
44
+ 'min_value' => "0.5",
45
+ 'propagate_training_events' => 'true',
46
+ 'expected_update_period_in_days' => "7"
47
+ }
48
+ end
49
+
50
+ def validate_options
51
+ errors.add(:base, "expected_update_period_in_days must be present") unless options['expected_update_period_in_days'].present?
52
+ 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)
53
+ end
54
+
55
+ def working?
56
+ received_event_without_error?
57
+ end
58
+
59
+ def receive(incoming_events)
60
+ incoming_events.each do |event|
61
+ nbayes = load(memory['data'])
62
+ if event.payload['nb_cats'].length != 0
63
+ cats = event.payload['nb_cats'].split(/\s+/)
64
+ if cats[0] == "=loadYML"
65
+ memory['data'] = event.payload['nb_content']
66
+ elsif cats[0] == "=delCat"
67
+ ca = event.payload['nb_content'].split(/\s+/)
68
+ ca.each do |c|
69
+ nbayes.delete_category(c)
70
+ end
71
+ memory['data'] = YAML.dump(nbayes)
72
+ elsif cats[0] == "=purgeTokens"
73
+ nbayes.purge_less_than(event.payload['nb_content'].to_i)
74
+ memory['data'] = YAML.dump(nbayes)
75
+ else
76
+ cats.each do |c|
77
+ c.starts_with?('-') ? nbayes.untrain(event.payload['nb_content'].split(/\s+/), c[1..-1]) : nbayes.train(event.payload['nb_content'].split(/\s+/), c)
78
+ end
79
+ memory['data'] = YAML.dump(nbayes)
80
+ if interpolated['propagate_training_events'] = "true"
81
+ create_event payload: event.payload
82
+ end
83
+ end
84
+ else
85
+ result = nbayes.classify(event.payload['nb_content'].split(/\s+/))
86
+ if interpolated['min_value'].to_f == 1
87
+ result.max_class
88
+ else
89
+ result.each do |cat, val|
90
+ if val > interpolated['min_value'].to_f
91
+ event.payload['nb_cats'] << (event.payload['nb_cats'].length == 0 ? cat : " "+cat)
92
+ end
93
+ end
94
+ end
95
+ create_event payload: event.payload
96
+ end
97
+ end
98
+ end
99
+
100
+ def load(dat)
101
+ if dat.nil?
102
+ nbayes = NBayes::Base.new
103
+ else
104
+ nbayes = self.class.from_yml(dat)
105
+ end
106
+ nbayes
107
+ end
108
+
109
+
110
+ def self.from_yml(yml_data)
111
+ nbayes = YAML.load(yml_data)
112
+ nbayes.reset_after_import() # yaml does not properly set the defaults on the Hashes
113
+ nbayes
114
+ end
115
+
116
+ # def purge_less_than(x)
117
+ # remove_list = {}
118
+ # nbayes = load(memory['data'])
119
+ # nbayes.vocab.each do |token|
120
+ # if nbayes.data.purge_less_than(token, x)
121
+ # # print "removing #{token}\n"
122
+ # remove_list[token] = 1
123
+ # end
124
+ # end # each vocab word
125
+ # remove_list.keys.each {|token| nbayes.vocab.delete(token) }
126
+ # memory['data'] = YAML.dump(nbayes)
127
+ # end
128
+
129
+ # Delete an entire category from the classification data
130
+ # def delete_category(category)
131
+ # nbayes = load(memory['data'])
132
+ # nbayes.data.delete_category(category)
133
+ # memory['data'] = YAML.dump(nbayes)
134
+ # end
135
+ end
136
+ end
@@ -1,4 +1,4 @@
1
- require "huginn_naive_bayes_agent/version"
2
1
  require 'huginn_agent'
3
2
 
3
+ #HuginnAgent.load 'huginn_naive_bayes_agent/concerns/my_agent_concern'
4
4
  HuginnAgent.register 'huginn_naive_bayes_agent/naive_bayes_agent'
@@ -0,0 +1,13 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::NaiveBayesAgent do
5
+ # before(:each) do
6
+ # @valid_options = Agents::NaiveBayesAgent.new.default_options
7
+ # @checker = Agents::NaiveBayesAgent.new(:name => "NaiveBayesAgent", :options => @valid_options)
8
+ # @checker.user = users(:bob)
9
+ # @checker.save!
10
+ # end
11
+
12
+ # pending "add specs here"
13
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: huginn_naive_bayes_agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.04
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Noah Greenstein
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-16 00:00:00.000000000 Z
11
+ date: 2017-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.13'
19
+ version: '1.7'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.13'
26
+ version: '1.7'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -39,21 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rspec
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '3.0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '3.0'
55
- - !ruby/object:Gem::Dependency
56
- name: nbayes
42
+ name: huginn_agent
57
43
  requirement: !ruby/object:Gem::Requirement
58
44
  requirements:
59
45
  - - ">="
@@ -67,7 +53,7 @@ dependencies:
67
53
  - !ruby/object:Gem::Version
68
54
  version: '0'
69
55
  - !ruby/object:Gem::Dependency
70
- name: huginn_agent
56
+ name: nbayes
71
57
  requirement: !ruby/object:Gem::Requirement
72
58
  requirements:
73
59
  - - ">="
@@ -84,23 +70,15 @@ description: The Huginn Naive Bayes agent uses some incoming Events as a trainin
84
70
  set for Naive Bayes Machine Learning. Then it classifies Events from other sources
85
71
  accordingly using tags. Acts as a Huginn Agent front end to the NBayes gem (https://github.com/oasic/nbayes).
86
72
  email:
87
- - ng03@noahgreenstein.com
73
+ - nogre1@noahgreenstein.com
88
74
  executables: []
89
75
  extensions: []
90
76
  extra_rdoc_files: []
91
77
  files:
92
- - ".gitignore"
93
- - ".rspec"
94
- - ".travis.yml"
95
- - Gemfile
96
78
  - LICENSE.txt
97
- - README.md
98
- - Rakefile
99
- - bin/console
100
- - bin/setup
101
- - huginn_naive_bayes_agent.gemspec
102
79
  - lib/huginn_naive_bayes_agent.rb
103
- - lib/huginn_naive_bayes_agent/version.rb
80
+ - lib/huginn_naive_bayes_agent/naive_bayes_agent.rb
81
+ - spec/naive_bayes_agent_spec.rb
104
82
  homepage: https://github.com/nogre/huginn_naive_bayes_agent
105
83
  licenses:
106
84
  - MIT
@@ -121,8 +99,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
99
  version: '0'
122
100
  requirements: []
123
101
  rubyforge_project:
124
- rubygems_version: 2.5.1
102
+ rubygems_version: 2.4.5.1
125
103
  signing_key:
126
104
  specification_version: 4
127
105
  summary: Naive Bayes Agent for Huginn.
128
- test_files: []
106
+ test_files:
107
+ - spec/naive_bayes_agent_spec.rb
data/.gitignore DELETED
@@ -1,9 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
data/.rspec DELETED
@@ -1,2 +0,0 @@
1
- --format documentation
2
- --color
data/.travis.yml DELETED
@@ -1,5 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- rvm:
4
- - 2.3.1
5
- before_install: gem install bundler -v 1.13.6
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in huginn_naive_bayes_agent.gemspec
4
- gemspec
data/README.md DELETED
@@ -1,41 +0,0 @@
1
- # HuginnNaiveBayesAgent
2
-
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/huginn_naive_bayes_agent`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
- ## Installation
8
-
9
- Add this line to your application's Gemfile:
10
-
11
- ```ruby
12
- gem 'huginn_naive_bayes_agent'
13
- ```
14
-
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install huginn_naive_bayes_agent
22
-
23
- ## Usage
24
-
25
- TODO: Write usage instructions here
26
-
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
-
33
- ## Contributing
34
-
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/huginn_naive_bayes_agent.
36
-
37
-
38
- ## License
39
-
40
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
-
data/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task :default => :spec
data/bin/console DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "huginn_naive_bayes_agent"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -1,39 +0,0 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'huginn_naive_bayes_agent/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "huginn_naive_bayes_agent"
8
- spec.version = HuginnNaiveBayesAgent::VERSION
9
- spec.authors = ["Noah Greenstein"]
10
- spec.email = ["ng03@noahgreenstein.com"]
11
-
12
- spec.summary = %q{Naive Bayes Agent for Huginn.}
13
- spec.description = %q{The Huginn Naive Bayes agent uses some incoming Events as a training set for Naive Bayes Machine Learning. Then it classifies Events from other sources accordingly using tags. Acts as a Huginn Agent front end to the NBayes gem (https://github.com/oasic/nbayes).}
14
- spec.homepage = "https://github.com/nogre/huginn_naive_bayes_agent"
15
- spec.license = "MIT"
16
-
17
- # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
- # to allow pushing to a single host or delete this section to allow pushing to any host.
19
- #if spec.respond_to?(:metadata)
20
- # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
- #else
22
- # raise "RubyGems 2.0 or newer is required to protect against " \
23
- # "public gem pushes."
24
- #end
25
-
26
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
- f.match(%r{^(test|spec|features)/})
28
- end
29
- spec.bindir = "exe"
30
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
- spec.require_paths = ["lib"]
32
-
33
- spec.add_development_dependency "bundler", "~> 1.13"
34
- spec.add_development_dependency "rake", "~> 10.0"
35
- spec.add_development_dependency "rspec", "~> 3.0"
36
-
37
- spec.add_runtime_dependency "nbayes"
38
- spec.add_runtime_dependency "huginn_agent"
39
- end
@@ -1,3 +0,0 @@
1
- module HuginnNaiveBayesAgent
2
- VERSION = "0.0.04"
3
- end