/usr/lib/python2.7/dist-packages/spambayes/hammie.py is in spambayes 1.1b1-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | #! /usr/bin/env python
import math
from spambayes import mboxutils
from spambayes import storage
from spambayes.Options import options
from spambayes.tokenizer import tokenize
class Hammie:
"""A spambayes mail filter.
This implements the basic functionality needed to score, filter, or
train.
"""
def __init__(self, bayes, mode):
self.bayes = bayes
self.mode = mode
def _scoremsg(self, msg, evidence=False):
"""Score a Message.
msg can be a string, a file object, or a Message object.
Returns the probability the message is spam. If evidence is
true, returns a tuple: (probability, clues), where clues is a
list of the words which contributed to the score.
"""
return self.bayes.spamprob(tokenize(msg), evidence)
def formatclues(self, clues, sep="; "):
"""Format the clues into something readable."""
return sep.join(["%r: %.2f" % (word, prob)
for word, prob in clues
if (word[0] == '*' or
prob <= options["Headers",
"clue_mailheader_cutoff"] or
prob >= 1.0 - options["Headers",
"clue_mailheader_cutoff"])])
def score(self, msg, evidence=False):
"""Score (judge) a message.
msg can be a string, a file object, or a Message object.
Returns the probability the message is spam. If evidence is
true, returns a tuple: (probability, clues), where clues is a
list of the words which contributed to the score.
"""
return self._scoremsg(msg, evidence)
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
def filter(self, msg, header=None, spam_cutoff=None,
ham_cutoff=None, debugheader=None,
debug=None, train=None):
_prob, result = self.score_and_filter(
msg, header, spam_cutoff, ham_cutoff, debugheader,
debug, train)
return result
def train(self, msg, is_spam, add_header=False):
"""Train bayes with a message.
msg can be a string, a file object, or a Message object.
is_spam should be 1 if the message is spam, 0 if not.
If add_header is True, add a header with how it was trained (in
case we need to untrain later)
"""
self.bayes.learn(tokenize(msg), is_spam)
if add_header:
if is_spam:
trained = options["Headers", "header_spam_string"]
else:
trained = options["Headers", "header_ham_string"]
del msg[options["Headers", "trained_header_name"]]
msg.add_header(options["Headers", "trained_header_name"], trained)
def untrain(self, msg, is_spam):
"""Untrain bayes with a message.
msg can be a string, a file object, or a Message object.
is_spam should be True if the message is spam, False if not.
"""
self.bayes.unlearn(tokenize(msg), is_spam)
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"])
def train_ham(self, msg, add_header=False):
"""Train bayes with ham.
msg can be a string, a file object, or a Message object.
If add_header is True, add a header with how it was trained (in
case we need to untrain later)
"""
self.train(msg, False, add_header)
def train_spam(self, msg, add_header=False):
"""Train bayes with spam.
msg can be a string, a file object, or a Message object.
If add_header is True, add a header with how it was trained (in
case we need to untrain later)
"""
self.train(msg, True, add_header)
def untrain_ham(self, msg):
"""Untrain bayes with a message previously trained as ham.
msg can be a string, a file object, or a Message object.
"""
self.untrain(msg, False)
def untrain_spam(self, msg):
"""Untrain bayes with a message previously traned as spam.
msg can be a string, a file object, or a Message object.
"""
self.untrain(msg, True)
def store(self):
"""Write out the persistent store.
This makes sure the persistent store reflects what is currently
in memory. You would want to do this after a write and before
exiting.
"""
self.bayes.store()
def close(self):
if self.mode != 'r':
self.store()
def open(filename, useDB="dbm", mode='r'):
"""Open a file, returning a Hammie instance.
mode is used as the flag to open DBDict objects. 'c' for read-write
(create if needed), 'r' for read-only, 'w' for read-write.
"""
return Hammie(storage.open_storage(filename, useDB, mode), mode)
if __name__ == "__main__":
# Everybody's used to running hammie.py. Why mess with success? ;)
from spambayes import hammiebulk
hammiebulk.main()
|