Lisp to Python translation criticism?
John E. Barham
jbarham at jbarham.com
Fri Aug 16 20:59:33 EDT 2002
Don't know how many saw the story on Slashdot about Paul Graham's article
(http://www.paulgraham.com/spam.html) on how he filters spam. He posted two
snippets of code in Lisp, a language which I only have a very passing
knowledge of. Here's my attempt at translating it into Python:
Lisp:
(let ((g (* 2 (or (gethash word good) 0)))
(b (or (gethash word bad) 0)))
(unless (< (+ g b) 5)
(max .01
(min .99 (float (/ (min 1 (/ b nbad))
(+ (min 1 (/ g ngood))
(min 1 (/ b nbad)))))))))
(let ((prod (apply #'* probs)))
(/ prod (+ prod (apply #'* (mapcar #'(lambda (x) (- 1 x))
probs)))))
Python:
def spam_word_prob(word, good, bad, ngood, nbad):
g = 2 * good.get(word, 0)
b = bad.get(word, 0)
if g + b >= 5:
return max(0.01, min(0.99, float(min(1, b / nbad) / ((min(1, g /
ngood) + min(1, b / nbad))))))
else:
return 0.0
def spam_prob(probs):
prod = 1.0
for prob in probs:
prod = prod * prob
inv_probs = [1 - x for x in probs]
inv_prob = 1.0
for prob in inv_probs:
inv_prob = inv_prob * prob
return prod / (prob + inv_prob)
Any comments on the correctness, style, efficiency etc. of my translation?
I'd like to write a Python spam filtering system using Graham's techniques.
Please note that this is not meant to revive the perpetual debate over the
relative merits of Python's lambda... ;)
John
More information about the Python-list
mailing list