rrbayes 0.0.0 → 0.0.1
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.
- data/VERSION +1 -1
- data/lib/rrbayes/rrbayes.rb +15 -2
- data/rrbayes.gemspec +1 -1
- data/test/rrbayes_test.rb +6 -0
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.1
|
data/lib/rrbayes/rrbayes.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
module Rrbayes
|
2
2
|
|
3
|
+
class LoadingError < ArgumentError
|
4
|
+
end
|
5
|
+
|
3
6
|
class Classifier
|
4
7
|
|
5
8
|
attr_reader :categories, :db
|
@@ -11,10 +14,9 @@ module Rrbayes
|
|
11
14
|
# Rrbayes.new(:categories => %w(spam ham), {:host => '127.0.0.1'})
|
12
15
|
#
|
13
16
|
def initialize(options = {}, redis_options = {})
|
14
|
-
raise "No categories specified for the classifier" unless options[:categories] || load_categories
|
15
17
|
@db = Redis.new(redis_options)
|
16
18
|
@db.connect_to_server
|
17
|
-
@categories = options
|
19
|
+
@categories = find_categories(options).map { |c| Category.new(c, self) }
|
18
20
|
end
|
19
21
|
|
20
22
|
#given a frequency hash and a category, stores teh frequency data
|
@@ -49,6 +51,17 @@ module Rrbayes
|
|
49
51
|
@db.set_members('categories')
|
50
52
|
end
|
51
53
|
|
54
|
+
def find_categories(options)
|
55
|
+
categories = load_categories
|
56
|
+
if categories.empty?
|
57
|
+
categories = options[:categories]
|
58
|
+
raise LoadingError, "No categories specified for the classifier" unless categories
|
59
|
+
elsif options[:categories] && options[:categories].sort != categories.sort
|
60
|
+
raise LoadingError, "you specified categories #{options[:as].inspect} but #{categories.inspect} were found in the db"
|
61
|
+
end
|
62
|
+
categories
|
63
|
+
end
|
64
|
+
|
52
65
|
end
|
53
66
|
|
54
67
|
end
|
data/rrbayes.gemspec
CHANGED
data/test/rrbayes_test.rb
CHANGED
@@ -15,6 +15,12 @@ context "a new classifier" do
|
|
15
15
|
|
16
16
|
should("load categories") { topic.send(:load_categories) }.equals %w(spam ham)
|
17
17
|
|
18
|
+
should("initialize with no categories") { Classifier.new.categories.map { |c| c.name } }.equals %w(spam ham)
|
19
|
+
|
20
|
+
should("initialize with same categories") { Classifier.new(:categories => %w(spam ham)).categories.map { |c| c.name } }.equals %w(spam ham)
|
21
|
+
|
22
|
+
should("detect category mismatch") { Classifier.new({:categories => %w(bad good)}) }.raises(LoadingError)
|
23
|
+
|
18
24
|
context "in training" do
|
19
25
|
|
20
26
|
setup do
|