Lisp to Python translation criticism?
Sean 'Shaleh' Perry
shalehperry at attbi.com
Sat Aug 17 04:58:26 CEST 2002
On 17-Aug-2002 John E. Barham wrote:
> 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:
I was pondering this myself (-:
> 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))))))
> return 0.0
one word variable names are rough on the reader. I would also probably factor
that return statement into smaller statements if possible.
> def spam_prob(probs):
> prod = 1.0
> for prob in probs:
> prod = prod * prob
prod = reduce(lambda x,y: x * y, probs)
prod = reduce(operator.mul, probs) # for those who dislike lambda (-:
Personally, I would do this closer to the use of prod. It looks like you mean
to use it but only use it in the last two lines.
> 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.
I was pondering doing this and then using a pickle to retrieve the tables.
What were you considering?
More information about the Python-list