Exemplo n.º 1
0
    def untrain_from_header(self, msg):
        """Untrain bayes based on X-Spambayes-Trained header.

        msg can be a string, a file object, or a Message object.

        If no such header is present, nothing happens.

        If add_header is True, add a header with how it was trained (in
        case we need to untrain later)

        """

        msg = mboxutils.get_message(msg)
        trained = msg.get(options["Headers", "trained_header_name"])
        if not trained:
            return
        del msg[options["Headers", "trained_header_name"]]
        if trained == options["Headers", "header_ham_string"]:
            self.untrain_ham(msg)
        elif trained == options["Headers", "header_spam_string"]:
            self.untrain_spam(msg)
        else:
            raise ValueError('%s header value unrecognized'
                             % options["Headers", "trained_header_name"])
Exemplo n.º 2
0
    def score_and_filter(self, msg, header=None, spam_cutoff=None,
                         ham_cutoff=None, debugheader=None,
                         debug=None, train=None):
        """Score (judge) a message and add a disposition header.

        msg can be a string, a file object, or a Message object.

        Optionally, set header to the name of the header to add, and/or
        spam_cutoff/ham_cutoff to the probability values which must be met
        or exceeded for a message to get a 'Spam' or 'Ham' classification.

        An extra debugging header can be added if 'debug' is set to True.
        The name of the debugging header is given as 'debugheader'.

        If 'train' is True, also train on the result of scoring the
        message (ie. train as ham if it's ham, train as spam if it's
        spam).  If the message already has a trained header, it will be
        untrained first.  You'll want to be very dilligent about
        retraining mistakes if you use this option.

        All defaults for optional parameters come from the Options file.

        Returns the score and same message with a new disposition header.
        """

        if header == None:
            header = options["Headers", "classification_header_name"]
        if spam_cutoff == None:
            spam_cutoff = options["Categorization", "spam_cutoff"]
        if ham_cutoff == None:
            ham_cutoff = options["Categorization", "ham_cutoff"]
        if debugheader == None:
            debugheader = options["Headers", "evidence_header_name"]
        if debug == None:
            debug = options["Headers", "include_evidence"]
        if train == None:
            train = options["Hammie", "train_on_filter"]

        msg = mboxutils.get_message(msg)
        try:
            del msg[header]
        except KeyError:
            pass
        if train:
            self.untrain_from_header(msg)
        prob, clues = self._scoremsg(msg, True)
        if prob < ham_cutoff:
            is_spam = False
            disp = options["Headers", "header_ham_string"]
        elif prob > spam_cutoff:
            is_spam = True
            disp = options["Headers", "header_spam_string"]
        else:
            is_spam = False
            disp = options["Headers", "header_unsure_string"]
        if train:
            self.train(msg, is_spam, True)
        basic_disp = disp
        disp += "; %.*f" % (options["Headers", "header_score_digits"], prob)
        if options["Headers", "header_score_logarithm"]:
            if prob <= 0.005 and prob > 0.0:
                import math
                x = -math.log10(prob)
                disp += " (%d)" % x
            if prob >= 0.995 and prob < 1.0:
                x = -math.log10(1.0-prob)
                disp += " (%d)" % x
        del msg[header]
        msg.add_header(header, disp)

        # Obey notate_to and notate_subject.
        for header in ('to', 'subject'):
            if basic_disp in options["Headers", "notate_"+header]:
                orig = msg[header]
                del msg[header]
                msg[header] = "%s,%s" % (basic_disp, orig)

        if debug:
            disp = self.formatclues(clues)
            del msg[debugheader]
            msg.add_header(debugheader, disp)
        result = mboxutils.as_string(msg, unixfrom=(msg.get_unixfrom()
                                                    is not None))
        return prob, result