-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExploreEnron.R
270 lines (123 loc) · 5.36 KB
/
ExploreEnron.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
### Libraries
setwd("E:/SSJ/Sujit/IntrotoML/")
#library(formattable) # output is easier to read an well formatted
library(stringr) # String manipulation, Regex
library(plyr)
library(ggplot2)
library(tm)
library(SnowballC)
library(RColorBrewer)
library(wordcloud)
### read emails.csv
enron <- read.csv("E:/SSJ/Sujit/IntrotoML/emails.csv", stringsAsFactors = FALSE)
### locate the blank line "\n\n"
breaks <- str_locate(enron$message, "\n\n")
### Extract headers and bodies
headers <- str_sub(enron$message, end = breaks[,1] - 1)
bodies <- str_sub(enron$message, start = breaks[,2] + 1)
### Splitting the email header
parseHeader <- function(header){
MessageID <- str_sub(str_extract(header, "^Message-ID:.*"), start = 12)
Date <- str_sub(str_extract(header,"Date:.*"), start = 7)
From <- str_sub(str_extract(header,"From:.*"), start = 7)
To <- str_sub(str_extract(header,"To:.*"), start = 5)
Subject <- str_sub(str_extract(header,"Subject:.*"), start = 10)
#X-cc <- str_sub(str_extract(header,"X\\-cc:.*"), start = 7)
#X-bcc <- str_sub(str_extract(header,"X\\-bcc:.*"), start = 8)
headerParsed <- data.frame(MessageID, Date, From, To, Subject,
stringsAsFactors = FALSE)
return(headerParsed)
}
headerParsed <- parseHeader(headers)
### Conversion of dates
## UTC time
datesTest <- strptime(headerParsed$Date, format = "%a, %d %b %Y %H:%M:%S %z")
## localtime
datesLocal <- strptime(headerParsed$Date, format = "%a, %d %b %Y %H:%M:%S")
### Copy dates
headerParsed$Date <- datesTest
headerParsed$DateLocal <- datesLocal
# remove dates Test
rm(datesTest)
rm(datesLocal)
### File column
## split
fileSplit <- str_split(enron$file, "/")
fileSplit <-rbind.fill(lapply(fileSplit, function(X) data.frame(t(X))))
### Creating one dataset
enron <- data.frame(fileSplit, headerParsed, bodies, stringsAsFactors = FALSE)
colnames(enron)[1] <- "User"
### Cleaning up
rm(headerParsed)
rm(bodies)
rm(headers)
rm(breaks)
rm(fileSplit)
# garbage collection
gc()
## Some Top 20s
### Mail writers
head(sort(table(enron$From), decreasing = TRUE), n=20)
### Mail recipients
head(sort(table(enron$To), decreasing = TRUE), n=20)
### User
head(sort(table(enron$User), decreasing = TRUE), 20)
## Weekdays and Hour of day
# extract weekday
enron$Weekday <- weekdays(enron$DateLocal)
# extract Hour of day
enron$Hour <- enron$DateLocal$hour
## Weekdays Analysis
WeekdayCounts <- as.data.frame(table(enron$Weekday))
str(WeekdayCounts)
WeekdayCounts$Var1 <- factor(WeekdayCounts$Var1, ordered=TRUE,
levels=c( "Monday", "Tuesday", "Wednesday", "Thursday", "Friday","Saturday", "Sunday"))
DayHourCounts <- as.data.frame(table(enron$Weekday, enron$Hour))
str(DayHourCounts)
DayHourCounts$Hour <- as.numeric(as.character(DayHourCounts$Var2))
DayHourCounts$Var1 <- factor(WeekdayCounts$Var1, ordered=TRUE,
levels=c( "Monday", "Tuesday", "Wednesday", "Thursday", "Friday","Saturday", "Sunday"))
### Plot number of emails per Weekday
ggplot(WeekdayCounts, aes(x=Var1, y=Freq)) + geom_line(aes(group=1))
### Plot number of emails per Hour per Day
ggplot(DayHourCounts, aes(x=Hour, y=Freq)) +
geom_line(aes(group=Var1, color=Var1), size=1)
### Heatmap: emails per Hour per Day
ggplot(DayHourCounts, aes(x = Hour, y = Var1)) +
geom_tile(aes(fill = Freq)) +
scale_fill_gradient(name="Total emails", low = "lightgrey", high = "darkblue") +
theme(axis.title.y = element_blank())
## Making a Wordcloud of email bodies
#### Create a corpus using the bodies variable
corpus <- Corpus(VectorSource(enron$bodies[1:100000]))
#### Convert corpus to lowercase
corpus <- tm_map(corpus,tolower)
corpus <- tm_map(corpus, PlainTextDocument)
#### Remove punctuation from corpus
corpus <- tm_map(corpus, removePunctuation)
#### Remove all English-language stopwords
corpus <- tm_map(corpus, removeWords, stopwords("english"))
#### Remove some more words
corpus <- tm_map(corpus, removeWords, c("just", "will", "thanks","please", "can", "let", "said", "say", "per"))
#### Stem document
corpus <- tm_map(corpus, stemDocument)
# Stop Here. Error correction must be done. Progress will be done in Major Project.
#### Build a document-term matrix out of the corpus
bodiesDTM <- TermDocumentMatrix(corpus)
#### remove Sparse Terms
sparseDTM <- removeSparseTerms(bodiesDTM, 0.99)
sparseDTM
# some cleaning due to memory intensive operations following
rm(corpus)
rm(bodiesDTM)
gc()
#### Convert the document-term matrix to a data frame called allBodies
allBodies <- as.data.frame(as.matrix(sparse))
#### Building wordcloud
par(bg = "gray27") # setting background color to a dark grey
pal <- brewer.pal(7,"Dark2") # Choosing a color palette
# Wordcloud
wordcloud(colnames(allBodies), colSums(allBodies), scale = c(2.5,0.25), max.words = 150, colors = pal)
## TODO
# 1. deleting some unimportant mails, like private conversation about vacation or amazon.com mails ...
# 2. Creating a network and graph: Person1 $\overrightarrow{writes mail to}$ Person2 $\overrightarrow{receives from}$ Person3 and so on