Spam
Time limit1sMemory limit128 MB
Count trigram frequencies in sample spam and non-spam messages, then classify each test message by which sample it matches more closely under the cosine similarity measure.
- Level
Medium5 of 10
- Topics
- String, Hash map, Math, Implementation
- Solved
- No attempts yet
Problem
Unsolicited email (spam) is annoying and clutters your mailbox. Write a spam filter: a program that reads email messages made of ordinary ASCII characters and decides whether or not each message is spam.
How can we tell whether a message is spam? Spam tends to contain words and phrases that are uncommon in genuine email. For example, the phrase
MAKE MONEY FAST, HONEY!!
is all uppercase, contains the word money, and ends with a double exclamation mark.
One way to build a spam filter is to read many spam and non-spam messages and come up with a set of rules that classify any particular message. Doing this by hand is tedious and error prone, so instead we write a program to automate it.
A useful step is to split the text into a set of trigrams. A trigram is a sequence of three adjacent characters that appear in the message, and it is case sensitive. The phrase above is composed of the trigrams:
MAK
AKE
KE
E M
MO
MON
ONE
NEY
EY
Y F
FA
FAS
AST
ST,
T,
, H
HO
HON
ONE
NEY
EY!
Y!!
If we examine a sample of spam and non-spam messages, some trigrams turn out to be more common in spam and others more common in non-spam. This leads to a classification method:
- Take a large sample of spam messages and count how many times each trigram occurs. In the phrase above there are distinct trigrams:
ONEandNEYoccur twice each and the remaining occur once each. (A trigram that never appears occurs times.) Formally, for each trigram we compute the frequency with which it occurs in the spam sample. - Take a large sample of non-spam messages and compute , the frequency with which each trigram appears in that sample.
- For each message to be filtered, compute for every trigram .
- If resembles more closely than it resembles , the message is spam; otherwise it is non-spam.
- A similarity measure tells how closely and resemble one another. One of the simplest is the cosine measure:
A message is classified as spam if
Input
The first line contains three integers: , the number of sample spam messages that follow; , the number of sample non-spam messages that follow; and , the number of messages to be classified as spam or non-spam using the trigram frequencies of the sample messages. Each message consists of several lines of text and is terminated by a line containing exactly ENDMESSAGE. This terminator line never appears anywhere else in the input and is not considered part of the message.
Output
For each of the messages, print two lines. On the first line print and , each rounded to five decimal places. On the second line print the classification of the message (spam or non-spam).
When forming trigrams we never include a newline character, and we never form a trigram that spans two lines. For example, in the first spam message of the first sample the only trigrams are:
AAA
BBB
BB
B
C
CC
CCC