Bayesian spam filtering with a bag-of-words classifier
Email / NLPIn the early 2000s, rule-based spam filters were brittle and easy for spammers to evade. Paul Graham’s essay "A Plan for Spam" proposed instead learning a per-user statistical filter that scores each incoming email from the words it contains, treating tokens as (naively) independent evidence for the Spam vs Ham classes.
Build a bag-of-words token model: estimate and from each user’s own corpus of good and spam mail, smooth rare tokens to avoid zero probabilities, and combine the most informative tokens via Bayes’ theorem to produce a spam probability for each new message.
The naive-Bayes-style filter classified spam with very high accuracy — Graham reported catching about 99.5% of spam with roughly 0.03% false positives on his mail — dramatically better than the hand-written rules it replaced, and the approach (popularized as "Bayesian spam filtering") became the template for production spam filters. The lesson: a simple bag-of-words Naive Bayes model, trained per user and properly smoothed, is a remarkably strong text classifier.
Source: A Plan for Spam — Paul Graham