Beispiel #1
0
class BayesianFilterStrategyTestCase(unittest.TestCase):

    def setUp(self):
        self.env = EnvironmentStub(enable=[BayesianFilterStrategy])
        self.env.config.set('spam-filter', 'bayes_karma', '10')
        FilterSystem(self.env).upgrade_environment()

        self.strategy = BayesianFilterStrategy(self.env)

    def tearDown(self):
        reset_db(self.env)

    def test_karma_calculation_unsure(self):
        bayes.Hammie = lambda x: Mock(score=lambda x: .5,
                                      bayes=Mock(nham=1000, nspam=1000))

        req = MockRequest(self.env)
        self.assertEquals(None, self.strategy.test(req, 'John Doe', 'Spam',
                                                   '127.0.0.1'))

    def test_karma_calculation_negative(self):
        bayes.Hammie = lambda x: Mock(score=lambda x: .75,
                                      bayes=Mock(nham=1000, nspam=1000))

        req = MockRequest(self.env)
        points, reasons, args = \
            self.strategy.test(req, 'John Doe', 'Spam', '127.0.0.1')
        self.assertEquals(-5, points)

    def test_karma_calculation_positive(self):
        bayes.Hammie = lambda x: Mock(score=lambda x: .25,
                                      bayes=Mock(nham=1000, nspam=1000))

        req = MockRequest(self.env)
        points, reasons, args = \
            self.strategy.test(req, 'John Doe', 'Spam', '127.0.0.1')
        self.assertEquals(5, points)

    def test_classifier_untrained(self):
        req = MockRequest(self.env)
        self.assertEqual(None, self.strategy.test(req, 'John Doe', 'Hammie',
                                                  '127.0.0.1'))

    def test_classifier_basics(self):
        req = MockRequest(self.env)
        self.env.config.set('spam-filter', 'bayes_min_training', '1')
        self.strategy.train(req, 'John Doe', 'Spam spam spammie', '127.0.0.1',
                            True)
        self.strategy.train(req, 'John Doe', 'Ham ham hammie', '127.0.0.1',
                            False)

        points, reasons, args = \
            self.strategy.test(req, 'John Doe', 'Hammie', '127.0.0.1')
        self.assertGreater(points, 0, 'Expected positive karma')
        points, reasons, args = \
            self.strategy.test(req, 'John Doe', 'Spam', '127.0.0.1')
        self.assertLess(points, 0, 'Expected negative karma')
Beispiel #2
0
class BayesianFilterStrategyTestCase(unittest.TestCase):

    def setUp(self):
        self.env = EnvironmentStub(enable=[BayesianFilterStrategy])
        self.env.config.set('spam-filter', 'bayes_karma', '10')
        db = self.env.get_db_cnx()
        cursor = db.cursor()
        for table in schema:
            for stmt in _to_sql(table):
                cursor.execute(stmt)

        self.strategy = BayesianFilterStrategy(self.env)

    def test_karma_calculation_unsure(self):
        bayes.Hammie = lambda x: Mock(score=lambda x: .5,
                                      bayes=Mock(nham=1000, nspam=1000))

        req = Mock(authname='anonymous', base_url='http://example.org/',
                   remote_addr='127.0.0.1')
        self.assertEquals(None, self.strategy.test(req, 'John Doe', 'Spam'))

    def test_karma_calculation_negative(self):
        bayes.Hammie = lambda x: Mock(score=lambda x: .75,
                                      bayes=Mock(nham=1000, nspam=1000))

        req = Mock(authname='anonymous', base_url='http://example.org/',
                   remote_addr='127.0.0.1')
        points, reasons = self.strategy.test(req, 'John Doe', 'Spam')
        self.assertEquals(-5, points)

    def test_karma_calculation_positive(self):
        bayes.Hammie = lambda x: Mock(score=lambda x: .25,
                                      bayes=Mock(nham=1000, nspam=1000))

        req = Mock(authname='anonymous', base_url='http://example.org/',
                   remote_addr='127.0.0.1')
        points, reasons = self.strategy.test(req, 'John Doe', 'Spam')
        self.assertEquals(5, points)

    def test_classifier_untrained(self):
        req = Mock(authname='anonymous', base_url='http://example.org/',
                   remote_addr='127.0.0.1')
        self.assertEqual(None, self.strategy.test(req, 'John Doe', 'Hammie'))

    def test_classifier_basics(self):
        req = Mock(authname='anonymous', base_url='http://example.org/',
                   remote_addr='127.0.0.1')
        self.env.config.set('spam-filter', 'bayes_min_training', '1')
        self.strategy.train(req, 'John Doe', 'Spam spam spammie', True)
        self.strategy.train(req, 'John Doe', 'Ham ham hammie', False)

        points, reasons = self.strategy.test(req, 'John Doe', 'Hammie')
        assert points > 0, 'Expected positive karma'
        points, reasons = self.strategy.test(req, 'John Doe', 'Spam')
        assert points < 0, 'Expected negative karma'
Beispiel #3
0
    def setUp(self):
        self.env = EnvironmentStub(enable=[BayesianFilterStrategy])
        self.env.config.set('spam-filter', 'bayes_karma', '10')
        db = self.env.get_db_cnx()
        cursor = db.cursor()
        for table in schema:
            for stmt in _to_sql(table):
                cursor.execute(stmt)

        self.strategy = BayesianFilterStrategy(self.env)
Beispiel #4
0
    def render_admin_panel(self, req, cat, page, path_info):
        req.perm.assert_permission('SPAM_CONFIG')

        bayes = BayesianFilterStrategy(self.env)
        hammie = bayes._get_hammie()
        data = {}

        if req.method == 'POST':
            if 'train' in req.args:
                bayes.train(None, None, req.args['content'],
                            spam='spam' in req.args['train'].lower())
                req.redirect(req.href.admin(cat, page))

            elif 'test' in req.args:
                data['content'] = req.args['content']
                try:
                    data['score'] = hammie.score(req.args['content'].encode('utf-8'))
                except Exception, e:
                    self.log.warn('Bayes test failed: %s', e, exc_info=True)
                    data['error'] = unicode(e)

            else:
                if 'reset' in req.args:
                    self.log.info('Resetting SpamBayes training database')
                    db = self.env.get_db_cnx()
                    cursor = db.cursor()
                    cursor.execute("DELETE FROM spamfilter_bayes")
                    db.commit()

                try:
                    min_training = int(req.args['min_training'])
                    if min_training != bayes.min_training:
                        self.config.set('spam-filter', 'bayes_min_training',
                                        min_training)
                        self.config.save()
                except ValueError:
                    pass
                req.redirect(req.href.admin(cat, page))
Beispiel #5
0
    def render_admin_panel(self, req, cat, page, path_info):
        req.perm.require('SPAM_CONFIG')

        bayes = BayesianFilterStrategy(self.env)
        hammie = bayes._get_hammie()
        data = {}

        if req.method == 'POST':
            if 'train' in req.args:
                bayes.train(None, None, req.args['bayes_content'], '127.0.0.1',
                            spam='spam' in req.args['train'].lower())
                req.redirect(req.href.admin(cat, page))

            elif 'test' in req.args:
                bayes_content = req.args['bayes_content']
                data['content'] = bayes_content
                try:
                    data['score'] = hammie.score(bayes_content.encode('utf-8'))
                except Exception, e:
                    self.log.warn('Bayes test failed: %s', e, exc_info=True)
                    data['error'] = unicode(e)

            else:
                if 'reset' in req.args:
                    self.log.info('Resetting SpamBayes training database')
                    self.env.db_transaction("DELETE FROM spamfilter_bayes")
                elif 'reduce' in req.args:
                    self.log.info('Reducing SpamBayes training database')
                    bayes.reduce()

                min_training = req.args.as_int('min_training')
                if min_training is not None and \
                        min_training != bayes.min_training:
                    self.config.set('spam-filter', 'bayes_min_training',
                                    min_training)
                    self.config.save()

                min_dbcount = req.args.as_int('min_dbcount')
                if min_dbcount is not None and \
                        min_dbcount != bayes.min_dbcount:
                    self.config.set('spam-filter', 'bayes_min_dbcount',
                                    min_dbcount)
                    self.config.save()

                req.redirect(req.href.admin(cat, page))
Beispiel #6
0
    def setUp(self):
        self.env = EnvironmentStub(enable=[BayesianFilterStrategy])
        self.env.config.set('spam-filter', 'bayes_karma', '10')
        FilterSystem(self.env).upgrade_environment()

        self.strategy = BayesianFilterStrategy(self.env)