lib/ankusa/naive_bayes.rb in ankusa-0.0.13 vs lib/ankusa/naive_bayes.rb in ankusa-0.0.14

- old
+ new

@@ -11,11 +11,11 @@ # Classes is an array of classes to look at def classifications(text, classnames=nil) result = log_likelihoods text, classnames result.keys.each { |k| - result[k] = (result[k] == INFTY) ? 0 : Math.exp(result[k]) + result[k] = (result[k] == -INFTY) ? 0 : Math.exp(result[k]) } # normalize to get probs sum = result.values.inject { |x,y| x+y } result.keys.each { |k| result[k] = result[k] / sum } @@ -28,11 +28,11 @@ result = Hash.new 0 TextHash.new(text).each { |word, count| probs = get_word_probs(word, classnames) classnames.each { |k| - # log likelihood should be infinity if we've never seen the klass - result[k] += probs[k] > 0 ? (Math.log(probs[k]) * count) : INFTY + # log likelihood should be negative infinity if we've never seen the klass + result[k] += probs[k] > 0 ? (Math.log(probs[k]) * count) : -INFTY } } # add the prior doc_counts = doc_count_totals.select { |k,v| classnames.include? k }.map { |k,v| v }