nb 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +8 -1
- data/lib/nb/naive_bayes.rb +16 -2
- data/lib/nb/version.rb +1 -1
- data/spec/nb/naive_bayes_spec.rb +2 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d55c868cf9d3df0866130593f9e7d78935d9fde
|
4
|
+
data.tar.gz: 5e9cd9507bdea2a306e3a8fa9423153d0d4cfbb5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 299259d6e2322cfe07adca36f84899796632bbc7f36c407de3a6e28d763355ba7518cabc82c7f3e8339c8783fa9937cb14edfc062d50946832c0d112c72d4c44
|
7
|
+
data.tar.gz: 243a9a559861ea695f66f21fe6b34bd5ecc89809b819306c1a9833f08bf51cad1131846180fa6dc55aaad404dc97a13b708ce281b9f95c499691da09e65a6e75
|
data/README.md
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
[](https://codeclimate.com/github/forresty/nb)
|
4
4
|
[](https://travis-ci.org/forresty/nb)
|
5
|
+
[](http://badge.fury.io/rb/nb)
|
5
6
|
|
6
7
|
yet another Naive Bayes library
|
7
8
|
|
@@ -60,9 +61,15 @@ bayes.classify(*%w{ love }).should == [:love, 0.5]
|
|
60
61
|
+------------+------+--------------------+
|
61
62
|
```
|
62
63
|
|
64
|
+
## Credits
|
65
|
+
|
66
|
+
- [classifier gem](https://github.com/cardmagic/classifier)
|
67
|
+
- [naive_bayes gem](https://github.com/reddavis/Naive-Bayes)
|
68
|
+
- [nbayes gem](https://github.com/oasic/nbayes)
|
69
|
+
|
63
70
|
## Contributing
|
64
71
|
|
65
|
-
1. Fork it ( https://github.com/
|
72
|
+
1. Fork it ( https://github.com/forresty/nb/fork )
|
66
73
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
67
74
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
68
75
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/lib/nb/naive_bayes.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
require "yaml"
|
2
2
|
|
3
3
|
class NaiveBayes
|
4
|
-
attr_accessor :categories, :tokens_count, :categories_count
|
4
|
+
attr_accessor :categories, :tokens_count, :categories_count, :default_category
|
5
5
|
|
6
6
|
def initialize(*categories)
|
7
7
|
@categories = categories
|
8
8
|
@tokens_count = {}
|
9
9
|
@categories_count = {}
|
10
|
+
@default_category = @categories.first
|
10
11
|
|
11
12
|
categories.each do |category|
|
12
13
|
@tokens_count[category] = Hash.new(0)
|
@@ -21,8 +22,21 @@ class NaiveBayes
|
|
21
22
|
@categories_count[category] += 1
|
22
23
|
end
|
23
24
|
|
25
|
+
def untrain(category, *tokens)
|
26
|
+
tokens.uniq.each do |token|
|
27
|
+
@tokens_count[category][token] -= 1
|
28
|
+
end
|
29
|
+
@categories_count[category] -= 1
|
30
|
+
end
|
31
|
+
|
24
32
|
def classify(*tokens)
|
25
|
-
classifications(*tokens).first
|
33
|
+
result = classifications(*tokens).first
|
34
|
+
|
35
|
+
if result.last == 0.0
|
36
|
+
[@default_category, 0.0]
|
37
|
+
else
|
38
|
+
result
|
39
|
+
end
|
26
40
|
end
|
27
41
|
|
28
42
|
def classifications(*tokens)
|
data/lib/nb/version.rb
CHANGED
data/spec/nb/naive_bayes_spec.rb
CHANGED
@@ -2,6 +2,7 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe NaiveBayes do
|
4
4
|
it { should respond_to :train }
|
5
|
+
it { should respond_to :untrain }
|
5
6
|
it { should respond_to :save }
|
6
7
|
it { should respond_to :classify }
|
7
8
|
it { should respond_to :classifications }
|
@@ -12,6 +13,7 @@ describe NaiveBayes do
|
|
12
13
|
# it { should respond_to :total_number_of_tokens }
|
13
14
|
it { should respond_to :total_number_of_items }
|
14
15
|
it { should respond_to :top_tokens_of_category }
|
16
|
+
it { should respond_to :default_category= }
|
15
17
|
|
16
18
|
let(:bayes) { NaiveBayes.new(:love, :hate) }
|
17
19
|
subject { bayes }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Forrest Ye
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|