Beispiel #1
0
 def __init__(self):
     self.bayes = Bayes()
Beispiel #2
0
class Test:
    def __init__(self):
        self.bayes = Bayes()

    def status(self):
        args = ['', 'status']
        self.bayes.run(args)

    def reset(self):
        args = ['', 'reset']
        self.bayes.run(args)

    def learn(self, dataset, count_spam, count_ham):
        b = self.bayes
        args = ['', 'learn', 'spam', '%s/spam' % dataset, int(count_spam)]
        b.run(args)
        args = ['', 'learn', 'ham', '%s/ham' % dataset, int(count_ham)]
        b.run(args)

    def test(self, dataset, threshold):
            
        b = self.bayes
        
        print '>> Classifying spam ...'
        args = ['./test.py', 'classify', '%s/spam' % dataset, 'spam', 'ham', threshold]
        result = b.run(args)
        tp = result['positive']
        fn = result['count'] - tp

        print '>> Classifying ham ...'
        args = ['./test.py', 'classify', '%s/ham' % dataset, 'spam', 'ham', threshold]
        result = b.run(args)
        fp = result['positive']
        tn = result['count'] - fp
                                    
        precison = 1.0 * tp / (tp + fp)
        recall = 1.0 * tp / (tp + fn)
        fscore = 2/(1/precison + 1/recall)

        fpr = 1.0*fp/(fp+tn)
        tnr = 1 - fpr

        # self.status()
        print "Classifying Results:"
        print "========================================\n"
        print 'POSITIVE: %6d, TP: %6d, FN: %6d'   % (tp + fn, tp, fn)
        print 'NEGATIVE: %6d, TN: %6d, FP: %6d\n' % (tn + fp, tn, fp)
        print 'TPR: %1.4f, FPR: %1.4f, TNR: %1.4f'   % (recall, fpr, tnr)
        print '  R: %1.4f,   P: %1.4f,   F: %1.4f'   % (recall, precison, fscore)
        print "\n========================================\n"

    def tfidf(self, dataset):
            
        b = self.bayes
        
        print '>> TFIDFing spam ...'
        args = ['./test.py', 'tfidf', '%s/spam' % dataset, 'spam', 'ham']
        result = b.run(args)

        print '>> TFIDF ham ...'
        args = ['./test.py', 'tfidf', '%s/ham' % dataset, 'spam', 'ham']
        result = b.run(args)
                                    

    def chi(self, dataset):
            
        b = self.bayes
        
        print '>> TFIDFing spam ...'
        args = ['./test.py', 'chi', '%s/spam' % dataset, 'spam', 'ham']
        result = b.run(args)

        print '>> TFIDF ham ...'
        args = ['./test.py', 'chi', '%s/ham' % dataset, 'spam', 'ham']
        result = b.run(args)