Is this text message spam?

Linear SVM on word and character TF-IDF, trained on the UCI SMS Spam Collection (CC BY 4.0)

The model runs in your browser. What you type is never sent anywhere.

Model

Try a message

Loading the model…

How it works
  1. Two views of the message. The message is lowercased and read twice. The word view cuts it into runs of two or more letters, digits or underscores (so “Free!!” becomes free) and drops common English stop words such as you and the. The character view cuts every white-space-separated word, padded with a space on each side, into pieces of 2 to 5 characters (so “free!!” gives pieces such as ␣fr, ree and e!!). Pieces that were not seen at least twice in training are ignored.
  2. Weight with TF-IDF. Each known word or piece gets its count (for pieces, one plus the log of its count) times its inverse document frequency: rarer across the training messages means heavier. Each view’s vector is scaled to length 1.
  3. Add up the evidence. A linear SVM learned one weight for each of the 40,299 words and pieces. The decision value is the starting point (the bias) plus the sum of weight times TF-IDF value over everything found in the message. Above 0 means spam. The bars above split that sum over the pieces of the message that are separated by spaces: each word’s own weight plus the weights of its character pieces.

The SVM sees short spans of characters, so it can pick up shapes such as 08 at the start of a phone number or £ before a price, but not word order or meaning. Its decision value is a margin, not a probability.

Both models only know English text messages from a research corpus published in 2011, drawn mostly from UK and Singapore sources. The measured results, with a majority-class baseline and a comparison of the models, are in the README.

This page re-implements the scikit-learn pipelines in JavaScript. Parity tests check that it gives the same prediction as scikit-learn on all 1,672 held-out test messages, for both models.