Skip to content

Instantly share code, notes, and snippets.

@lutzky
Last active August 29, 2015 13:59
Show Gist options
  • Save lutzky/10907342 to your computer and use it in GitHub Desktop.
Save lutzky/10907342 to your computer and use it in GitHub Desktop.
Unit test for add-one Laplace smoothing
def test_p_word(self):
e = Evaluator()
# Add negative reviews
e.add_review(False, {"horrible", "crappy", "boring"})
e.add_review(False, {"awfully", "mediocre"})
e.add_review(False, {"terrible", "boring"})
# Add positive reviews
e.add_review(True, {"wonderful", "delightful", "awesome"})
e.add_review(True, {"awesome", "awfully", "thrilling"})
# e.p_word(word, category) should return smoothed P(word | category)
self.assertAlmostEquals(e.p_word("unbelievable", True), 1/4)
self.assertAlmostEquals(e.p_word("awesome", True), 3/4)
self.assertAlmostEquals(e.p_word("boring", False), 3/5)
self.assertAlmostEquals(e.p_word("heinous", False), 1/5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment