/usr/share/pyshared/spambayes/CostCounter.py is in spambayes 1.1a6-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 | from spambayes.Options import options
class CostCounter:
name = "Superclass Cost"
def __init__(self):
self.total = 0
def spam(self, scr):
pass
def ham(self, scr):
pass
def __str__(self):
return "%s: $%.4f" % (self.name, self.total)
class CompositeCostCounter:
def __init__(self, cclist):
self.clients = cclist
def spam(self, scr):
for c in self.clients:
c.spam(scr)
def ham(self, scr):
for c in self.clients:
c.ham(scr)
def __str__(self):
s = []
for c in self.clients:
s.append(str(c))
return '\n'.join(s)
class DelayedCostCounter(CompositeCostCounter):
def __init__(self, cclist):
CompositeCostCounter.__init__(self, cclist)
self.spamscr = []
self.hamscr = []
def spam(self, scr):
self.spamscr.append(scr)
def ham(self, scr):
self.hamscr.append(scr)
def __str__(self):
for scr in self.spamscr:
CompositeCostCounter.spam(self, scr)
for scr in self.hamscr:
CompositeCostCounter.ham(self, scr)
s = []
for line in CompositeCostCounter.__str__(self).split('\n'):
s.append('Delayed-'+line)
return '\n'.join(s)
class CountCostCounter(CostCounter):
def __init__(self):
CostCounter.__init__(self)
self._fp = 0
self._fn = 0
self._unsure = 0
self._unsureham = 0
self._unsurespam = 0
self._spam = 0
self._ham = 0
self._correctham = 0
self._correctspam = 0
self._total = 0
def spam(self, scr):
self._total += 1
self._spam += 1
if scr < options["Categorization", "ham_cutoff"]:
self._fn += 1
elif scr < options["Categorization", "spam_cutoff"]:
self._unsure += 1
self._unsurespam += 1
else:
self._correctspam += 1
def ham(self, scr):
self._total += 1
self._ham += 1
if scr > options["Categorization", "spam_cutoff"]:
self._fp += 1
elif scr > options["Categorization", "ham_cutoff"]:
self._unsure += 1
self._unsureham += 1
else:
self._correctham += 1
def __str__(self):
return ("Total messages: %d; %d (%.1f%%) ham + %d (%.1f%%) spam\n"%(
self._total,
self._ham, zd(100.*self._ham,self._total),
self._spam, zd(100.*self._spam,self._total))+
"Ham: %d (%.2f%%) ok, %d (%.2f%%) unsure, %d (%.2f%%) fp\n"%(
self._correctham, zd(100.*self._correctham,self._ham),
self._unsureham, zd(100.*self._unsureham,self._ham),
self._fp, zd(100.*self._fp,self._ham))+
"Spam: %d (%.2f%%) ok, %d (%.2f%%) unsure, %d (%.2f%%) fn\n"%(
self._correctspam, zd(100.*self._correctspam,self._spam),
self._unsurespam, zd(100.*self._unsurespam,self._spam),
self._fn, zd(100.*self._fn,self._spam))+
"Score False: %.2f%% Unsure %.2f%%"%(
zd(100.*(self._fp+self._fn),self._total),
zd(100.*self._unsure,self._total)))
def zd(x, y):
if y > 0:
return x / y
else:
return 0
class StdCostCounter(CostCounter):
name = "Standard Cost"
def spam(self, scr):
if scr < options["Categorization", "ham_cutoff"]:
self.total += options["TestDriver", "best_cutoff_fn_weight"]
elif scr < options["Categorization", "spam_cutoff"]:
self.total += options["TestDriver", "best_cutoff_unsure_weight"]
def ham(self, scr):
if scr > options["Categorization", "spam_cutoff"]:
self.total += options["TestDriver", "best_cutoff_fp_weight"]
elif scr > options["Categorization", "ham_cutoff"]:
self.total += options["TestDriver", "best_cutoff_unsure_weight"]
class FlexCostCounter(CostCounter):
name = "Flex Cost"
def _lambda(self, scr):
if scr < options["Categorization", "ham_cutoff"]:
return 0
elif scr > options["Categorization", "spam_cutoff"]:
return 1
else:
return (scr - options["Categorization", "ham_cutoff"]) / (
options["Categorization", "spam_cutoff"] \
- options["Categorization", "ham_cutoff"])
def spam(self, scr):
self.total += (1 - self._lambda(scr)) * options["TestDriver",
"best_cutoff_fn_weight"]
def ham(self, scr):
self.total += self._lambda(scr) * options["TestDriver",
"best_cutoff_fp_weight"]
class Flex2CostCounter(FlexCostCounter):
name = "Flex**2 Cost"
def spam(self, scr):
self.total += (1 - self._lambda(scr))**2 * options["TestDriver",
"best_cutoff_fn_weight"]
def ham(self, scr):
self.total += self._lambda(scr)**2 * options["TestDriver",
"best_cutoff_fp_weight"]
def default():
return CompositeCostCounter([
CountCostCounter(),
StdCostCounter(),
FlexCostCounter(),
Flex2CostCounter(),
DelayedCostCounter([
CountCostCounter(),
StdCostCounter(),
FlexCostCounter(),
Flex2CostCounter(),
])
])
def nodelay():
return CompositeCostCounter([
CountCostCounter(),
StdCostCounter(),
FlexCostCounter(),
Flex2CostCounter(),
])
if __name__ == "__main__":
cc = default()
cc.ham(0)
cc.spam(1)
cc.ham(0.5)
cc.spam(0.5)
options["Categorization", "spam_cutoff"] = 0.7
options["Categorization", "ham_cutoff"] = 0.4
print cc
|