Skip to content

Commit 17143ab

Browse files
authored
Merge pull request #39 from cardmagic/add-remove_category
Add remove_category functionality to Bayes classifier
2 parents a537321 + ea32aea commit 17143ab

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

classifier.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'classifier'
3-
s.version = '1.4.0'
3+
s.version = '1.4.1'
44
s.summary = 'A general classifier module to allow Bayesian and other types of classifications.'
55
s.description = 'A general classifier module to allow Bayesian and other types of classifications.'
66
s.author = 'Lucas Carlson'

lib/classifier/bayes.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,23 @@ def add_category(category)
139139
end
140140

141141
alias append_category add_category
142+
143+
#
144+
# Allows you to remove categories from the classifier.
145+
# For example:
146+
# b.remove_category "Spam"
147+
#
148+
# WARNING: Removing categories from a trained classifier will
149+
# result in the loss of all training data for that category.
150+
# Make sure you really want to do this before calling this method.
151+
def remove_category(category)
152+
category = category.prepare_category_name
153+
raise StandardError, "No such category: #{category}" unless @categories.key?(category)
154+
155+
@categories.delete(category)
156+
@category_counts.delete(category)
157+
@category_word_count.delete(category)
158+
@total_words -= @category_word_count[category].to_i
159+
end
142160
end
143161
end

test/bayes/bayesian_test.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,56 @@ def test_safari_animals
4242
assert_equal 'Lion', bayes.classify('lion')
4343
assert_equal 'Elephant', bayes.classify('elephant')
4444
end
45+
46+
def test_remove_category
47+
@classifier.train_interesting 'This is interesting content'
48+
@classifier.train_uninteresting 'This is uninteresting content'
49+
50+
assert_equal %w[Interesting Uninteresting].sort, @classifier.categories.sort
51+
52+
@classifier.remove_category 'Uninteresting'
53+
54+
assert_equal ['Interesting'], @classifier.categories
55+
end
56+
57+
def test_remove_nonexistent_category
58+
assert_raises(StandardError) do
59+
@classifier.remove_category 'NonexistentCategory'
60+
end
61+
end
62+
63+
def test_remove_category_affects_classification
64+
@classifier.train_interesting 'This is interesting content'
65+
@classifier.train_uninteresting 'This is uninteresting content'
66+
67+
assert_equal 'Uninteresting', @classifier.classify('This is uninteresting')
68+
69+
@classifier.remove_category 'Uninteresting'
70+
71+
assert_equal 'Interesting', @classifier.classify('This is uninteresting')
72+
end
73+
74+
def test_remove_all_categories
75+
@classifier.remove_category 'Interesting'
76+
@classifier.remove_category 'Uninteresting'
77+
78+
assert_empty @classifier.categories
79+
end
80+
81+
def test_remove_and_add_category
82+
@classifier.remove_category 'Uninteresting'
83+
@classifier.add_category 'Neutral'
84+
85+
assert_equal %w[Interesting Neutral].sort, @classifier.categories.sort
86+
end
87+
88+
def test_remove_category_preserves_other_category_data
89+
@classifier.train_interesting 'This is interesting content'
90+
@classifier.train_uninteresting 'This is uninteresting content'
91+
92+
interesting_classification = @classifier.classify('This is interesting')
93+
@classifier.remove_category 'Uninteresting'
94+
95+
assert_equal interesting_classification, @classifier.classify('This is interesting')
96+
end
4597
end

0 commit comments

Comments
 (0)