naive_bayes 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -43,7 +43,6 @@ class NaiveBayes
43
43
  @features_count[klass][feature] += 1
44
44
  end
45
45
  @klass_count[klass] += 1
46
- save
47
46
  end
48
47
 
49
48
  #P(Class | Item) = P(Item | Class) * P(Class)
@@ -55,28 +54,26 @@ class NaiveBayes
55
54
  scores.sort {|a,b| b[1] <=> a[1]}[0]
56
55
  end
57
56
 
58
- private
59
-
60
57
  def save
61
- if @db_filepath
62
- File.open(@db_filepath, "w+") do |f|
63
- f.write(Marshal.dump(self))
64
- end
58
+ raise "You haven't set a db_filpath, I dont know where to save" if @db_filepath.nil?
59
+ File.open(@db_filepath, "w+") do |f|
60
+ f.write(Marshal.dump(self))
65
61
  end
66
62
  end
67
63
 
64
+ private
65
+
68
66
  # P(Item | Class)
69
67
  def prob_of_item_given_a_class(features, klass)
70
68
  a = features.inject(1.0) do |sum, feature|
71
69
  prob = prob_of_feature_given_a_class(feature, klass)
72
- prob = assumed_probability if prob == 0
73
70
  sum *= prob
74
71
  end
75
72
  end
76
73
 
77
74
  # P(Feature | Class)
78
75
  def prob_of_feature_given_a_class(feature, klass)
79
- return 0.5 if @klass_count[klass] == 0
76
+ return assumed_probability if @klass_count[klass] == 0
80
77
  @features_count[klass][feature] / @klass_count[klass]
81
78
  end
82
79
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{naive_bayes}
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["reddavis"]
12
- s.date = %q{2010-01-05}
12
+ s.date = %q{2010-03-11}
13
13
  s.description = %q{Simple straight forward Naive Bayes classifier implementation}
14
14
  s.email = %q{reddavis@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -1 +1 @@
1
- o:NaiveBayes :@klass_count{: spamf1:hamf1:@db_filepath"D/Users/reddavis/Documents/projects/naive_bayes/spec/db/naive.nb:@features_count{;}" wordf1"badf1f0;}"wef1"badf1@:
1
+ o:NaiveBayes :@features_count{: spam}I"bad:
@@ -14,16 +14,26 @@ describe "NaiveBayes" do
14
14
  end
15
15
  end
16
16
 
17
- describe "Save" do
18
- before do
19
- @classifier = NaiveBayes.new(:spam, :ham)
20
- @classifier.db_filepath = db_filepath
17
+ describe "Saving the NB" do
18
+ describe "DB filepath has been set" do
19
+ before do
20
+ @classifier = NaiveBayes.new(:spam, :ham)
21
+ @classifier.db_filepath = db_filepath
22
+ end
23
+
24
+ it "should save to the filepath provided" do
25
+ FileUtils.rm(db_filepath, :force => true)
26
+ @classifier.save
27
+ File.exists?(db_filepath).should be_true
28
+ end
21
29
  end
22
30
 
23
- it "should save to the filepath provided" do
24
- FileUtils.rm(db_filepath, :force => true)
25
- @classifier.train(:spam, 'bad')
26
- File.exists?(db_filepath).should be_true
31
+ describe "DB filepath has no been set" do
32
+ it "should raise an error" do
33
+ lambda do
34
+ NaiveBayes.new(:spam, :ham).save
35
+ end.should raise_error
36
+ end
27
37
  end
28
38
  end
29
39
 
@@ -33,6 +43,7 @@ describe "NaiveBayes" do
33
43
  classifier.db_filepath = db_filepath
34
44
  classifier.train(:spam, 'bad', 'word')
35
45
  classifier.train(:ham, 'we', 'bad')
46
+ classifier.save
36
47
  end
37
48
 
38
49
  it "should return 0.5" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: naive_bayes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - reddavis
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-05 00:00:00 +00:00
12
+ date: 2010-03-11 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency