"Fossies" - the Fresh Open Source Software Archive

Member "roundup-2.0.0/share/roundup/templates/devel/extensions/spambayes.py" (26 Aug 2019, 2706 Bytes) of package /linux/www/roundup-2.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "spambayes.py": 1.6.1_vs_2.0.0.

    1 import re, math
    2 from roundup.cgi.actions import Action
    3 from roundup.cgi.exceptions import *
    4 from roundup.anypy import xmlrpc_
    5 
    6 import socket
    7 
    8 REVPAT = re.compile(r'(r[0-9]+\b|rev(ision)? [0-9]+\b)')
    9 
   10 def extract_classinfo(db, classname, nodeid):
   11     node = db.getnode(classname, nodeid)
   12 
   13     authorage = node['creation'].timestamp() - \
   14                 db.getnode('user', node.get('author', node.get('creator')))['creation'].timestamp()
   15 
   16     authorid = node.get('author', node.get('creator'))
   17 
   18     content = db.getclass(classname).get(nodeid, 'content')
   19 
   20     tokens = ["klass:%s" % classname,
   21               "author:%s" % authorid,
   22               "authorage:%d" % int(math.log(authorage)),
   23               "hasrev:%s" % (REVPAT.search(content) is not None)]
   24 
   25     return (content, tokens)
   26 
   27 def train_spambayes(db, content, tokens, is_spam):
   28     spambayes_uri = db.config.detectors['SPAMBAYES_URI']
   29 
   30     server = xmlrpc_.client.ServerProxy(spambayes_uri, verbose=False)
   31     try:
   32         server.train({'content':content}, tokens, {}, is_spam)
   33         return (True, None)
   34     except (socket.error, xmlrpc_.client.Error) as e:
   35         return (False, str(e))
   36 
   37 
   38 class SpambayesClassify(Action):
   39     permissionType = 'SB: May Classify'
   40     
   41     def handle(self):
   42         (content, tokens) = extract_classinfo(self.db,
   43                                               self.classname, self.nodeid)
   44 
   45         if "trainspam" in self.form:
   46             is_spam = True
   47         elif "trainham" in self.form:
   48             is_spam = False
   49 
   50         (status, errmsg) = train_spambayes(self.db, content, tokens,
   51                                            is_spam)
   52 
   53         node = self.db.getnode(self.classname, self.nodeid)
   54         props = {}
   55 
   56         if status:
   57             if node.get('spambayes_misclassified', False):
   58                 props['spambayes_misclassified'] = True
   59 
   60             props['spambayes_score'] = 1.0
   61             
   62             s = " SPAM"
   63             if not is_spam:
   64                 props['spambayes_score'] = 0.0
   65                 s = " HAM"
   66             self.client.add_ok_message(self._('Message classified as') + s)
   67         else:
   68             self.client.add_error_message(self._('Unable to classify message, got error:') + errmsg)
   69 
   70         klass = self.db.getclass(self.classname)
   71         klass.set(self.nodeid, **props)
   72         self.db.commit()
   73 
   74 def sb_is_spam(obj):
   75     cutoff_score = float(obj._db.config.detectors['SPAMBAYES_SPAM_CUTOFF'])
   76     try:
   77         score = obj['spambayes_score']
   78     except KeyError:
   79         return False
   80     return score >= cutoff_score
   81 
   82 def init(instance):
   83     instance.registerAction("spambayes_classify", SpambayesClassify)
   84     instance.registerUtil('sb_is_spam', sb_is_spam)
   85