Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't train or untrain empty word hashes #132

Merged
merged 4 commits into from
Jan 17, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions lib/classifier-reborn/bayes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def initialize(*args)
# b.train "that", "That text"
# b.train "The other", "The other text"
def train(category, text)
word_hash = Hasher.word_hash(text, @language, @enable_stemmer)
return if word_hash.empty?
category = CategoryNamer.prepare_name(category)

# Add the category dynamically or raise an error
Expand All @@ -73,7 +75,7 @@ def train(category, text)

@backend.update_category_training_count(category, 1)
@backend.update_total_trainings(1)
Hasher.word_hash(text, @language, @enable_stemmer).each do |word, count|
word_hash.each do |word, count|
@backend.update_category_word_frequency(category, word, count)
@backend.update_category_word_count(category, count)
@backend.update_total_words(count)
Expand All @@ -88,10 +90,12 @@ def train(category, text)
# b.train :this, "This text"
# b.untrain :this, "This text"
def untrain(category, text)
word_hash = Hasher.word_hash(text, @language, @enable_stemmer)
return if word_hash.empty?
category = CategoryNamer.prepare_name(category)
@backend.update_category_training_count(category, -1)
@backend.update_total_trainings(-1)
Hasher.word_hash(text, @language, @enable_stemmer).each do |word, count|
word_hash.each do |word, count|
next if @backend.total_words < 0
orig = @backend.category_word_frequency(category, word) || 0
@backend.update_category_word_frequency(category, word, -count)
Expand All @@ -112,18 +116,25 @@ def untrain(category, text)
def classifications(text)
score = {}
word_hash = Hasher.word_hash(text, @language, @enable_stemmer)
category_keys.each do |category|
score[category.to_s] = 0
total = (@backend.category_word_count(category) || 1).to_f
word_hash.each do |word, _count|
s = @backend.word_in_category?(category, word) ? @backend.category_word_frequency(category, word) : 0.1
score[category.to_s] += Math.log(s / total)
if word_hash.empty?
category_keys.each do |category|
score[category.to_s] = Float::INFINITY
end
score
else
category_keys.each do |category|
score[category.to_s] = 0
total = (@backend.category_word_count(category) || 1).to_f
word_hash.each do |word, _count|
s = @backend.word_in_category?(category, word) ? @backend.category_word_frequency(category, word) : 0.1
score[category.to_s] += Math.log(s / total)
end
# now add prior probability for the category
s = @backend.category_has_trainings?(category) ? @backend.category_training_count(category) : 0.1
score[category.to_s] += Math.log(s / @backend.total_trainings.to_f)
end
# now add prior probability for the category
s = @backend.category_has_trainings?(category) ? @backend.category_training_count(category) : 0.1
score[category.to_s] += Math.log(s / @backend.total_trainings.to_f)
score
Copy link
Contributor

@ibnesayeed ibnesayeed Jan 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you did not used return keyword in the if section of the word_hash.empty? clause and wrapped the remaining code in the else clause, I would encourage to remove implicit return of score from both the clauses and return it outside the conditional as the last line.

Alternatively, explicit return in the special case and not wrapping rest of the code in else clause would reduce a level of nesting, while gaining the same effect.

end
score
end

# Returns the classification of the provided +text+, which is one of the
Expand Down
2 changes: 1 addition & 1 deletion lib/classifier-reborn/extensions/hasher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def word_hash(str, language = 'en', enable_stemmer = true)

# Return a word hash without extra punctuation or short symbols, just stemmed words
def clean_word_hash(str, language = 'en', enable_stemmer = true)
word_hash_for_words str.gsub(/[^\p{WORD}\s]/, '').downcase.split, language, enable_stemmer
word_hash_for_words(str.gsub(/[^\p{WORD}\s]/, '').downcase.split, language, enable_stemmer)
end

def word_hash_for_words(words, language = 'en', enable_stemmer = true)
Expand Down