bayes_naive_jdp 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,14 @@
1
1
  # BayesNaiveJdp
2
2
 
3
- TODO: Write a gem description
3
+ This is a very simple Naive Bayesian classifier, build just for fun.
4
+
5
+ I decided to use it as an example while learning to package ruby code.
6
+
7
+ The algorithm used here is not original, but an adaptation from Burak Kanber's
8
+ [Machine Learning in Javascript](http://readable.cc/feed/view/34236/burak-kanber-s-blog) series.
9
+
10
+
11
+
4
12
 
5
13
  ## Installation
6
14
 
@@ -18,12 +26,25 @@ Or install it yourself as:
18
26
 
19
27
  ## Usage
20
28
 
21
- TODO: Write usage instructions here
22
-
23
- ## Contributing
29
+ # create an instance of the Classifier
30
+ classifier = BayesNaiveJdp::Classifier.new
31
+
32
+ # Train the Classifier using many samples from each valid category
33
+ # documents are strings, categories are strings, symbols, or integers
34
+ classifier.train(document_a_1, category_a)
35
+ classifier.train(document_a_2, category_a)
36
+ classifier.train(document_a_3, category_a)
37
+ ..
38
+ classifier.train(document_b_1, category_b)
39
+ classifier.train(document_b_2, category_b)
40
+ classifier.train(document_b_3, category_b)
41
+ ..
42
+
43
+ # use the classifier to categorize an unknown document
44
+ results = classifier.classify(mystery_document)
45
+ category = results[:winner][:classification]
46
+ confidence = results[:winner][:confidence]
47
+
48
+ # also, results[:all_scores] is a hash
49
+ # with categories as indices and confidence levels as values
24
50
 
25
- 1. Fork it
26
- 2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Add some feature'`)
28
- 4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create new Pull Request
@@ -1,3 +1,3 @@
1
1
  module BayesNaiveJdp
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,40 @@
1
+ require 'pathname'
2
+ require 'test/unit'
3
+
4
+ dir = Pathname.new File.expand_path(File.dirname(__FILE__))
5
+ require dir + '..' + 'lib' + 'bayes_naive_jdp'
6
+
7
+ DOCUMENT_PATH = dir + 'documents'
8
+ CONFIDENCE_MIN = 0.98
9
+
10
+ class MiscTest < Test::Unit::TestCase
11
+ def test_integer_categories
12
+ categories = [1, 2, 3]
13
+ classifier = BayesNaiveJdp::Classifier.new
14
+ categories.each do |cat|
15
+ 4.times do |i|
16
+ classifier.train("#{cat} " * i, cat)
17
+ end
18
+ end
19
+
20
+ categories.each do |cat|
21
+ answer = classifier.classify("#{cat} " * 5)
22
+ assert_equal(answer[:winner][:classification], cat)
23
+ end
24
+ end
25
+
26
+ def test_symbol_categories_
27
+ categories = [:one, :two, :three]
28
+ classifier = BayesNaiveJdp::Classifier.new
29
+ categories.each do |cat|
30
+ 4.times do |i|
31
+ classifier.train("#{cat.to_s} " * i, cat)
32
+ end
33
+ end
34
+
35
+ categories.each do |cat|
36
+ answer = classifier.classify("#{cat.to_s} " * 5)
37
+ assert_equal(answer[:winner][:classification], cat)
38
+ end
39
+ end
40
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bayes_naive_jdp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -77,6 +77,7 @@ files:
77
77
  - test/documents/schneier-4
78
78
  - test/documents/schneier-5
79
79
  - test/test_author_classification.rb
80
+ - test/test_misc.rb
80
81
  homepage: ''
81
82
  licenses:
82
83
  - MIT
@@ -92,7 +93,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
92
93
  version: '0'
93
94
  segments:
94
95
  - 0
95
- hash: 3893581627066365178
96
+ hash: -4046118477202807926
96
97
  required_rubygems_version: !ruby/object:Gem::Requirement
97
98
  none: false
98
99
  requirements:
@@ -101,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
102
  version: '0'
102
103
  segments:
103
104
  - 0
104
- hash: 3893581627066365178
105
+ hash: -4046118477202807926
105
106
  requirements: []
106
107
  rubyforge_project:
107
108
  rubygems_version: 1.8.25
@@ -125,3 +126,4 @@ test_files:
125
126
  - test/documents/schneier-4
126
127
  - test/documents/schneier-5
127
128
  - test/test_author_classification.rb
129
+ - test/test_misc.rb