@@ -0,0 +1,2653 @@
+import math%0A%0Aclass BayesianClassifier:%0A    def __init__(self, beta = 0):%0A        # total occurrence of all features%0A        self.total = 0%0A        # Labels and their occurrence%0A        self.labelCount = %7B%7D%0A        # Features and their occurrence%0A        self.featureCount = %7B%7D%0A        # Features' occurrence under certain label%0A        self.labelFeatureCount = %7B%7D%0A        # beta value%0A        self.beta = beta%0A%0A    def train(self, features, labels):%0A        self.total += sum(features.values())%0A        # Update the counting of the attributes under differnt labels%0A        for features, count in features.items():%0A            self._updateCount(%5C%0A                    self.featureCount, features, count)%0A            for label in labels:%0A                self._updateCount(%5C%0A                        self.labelFeatureCount, (label, features), count)%0A                self._updateCount(%5C%0A                        self.labelCount, label, count)%0A%0A    def CLassify(self, features):%0A        scores = sorted(((label, self.getScore(features, label)) for %5C%0A                          label in self.labelCount.keys()), %5C%0A                          key = lambda x: x%5B1%5D,%0A                          reverse = True)%0A%0A        return scores%0A%0A    def getScore(self, features, label):%0A        totalScore = self._getScoreOfOneFeature(self.labelCount%5Blabel%5D, self.total)%0A        fc = len(self.labelCount)%0A        lCount = self.labelCount%5Blabel%5D%0A%0A        for feature, count in features.items():%0A                key = (label, feature)%0A                fCount = 0%0A                if key in self.labelFeatureCount:%0A                    fCount =self.labelFeatureCount%5Bkey%5D%0A%0A                score = self._getScoreOfOneFeature(fCount + self.beta, lCount + fc * self.beta)%0A                totalScore += count * score%0A        return totalScore%0A%0A    def _updateCount(self, d, key, val = 1):%0A        d.setdefault(key, 0)%0A        d%5Bkey%5D += val%0A%0A    def _getScoreOfOneFeature(self, n1, n2):%0A        if n1 == 0:%0A            return 0%0A        return math.log(n1 * 1.0 / n2, 2)%0A%0Aif __name__ == %22__main__%22:%0A    def split(text):%0A        return dict((token, 1) for token in text.split())%0A    classifier = BayesianClassifier(0.5)%0A    classifier.train(split(%22money money is making finance%22), %5B%22finance%22%5D)%0A    classifier.train(split(%22Best coffee shop in Pit? I enjoy days with friends! life is here%22), %5B%22life%22, %22pit%22%5D)%0A    classifier.train(split(%22life life life good%22), %5B%22life%22%5D)%0A    classifier.train(split(%22money doesn't make friend with don't give up life, you%22), %5B%22finance%22, %22life%22%5D)%0A%0A    print classifier.CLassify(split(%22money wall is finance%22))%0A    print classifier.CLassify(split(%22life is good%22))%0A%0A%0A%0A

add naive bayesian classifier