Changeset 17736
- Timestamp:
- Oct 28, 2023, 11:29:42 PM (9 months ago)
- Location:
- plugins/trunk/spam-filter
- Files:
-
- 9 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
plugins/trunk/spam-filter/setup.py
r17735 r17736 52 52 extras_require={ 53 53 'dns': ['dnspython>=1.3.5'], 54 'spambayes': ['spambayes'],54 'spambayes': ['spambayes'], 55 55 'pillow': ['pillow>=2'], 56 56 'account': ['TracAccountManager >= 0.4'], -
plugins/trunk/spam-filter/tracspamfilter/admin.py
r17729 r17736 446 446 447 447 bayes = BayesianFilterStrategy(self.env) 448 hammie = bayes._get_hammie()449 448 data = {} 450 449 … … 459 458 data['content'] = bayes_content 460 459 try: 461 data['score'] = hammie.score(bayes_content.encode('utf-8'))460 data['score'] = ) 462 461 except Exception as e: 463 462 self.log.warning('Bayes test failed: %s', e, exc_info=True) … … 488 487 req.redirect(req.href.admin(cat, page)) 489 488 ratio = '' 490 nspam = hammie.bayes.nspam 491 nham = hammie.bayes.nham 489 nspam, nham = bayes.getnumbers() 492 490 if nham and nspam: 493 491 if nspam > nham: -
plugins/trunk/spam-filter/tracspamfilter/filters/bayes.py
r17735 r17736 22 22 from tracspamfilter.api import IFilterStrategy, N_ 23 23 24 from spambayes.hammie import Hammie 25 from spambayes.storage import SQLClassifier 26 24 from spambayes.classifier import Classifier 25 from spambayes.tokenizer import tokenize 27 26 28 27 class BayesianFilterStrategy(Component): … … 51 50 52 51 def test(self, req, author, content, ip): 53 hammie = self._get_hammie()54 nspam = hammie.bayes.nspam55 nham = hammie.bayes.nham52 () 53 nspam = bayes.nspam 54 nham = bayes.nham 56 55 if author is not None: 57 56 testcontent = author + '\n' + content … … 71 70 "large, results may be bad.") 72 71 73 score = hammie.score(testcontent)72 score = ) 74 73 self.log.debug("SpamBayes reported spam probability of %s", score) 75 74 points = -int(round(self.karma_points * (score * 2 - 1))) … … 79 78 ("%3.2f" % (score * 100))) 80 79 80 81 82 83 84 85 86 87 81 88 def train(self, req, author, content, ip, spam=True): 82 # Split tokens at null characters by tokenizer in spambayes.hammie89 # Split tokens at null characters by tokenizer 83 90 content = content.replace('\x00', ' ') 84 91 if author is not None: … … 89 96 spam and "spam" or "ham") 90 97 91 hammie = self._get_hammie()92 hammie.train(testcontent, spam)93 hammie.store()98 () 99 , spam) 100 .store() 94 101 return 1 95 102 … … 102 109 # Internal methods 103 110 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) 113 113 114 114 # used by admin panel … … 140 140 141 141 142 class TracDbClassifier(SQLClassifier): 143 # FIXME: This thing is incredibly slow 144 142 class TracDbClassifier(Classifier): 145 143 def __init__(self, env_db, log): 144 146 145 self.env_db = env_db 147 146 self.log = log 148 self.nham = None 149 self.nspam = None 150 SQLClassifier.__init__(self, 'Trac') 147 self.statekey = 'saved state' 148 self.load() 151 149 152 150 def load(self): … … 157 155 else: # new database 158 156 self.nspam = self.nham = 0 157 158 159 159 160 160 161 def _sanitize(self, text): … … 224 225 words.append(word) 225 226 return words 227 228 229 230 231 232 233 234 235 236 -
plugins/trunk/spam-filter/tracspamfilter/templates/admin_bayes.html
r17735 r17736 119 119 <strong>${_("Error: %(error)s") % {'error': error}}</strong> 120 120 # else: 121 <strong>${_("Score: %(score)s%%") % {'score': round(score * 100, 2)}}</strong>121 <strong>${_("Score: %(score)s%%") % {'score': }}</strong> 122 122 # endif 123 123 </div>
Note:
See TracChangeset
for help on using the changeset viewer.