Edgewall Software

Changeset 17736

Timestamp:
Oct 28, 2023, 11:29:42 PM (9 months ago)
Author:
Dirk Stöcker
Message:

remove usage of two intermediate classes for spambayes, create proper interface for admin.py, include the few spambayes classes we need directly from the inofficial incomplete python3 port at https://github.com/mpwillson/spambayes3

Location:
plugins/trunk/spam-filter
Files:
9 added
4 edited

Legend:

Unmodified
Added
Removed
  • plugins/trunk/spam-filter/setup.py

    r17735 r17736  
    5252    extras_require={
    5353        'dns': ['dnspython>=1.3.5'],
    54         'spambayes': ['spambayes'],
     54        'spambayes': ['spambayes'],
    5555        'pillow': ['pillow>=2'],
    5656        'account': ['TracAccountManager >= 0.4'],
  • plugins/trunk/spam-filter/tracspamfilter/admin.py

    r17729 r17736  
    446446
    447447        bayes = BayesianFilterStrategy(self.env)
    448         hammie = bayes._get_hammie()
    449448        data = {}
    450449
     
    459458                data['content'] = bayes_content
    460459                try:
    461                     data['score'] = hammie.score(bayes_content.encode('utf-8'))
     460                    data['score'] = )
    462461                except Exception as e:
    463462                    self.log.warning('Bayes test failed: %s', e, exc_info=True)
     
    488487                req.redirect(req.href.admin(cat, page))
    489488        ratio = ''
    490         nspam = hammie.bayes.nspam
    491         nham = hammie.bayes.nham
     489        nspam, nham = bayes.getnumbers()
    492490        if nham and nspam:
    493491            if nspam > nham:
  • plugins/trunk/spam-filter/tracspamfilter/filters/bayes.py

    r17735 r17736  
    2222from tracspamfilter.api import IFilterStrategy, N_
    2323
    24 from spambayes.hammie import Hammie
    25 from spambayes.storage import SQLClassifier
    26 
     24from spambayes.classifier import Classifier
     25from spambayes.tokenizer import tokenize
    2726
    2827class BayesianFilterStrategy(Component):
     
    5150
    5251    def test(self, req, author, content, ip):
    53         hammie = self._get_hammie()
    54         nspam = hammie.bayes.nspam
    55         nham = hammie.bayes.nham
     52        ()
     53        nspam = bayes.nspam
     54        nham = bayes.nham
    5655        if author is not None:
    5756            testcontent = author + '\n' + content
     
    7170                          "large, results may be bad.")
    7271
    73         score = hammie.score(testcontent)
     72        score = )
    7473        self.log.debug("SpamBayes reported spam probability of %s", score)
    7574        points = -int(round(self.karma_points * (score * 2 - 1)))
     
    7978                    ("%3.2f" % (score * 100)))
    8079
     80
     81
     82
     83
     84
     85
     86
     87
    8188    def train(self, req, author, content, ip, spam=True):
    82         # Split tokens at null characters by tokenizer in spambayes.hammie
     89        # Split tokens at null characters by tokenizer
    8390        content = content.replace('\x00', ' ')
    8491        if author is not None:
     
    8996                      spam and "spam" or "ham")
    9097
    91         hammie = self._get_hammie()
    92         hammie.train(testcontent, spam)
    93         hammie.store()
     98        ()
     99        , spam)
     100        .store()
    94101        return 1
    95102
     
    102109    # Internal methods
    103110
    104     def _get_hammie(self):
    105         try:  # 1.0
    106             return Hammie(TracDbClassifier(self.env, self.log))
    107         except TypeError:  # 1.1
    108             return Hammie(TracDbClassifier(self.env, self.log), 'c')
    109 
    110     def _get_numbers(self):
    111         hammie = self._get_hammie()
    112         return hammie.nspam, hammie.nham
     111    def _get_bayes(self):
     112        return TracDbClassifier(self.env, self.log)
    113113
    114114    # used by admin panel
     
    140140
    141141
    142 class TracDbClassifier(SQLClassifier):
    143     # FIXME: This thing is incredibly slow
    144 
     142class TracDbClassifier(Classifier):
    145143    def __init__(self, env_db, log):
     144
    146145        self.env_db = env_db
    147146        self.log = log
    148         self.nham = None
    149         self.nspam = None
    150         SQLClassifier.__init__(self, 'Trac')
     147        self.statekey = 'saved state'
     148        self.load()
    151149
    152150    def load(self):
     
    157155        else:  # new database
    158156            self.nspam = self.nham = 0
     157
     158
     159
    159160
    160161    def _sanitize(self, text):
     
    224225            words.append(word)
    225226        return words
     227
     228
     229
     230
     231
     232
     233
     234
     235
     236
  • plugins/trunk/spam-filter/tracspamfilter/templates/admin_bayes.html

    r17735 r17736  
    119119          <strong>${_("Error: %(error)s") % {'error': error}}</strong>
    120120          # else:
    121           <strong>${_("Score: %(score)s%%") % {'score': round(score * 100, 2)}}</strong>
     121          <strong>${_("Score: %(score)s%%") % {'score': }}</strong>
    122122          # endif
    123123        </div>
Note: See TracChangeset for help on using the changeset viewer.