def testFCount(self): c = Classifier(getwords) c.incf("hello", "Good") c.incf("hello", "Good") c.incf("hello", "Bad") self.assertEqual(c.fcount("hello", "Good"), 2) self.assertEqual(c.fcount("hello", "Bad"), 1) self.assertEqual(c.fcount("wurst", "Bad"), 0)
def testWeightedProb(self): c = Classifier(getwords) c.incc("Good") c.incf("hello", "Good") c.incc("Good") c.incf("world", "Good") c.incc("Good") c.incf("world", "Good") c.incc("Bad") c.incf("world", "Bad") self.assertEqual(c.weightedprob("world", "Good"), 5.0/8.0) self.assertEqual(c.weightedprob("wurst", "Good"), 0.5)
def testIncF(self): c = Classifier(getwords) c.incf("hello", "Good") self.assertEqual(c.fc["hello"]["Good"], 1) c.incf("hello", "Good") self.assertEqual(c.fc["hello"]["Good"], 2) c.incf("hello", "Bad") self.assertEqual(c.fc["hello"]["Bad"], 1)
def testFProb(self): c = Classifier(getwords) c.incc("Good") c.incf("hello", "Good") c.incc("Good") c.incf("world", "Good") c.incc("Good") c.incf("world", "Good") self.assertEqual(c.fprob("world", "Good"), 2.0/3.0)
def testProb(self): c = Classifier(getwords) # training c.incc("Good") c.incf("hello", "Good") c.incc("Good") c.incf("world", "Good") c.incc("Good") c.incf("world", "Good") c.incc("Bad") c.incf("world", "Bad") # classify new document item = "world world wurst Wurst wurst world" self.assertEqual(c.prob(item, "Good"), 0.234375)