Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
STA 141B
We are all familiar with SPAM email messages, phone calls, texts, ... Nowadays, SPAM filters are quite effective. These filters use statistics to classify new email messages as SPAM or HAM (valid mail). There are many statistical techniques one could use. But before we can use them we need “data” . We need to “measure” variables on each message in a sample of email messages. We also need to know if they are SPAM or HAM. We can then train a statistical classifier using these variables to predict if a new message is SPAM or HAM.
Your job is to process a collection of email messages and create an R data frame of “derived” variables that give various measures of the email messages, e.g. the number of recipients to whom the mail was sent, the percentage of capital words in the body of the text, is the message a reply to another message. See below for a list of all the variables and also consider other variables you think might help help classify a message as SPAM versus HAM.
The data we use are derived from the email messages from the Spam Assassin website
http://spamassassin.apache.org/publiccorpus. These are old messages from the early 2000’s. Accordingly, the nature of SPAM messages is different than it is today.
The data you are to work with is available at https://canvas.ucdavis.edu/files/23930657/download.
The messages are in 5 different directories/folders. The name of the directory indicates whether the messages it contains are HAM or SPAM. There are 6,541 messages in total.
Once you have this data frame. of “derived” variables, explore the relationships amongst these variables and especially how they might be used to classify SPAM and HAM messages. In other words, look at density plots, scatter plots, mosaic plots of the variables. For scatter plots, you might color code the points based on if the message is SPAM or HAM. Which variables seem to do best at discriminating between SPAM and HAM messages?
For each message, extract URLs in the body of the message. Which URLs are only in the SPAM messages? Which Web domains (e.g., www.google.com) are most common in the SPAM URLs?
You are to write functions to
? process a file containing an email message, converting it into a list with 3 elements
1. the header of the email message as a named vector of values,
2. the body of the message
3. a list with each attachment, where each attachment is a list with
– a header as a named vector of values
– the body of the attachment
Once you have a list of message information in this format, you can compute the different variables.
Compute at least 20 of these.
And there are bonus marks for proposing and computing other variables that could help classify messages.
Some functions you might find useful include: list.files, readLines, strsplit, grep, grepl, gregexpr, gregexec, regmatches, gsub, substring, nchar.
1 The Anatomy of an E-mail message
Electronic mail, usually called e-mail, consists of simple text messages – a piece of text sent to a recipient via the internet. An e-mail message consists of two parts, the header and the body. The body of the e-mail message is separated from the header by a single blank line. When an attachment is added to an e-mail message, the attachment is included in the body of the message. Even with attachments, e-mail messages are still only text messages.
1.1 The E-mail Header
The header contains information about the message such as the sender’s address, the recipient’s address, and the date of transmission. This information is relayed in a special format that consists of KEY:VALUE pairs. Below is a sample header from a message found on the SpamAssassin website.
Return-Path: [email protected]
Delivery-Date: Fri Sep 6 20:53:36 2002
From: [email protected] (David LeBlanc)
Date: Fri, 6 Sep 2002 12:53:36 -0700
Subject: [Spambayes] Deployment
In-Reply-To: <[email protected]> Message-ID: <[email protected]>
Notice the keys are Return-Path, Delivery-Date, From, Date, Subject, In-Reply-to, and Message-ID. The
value follows the keyword. For example, in the above header, the value of the From key is whisperatoz.net (David LeBlanc).
Some of these keys are mandatory such as Date, From, and To (or In-Reply-To, or Bcc). Other keys are optional but widely used, such as Subject, Cc, Received, and Message-ID. Many keys
Description of Variables
isSpam logical whether mail is Spam (TRUE) or Ham (FALSE)
isRe logical if the string Re: appears as the first word in the subject of the message
numLinesInBody integer a count of the number of lines in the body of the email message
bodyCharacterCount integer the number of characters in the body of the email message
replyUnderline logical whether the Reply-To field in the header has an underline and num- bers/letters
subjectExclamationCount integer a count of the number of exclamation marks (!) in the subject of the message
subjectQuestCount integer the number of question marks in the subject
numAttachments integer the number of attachments in the message.
priority logical whether the message’s header had an X-Priority or X-Msmail- Priority that was set to high
numRecipients integer the number of recipients in the To, Cc fields
percentCapitals numeric the percentage of the characters in the body of the email that are upper case (excluding blanks, numbers, and punctuation)
isInReplyTo logical whether the header of the message has an In-Reply-To field.
sortedRecipients logical the recipient list is sorted by address
subjectPunctuationCheck logical whether the subject has punctuation or digits surrounded by char- acters, e.g. V?agra and pay1ng, but not New!
hourSent integer the hour in the day the mail was sent (0 – 23)
multipartText logical whether the header states that the message is a multipart/text, i.e. with attachments.
containsImages logical whether the message contain images (in HTML, i.e. the <img> tag)
isPGPsigned logical indicates whether the mail was digitally signed (e.g. using PGP or GPG)
percentHTMLTags numeric the proportion of any HTML text in the message’s body that is made up of HTML markup and not content.
subjectSpamWords logical whether the subject contains one of the following phrases: viagra, pounds, free, weight, guarantee, millions, dollars, credit, risk, pre- scription, generic, drug, money back, credit card.
percentSubjectBlanks numeric the percentage of blanks in the subject
messageIdHasNoHostname logical whether the message identifier (id) that uniquely identifies the message has no component identifying the machine from which it was sent
fromNumericEnd logical whether the user login in the From: field ends in numbers
isYelling logical whether the Subject of the mail is in capital letters
percentForwards numeric percent of the message’s body that is made up of content included from other messages
isOriginalMessage logical body does not contain the phrase “original message” or something similar
isDear logical whether the message body contains a form of the introduction Dear
isWrote logical wh(...)ether the text includes a line indicating an included message
as identified by the word wrote: in several different possible lan- guages
averageWordLength numeric the average length of the words in the body of the message numDollarSigns integer the number of dollar signs in the body of the message
are ignored by the mail system, but the entire header is relayed on to the recipient’s server whether or not it is recognized. For example, keys starting with “X-” are for personal application or institution use and are ignored by other applications. The Received header lines are important because they allow the message to be tracked. As a message makes its way to the intended recipient, servers add additional Received lines to the header.
Below are some typical header keys:
? Message-Id: a unique identifier for the message, assigned by the originating server;
? Return-Path: specifies the sender’s address and bounced mail gets sent to this address;
? Date: added by the e-mail client;
? Cc: lists the recipients of a “carbon copied” message;
? Reply-To: the address set by the sender to which the recipient can reply;
? MIME-Version: used for encoding binary content as attachments.
A value may be continued on a second line of the header, in which case the line will be indented and begin with a tab character or blank spaces. Consider this header
From [email protected] Thu Aug 22 14:23:39 2002
Return-Path: <[email protected]>
Delivered-To: [email protected]
Received: from localhost (localhost [ 127.0.0.1 ])
by phobos.labs.netnoteinc.com (Postfix) with ESMTP id 9DAE147C66 for <zzzz@localhost>; Thu, 22 Aug 2002 09:23:38 -0400 (EDT)
Received: from phobos [ 127.0.0.1 ]
by localhost with IMAP (fetchmail-5.9.0)
for zzzz@localhost (single-drop); Thu, 22 Aug 2002 14:23:38 +0100 (IST) MIME-Version: 1.0
After the ’From ’ row, there are two fields (Return-Path and Delivered-To) on separate lines. The next two fields however are ’Received’ fields and both span 3 rows. The first row of each starts in the first column and identifies the key ’Received’. The subsequent lines start with white space. The second ’Received’ starts in the first column and so indicates the start of a new field, and the conclusion of the previous field.
1.2 The Body of the Email
The body of the email is all the text after the first blank line following the header and up to any attachments. If the message has no attachments, then the body is everything excluding the header. If the message has attachments, we need to find where they begin to find the body. So we look at attachments next.
1.3 E-mail Attachments
An Internet standard called MIME, Multipurpose Internet Mail Extensions, specifies how messages may be formatted and how to separate the attachments from the message. Information about the MIME encoding is provided through header fields, which are specified in an RFC.
The Content-Type key is used to describe the content of a component or of the entire body. The value provides the top-level type and subtype using the syntax:
top-level/subtype; parameter .
Parameters may be required or optional. Below is an example of a content-type where the top-level is multipart, which indicates there will be several documents in the body of the message, the mixed subtype tells us that the documents may be of different types, and the boundary parameter provides a special character string for delimiting the start and end of the message parts.
Content-Type: multipart/mixed;
boundary="----=_NextPart_000_00DE_01511A02.DB1A02A0"
The Content-Type field in this example tells the receiving e-mail program that this message has more than one component, and each component will be separated by the string of characters
----=_NextPart_000_00DE_01511A02.DB1A02A0
The boundary string marks the beginning of each component. It is prefaced with two additional hyphens in all instances. The boundary string is also used to denote the end of the message, where it is both prefaced by two hyphens and followed immediately by two hyphens. The receiving email program knows when the last component of the message has been read when it reads the boundary string with two additional hyphens on either end of the string,
------=_NextPart_000_00DE_01511A02.DB1A02A0--
Each component of a message must be prefaced by the boundary string and a blank line. It may also contain MIME information. If the blank line is missing, the recipient’s e-mail client may have difficulty telling where the header information stops and the text of the message begins.
There are seven top-level types of attachments: text, image, audio, video, application, multipart, and mes- sage. Other examples of Content-Type values follow:
Content-type: text/html; charset=euc-kr;
Content-Type: application/zip; name="testFile.zip"
The first example indicates that the message is in HTML format using a Korean character set. The second indicates that the component is a zip file, and the sender named it testFile.zip. Binary files (such as a compressed archive) can be sent as attachments. In such cases, the sender’s software must first encode the binary file so that it can be sent over the Internet. One common encoding scheme is known as base64.
We conclude by providing two sample e-mail messages. The first is a plain text e-mail with no attachments. It consists of an instructor’s response to an e-mail inquiry sent by a student. The second e-mail message consists of a text message and two attachments sent by a student to the instructor. This e-mail message has then been forwarded by the instructor to the teaching assistant. The three periods at the end of each attachment indicates that only part of the attachment has been displayed. The first attachment is a pdf file and the second is an HTML file. The forwarded message is a plain text file.