-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthorship_Identification.R
377 lines (290 loc) · 10 KB
/
Authorship_Identification.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# Library used in this script.
library(dplyr) # slice function
library(tidyr) # separate_rows function
library(stopwords) # stopwords function
library(stringr) # stringr function
library(tm) # stripWhitespace, Corpus, DocumentTermMatrix function
library(gutenbergr) # gutenberg_works function
library(textstem) # lemmatize_strings function
library(caret) # stratified splitting function
library(naivebayes) # multinominal nb
# Create a pre-processing function to pre-process books.
preprocessing <- function(book, label) {
# Establish stop words and initialize a list.
stopwordsList <- stopwords('en')
list_of_stopwords <- vector(mode="list", length=174)
for (i in 174:1) {
list_of_stopwords[175-i] <- stopwordsList[i]
}
# Establishing stop words
stopwords_regex <- paste(list_of_stopwords, collapse = '\\b|\\b')
stopwords_regex <- paste0('\\b', stopwords_regex, '\\b')
# Rename column
names(book)[1] <- 'text'
# Replace certain type of punctuation.
book <- gsub("“", "\"", book$text)
book <- gsub("â€", "\"", book)
book <- gsub("‘", "\'", book)
book <- gsub("’", "\'", book)
# Convert data frame to string for splitting.
book <- toString(book)
# Splitting and converting back to data frame for further cleaning.
book <- as.vector(strsplit(book, split="[.]|[!]|[?]"))
book <- data.frame(book)
names(book)[1] <- 'text'
# Remove non-graphic characters
book$text <- gsub("[^[:graph:]]", " ", book$text)
# Lower-casing
book$text <- tolower((book$text))
# Remove "'s" and "'ll".
book$text <- gsub("'s", "", book$text)
book$text <- gsub("'ll", "", book$text)
# Remove stop words
book$text <- stringr::str_replace_all(book$text, stopwords_regex, "")
# Remove remaining punctuation, numbers and extra white space in all rows.
book$text <- gsub("[^a-zA-Z ]", "", book$text)
book$text <- stripWhitespace(book$text)
book$text <- str_trim(book$text)
# Remove empty rows.
book <- filter(book, book$text != "")
book <- filter(book, book$text != " ")
# Lemmatization
book$text <- lemmatize_strings(book$text)
# Labeling
book$target <- label
return(book)
}
################## Shakespeare, William ##################
# View works written by Shakespeare, William
# View(gutenberg_works(author == "Shakespeare, William"))
# A Midsummer Night's Dream by William Shakespeare
book_id <- 1514
book <- gutenberg_download(book_id, meta_fields = "author")
# Remove irrelevant rows and columns
book <- book %>%
slice(-(1:88)) %>%
subset(select=-(1))
# Pre-processing
book <- preprocessing(book, 1)
# Append to data
#dataIndex <- sample(1:nrow(book), size=1000)
#data <- as.data.frame(book[dataIndex, ])
data <- book
# The Comedy of Errors by William Shakespeare
book_id <- 1504
book <- gutenberg_download(book_id, meta_fields = "author")
# Remove irrelevant rows and columns
book <- book %>%
slice(-(1:60)) %>%
subset(select=-(1))
# Pre-processing
book <- preprocessing(book, 1)
# Append to data
#dataIndex <- sample(1:nrow(book), size=1000)
#data <- rbind(data, book[dataIndex, ])
data <- rbind(data, book)
# The Tragedy of Titus Andronicus by William Shakespeare
book_id <- 1507
book <- gutenberg_download(book_id, meta_fields = "author")
# Remove irrelevant rows and columns
book <- book %>%
slice(-(1:40)) %>%
subset(select=-(1))
# Pre-processing
book <- preprocessing(book, 1)
# Append to data
#dataIndex <- sample(1:nrow(book), size=1000)
#data <- rbind(data, book[dataIndex, ])
data <- rbind(data, book)
# The Taming of the Shrew by William Shakespeare
book_id <- 1508
book <- gutenberg_download(book_id, meta_fields = "author")
# Remove irrelevant rows and columns
book <- book %>%
slice(-(1:98)) %>%
subset(select=-(1))
# Pre-processing
book <- preprocessing(book, 1)
# Append to data
#dataIndex <- sample(1:nrow(book), size=1000)
#data <- rbind(data, book[dataIndex, ])
data <- rbind(data, book)
################## Bedford-Jones, H. (Henry) ##################
# View works written by Bedford-Jones, H. (Henry)
# View(gutenberg_works(author == "Bedford-Jones, H. (Henry)"))
# Nuala O'Malley by H. Bedford-Jones
book_id <- 30979
book <- gutenberg_download(book_id, meta_fields = "author")
# Remove irrelevant rows and columns
book <- book %>%
slice(-(1:89)) %>%
subset(select=-(1))
# Pre-processing
book <- preprocessing(book, 2)
# Append to data
#dataIndex <- sample(1:nrow(book), size=1400)
#data <- rbind(data, book[dataIndex, ])
data <- rbind(data, book)
# The Mesa Trail by H. Bedford-Jones
book_id <- 35078
book <- gutenberg_download(book_id, meta_fields = "author")
# Remove irrelevant rows and columns
book <- book %>%
slice(-(1:50)) %>%
subset(select=-(1))
# Pre-processing
book <- preprocessing(book, 2)
# Append to data
#dataIndex <- sample(1:nrow(book), size=1300)
#data <- rbind(data, book[dataIndex, ])
data <- rbind(data, book)
# The Mardi Gras Mystery by H. Bedford-Jones
book_id <- 39229
book <- gutenberg_download(book_id, meta_fields = "author")
# Remove irrelevant rows and columns
book <- book %>%
slice(-(1:117)) %>%
subset(select=-(1))
# Pre-processing
book <- preprocessing(book, 2)
# Append to data
#dataIndex <- sample(1:nrow(book), size=1300)
#data <- rbind(data, book[dataIndex, ])
data <- rbind(data, book)
################## Paterson, A. B. (Andrew Barton) ##################
# View works written by the author
# View(gutenberg_works(author == "Paterson, A. B. (Andrew Barton)"))
# Three Elephant Power, and Other Stories by A. B. Paterson
book_id <- 307
book <- gutenberg_download(book_id, meta_fields = "author")
# Remove irrelevant rows and columns
book <- book %>%
slice(-(1:44)) %>%
subset(select=-(1))
# Pre-processing
book <- preprocessing(book, 3)
# Append to data
#dataIndex <- sample(1:nrow(book), size=1400)
#data <- rbind(data, book[dataIndex, ])
data <- rbind(data, book)
# An Outback Marriage: A Story of Australian Life by A. B. Paterson
book_id <- 6119
book <- gutenberg_download(book_id, meta_fields = "author")
# Remove irrelevant rows and columns
book <- book %>%
slice(-(1:43)) %>%
subset(select=-(1))
# Pre-processing
book <- preprocessing(book, 3)
# Append to data
#dataIndex <- sample(1:nrow(book), size=1400)
#data <- rbind(data, book[dataIndex, ])
data <- rbind(data, book)
# Saltbush Bill, J. P. by A. B. Paterson
book_id <- 1317
book <- gutenberg_download(book_id, meta_fields = "author")
# Remove irrelevant rows and columns
book <- book %>%
slice(-(1:25)) %>%
subset(select=-(1))
# Pre-processing
book <- preprocessing(book, 3)
# Append to data
#dataIndex <- sample(1:nrow(book), size=600)
#data <- rbind(data, book[dataIndex, ])
data <- rbind(data, book)
# The Man from Snowy River by A. B. Paterson
book_id <- 213
book <- gutenberg_download(book_id, meta_fields = "author")
# Remove irrelevant rows and columns
book <- book %>%
slice(-(1:42)) %>%
subset(select=-(1))
# Pre-processing
book <- preprocessing(book, 3)
# Append to data
#dataIndex <- sample(1:nrow(book), size=600)
#data <- rbind(data, book[dataIndex, ])
data <- rbind(data, book)
################## Dickens, Charles ##################
# View works written by the author
# View(gutenberg_works(author == "Dickens, Charles"))
# A Christmas Carol in Prose; Being a Ghost Story of Christmas by Charles Dickens
book_id <- 46
book <- gutenberg_download(book_id, meta_fields = "author")
# Remove irrelevant rows and columns
book <- book %>%
slice(-(1:34)) %>%
subset(select=-(1))
# Pre-processing
book <- preprocessing(book, 4)
# Append to data
#dataIndex <- sample(1:nrow(book), size=1000)
#data <- rbind(data, book[dataIndex, ])
data <- rbind(data, book)
# A Tale of Two Cities by Charles Dickens
book_id <- 98
book <- gutenberg_download(book_id, meta_fields = "author")
# Remove irrelevant rows and columns
book <- book %>%
slice(-(1:75)) %>%
subset(select=-(1))
# Pre-processing
book <- preprocessing(book, 4)
# Append to data
#dataIndex <- sample(1:nrow(book), size=1000)
#data <- rbind(data, book[dataIndex, ])
data <- rbind(data, book)
# The Mystery of Edwin Drood by Charles Dickens
book_id <- 564
book <- gutenberg_download(book_id, meta_fields = "author")
# Remove irrelevant rows and columns
book <- book %>%
slice(-(1:41)) %>%
subset(select=-(1))
# Pre-processing
book <- preprocessing(book, 4)
# Append to data
#dataIndex <- sample(1:nrow(book), size=1000)
#data <- rbind(data, book[dataIndex, ])
data <- rbind(data, book)
# Barnaby Rudge: A Tale of the Riots of 'Eighty by Charles Dickens
book_id <- 917
book <- gutenberg_download(book_id, meta_fields = "author")
# Remove irrelevant rows and columns
book <- book %>%
slice(-(1:21)) %>%
subset(select=-(1))
# Pre-processing
book <- preprocessing(book, 4)
# Append to data
#dataIndex <- sample(1:nrow(book), size=1000)
#data <- rbind(data, book[dataIndex, ])
data <- rbind(data, book)
# Remove variables that are not used anymore
rm(book)
rm(book_id)
#rm(dataIndex)
rm(preprocessing)
################## Training & Testing ##################
# Create a Corpus
docs <- Corpus(VectorSource(data$text))
# Bags-of-words
dtm <- DocumentTermMatrix(docs)
dtm <- removeSparseTerms(dtm, 0.999)
data_raw <- data.frame(as.matrix(dtm))
data_raw$target <- data$target
# Splitting data into train and test set
trainIndex <- createDataPartition(data_raw$target,
p = .7,
list = FALSE)
train <- data_raw[ trainIndex,]
test <- data_raw[-trainIndex,]
# Model (Naive Bayes)
mnb <- multinomial_naive_bayes(x = as.matrix(train),
y = as.factor(train$target),
laplace = 1)
summary(mnb)
mnb.predict <- predict(mnb, newdata=as.matrix(test), type="class")
# Confusion matrix
table(mnb.predict, test$target)