-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepareData.R
426 lines (341 loc) · 13.5 KB
/
prepareData.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
################################################################################
# prepareData.R
#
# The data used in this project can be found at:
# https://d396qusza40orc.cloudfront.net/dsscapstone/dataset/Coursera-SwiftKey.zip
#
# On my 8-year-old Dell Precision T3500 with a Xeon 3.33 GHz 6-core
# hyperthreaded processor, 24 GB of RAM, and a WDC WD2002FAEX-007BA0 hard drive
# running under Kubuntu 16.04.3 linux, the runtimes were:
# 02h:35m:42s to process the train data
# 00h:11m:23s to process the test data
#
# Resources used in creating this code:
# https://github.com/rstudio/cheatsheets/raw/master/quanteda.pdf
# https://s3.amazonaws.com/assets.datacamp.com/blog_assets/datatable_Cheat_Sheet_R.pdf
# https://github.com/lgreski/datasciencectacontent/blob/master/markdown/capstone-ngramComputerCapacity.md
# https://www.coursera.org/learn/data-science-project/discussions/forums/bXKqKZfYEeaRew5BAmrkbw/threads/dW1Z5sKMEeeTyArhA0ZGig
library(data.table)
library(doParallel)
library(quanteda)
source("delta_t.R")
source("tokenizer.R")
# Set seed for reproducability
set.seed(222)
################################################################################
# Generic function for parallelizing any task (when possible)
# Input:
# task
# function to parallelize
# ...
# arguments for task
# Output:
# output of task with arguments ...
#
# Credit for this function goes to Eric Rodriguez
# http://rstudio-pubs-static.s3.amazonaws.com/169109_dcd8434e77bb43da8cf057971a010a56.html
parallelizeTask <- function(task, ...) {
# Calculate the number of cores
ncores <- detectCores() - 1
# Initiate cluster
cl <- makeCluster(ncores)
registerDoParallel(cl)
#print("Starting task")
r <- task(...)
#print("Task done")
stopCluster(cl)
return(r)
}
################################################################################
# Given a zip archive and pattern matching files to be extracted, this function
# reads all matching files and concatenates them. It then shuffles the data,
# and splits it into train and test sets.
# Input:
# zip_file
# name of zip archive with path relative to the directory of
# prepareData.R
# pattern
# a regex pattern of files to extract from the zip file
# Output:
# a list containing train data (element 1) and test data (element 2)
getData <- function(zip_file, pattern) {
# Create temp directory and extract files from zip archive
td <- tempdir()
# Get list of files to extract from the archive based on provided pattern
all_files <- unzip(zip_file, list=TRUE, exdir=td)
mask <- grepl(pattern, all_files$Name)
files_to_extract <- all_files[mask, ]
# Reformat Length column to be more meaningful
files_to_extract$Length <- round(files_to_extract$Length / 2^20, 4)
names(files_to_extract)[names(files_to_extract) == "Length"] <- "Size_Mb"
# Inform user of files to be extracted
print(paste0(
"Extracting these files from archive '", zip_file,
"' with pattern matching '", pattern, "':"
))
print(files_to_extract)
# Extract the files
extracted_files <- unzip(zip_file, files_to_extract$Name, exdir=td)
# Initialize output
dat <- NULL
i <- 1
# Loop over extracted files
for(next_file in extracted_files) {
# Read the next extracted file
txt <- readLines(con=next_file, encoding="UTF-8", skipNul=TRUE)
print(paste0(
files_to_extract$Name[i], " contains ", length(txt), " lines")
)
# Concatenate data
dat <- c(dat, txt)
i <- i + 1
}
# Delete extracted files
unlink(extracted_files)
print(paste0(
"Extracted data has ", length(dat), " lines and occupies ",
round(object.size(dat) / 2^20, 4), " Mb of memory"
))
# Shuffle data
dat <- dat[sample(seq(length(dat)))]
# Split data into train and train sets
# We retain 98% of the data for train because there are millions of rows,
# and 2% is more than adequate to validate
train_frac <- 0.98
print(paste0(
"Splitting data in train/test sets; fraction retained in train = ",
train_frac
))
smp_size <- floor(train_frac * length(dat))
inTrain <- sample(seq_len(length(dat)), size=smp_size)
train <- dat[inTrain]
test <- dat[-inTrain]
return(list(train=train, test=test))
}
################################################################################
# Given a number of lines in an input text file, this function creates an
# index to split the file into chunks, to aid in processing large files. The
# chunk size is hard-coded here.
# Input:
# max_idx
# the number of lines in the fule (i.e., the max index number)
# Output:
# a sequence (integer vector) from 0 to max_idx, by chunk_size
getIdx <- function(max_idx) {
# Chunk sizes of 10^5 rows are small enough not to overwhelm the memory
# and processing constraints of most modern computers.
chunk_size <- min(100000L, max_idx)
idx <- seq(0L, max_idx, by=chunk_size)
idx[length(idx)] <- as.integer(max_idx)
return(idx)
}
################################################################################
# This function breaks input raw text data dat into chunks as specified in idx,
# generates 1-grams, 2-grams, ..., Nmax-grams from each chunk, and then saves
# the chunks to disk.
# Input:
# dat
# character vector of raw input data. Each element may contain multiple
# sentences.
# idx
# an integer vector from 0 to length(dat)
# Nmax
# maximum size of Ngrams to create
# train
# TRUE for train data, FALSE for test
# Output:
# none (data saved to disk and status messages printed to console)
analyzeChunks <- function(dat, idx, Nmax, train) {
if (train == TRUE) {
# If train, set concatenator to "_" and folder fname to 'train'
concatenator <- "_"
fname <- "train"
} else {
# If test, set set concatenator to " " and folder fname to 'test'
concatenator <- " "
fname <- "test"
}
# Loop over the values of idx: i
for(i in 1:(length(idx)-1)) {
print(paste0("Analysing chunk ", i, " of ", length(idx)-1))
# Chunk data:
# Each chuck i starts at index idx[i]+1 and ends at index idx[i+1]
qcorpus <- corpus(dat[(idx[i]+1):idx[i+1]])
sentences <- parallelizeTask(makeSentences, qcorpus)
# Loop over Ngram size: j
# Make sure directories ../data, ../data/fname/chunks,
# ../data/fname/pruned, and ../data/fname/total all exist
for (j in 1:Nmax) {
tic <- Sys.time()
# Construct j-grams
ngram <- parallelizeTask(makeTokens, sentences, j, concatenator)
# Construct document-feature matrix from ngrams
ngram_dfm <- parallelizeTask(dfm, ngram)
# Collapse ngram_dfm into a data.table with columns ngram and count,
# keyed on ngram, creating chunk i of j-grams
dts <- data.table(
ngram=featnames(ngram_dfm),
count=as.integer(colSums(ngram_dfm)),
key="ngram"
)
# Save this chunk to disk as '../data/fname/chunks/dts_j_i.rda'
file_name <-
paste0("../data/", fname, "/chunks/dts_", j, "_", i, ".rda")
save(dts, file=file_name)
# Remove objects created in this iteration to release memory
# TODO: Research: Is this step neccessary or effectual?
rm(list=c("ngram", "ngram_dfm", "dts"))
toc <- Sys.time()
print(paste0("Constructed ", j, "-gram; Saved at: ", file_name,
"; ", delta_t(tic, toc))
)
}
}
}
################################################################################
# This function combines the previously-generated Ngram chunks into total
# Ngrams, and then saves the chunks to disk. It then prunes very low-frequency
# terms from the Ngrams, splits the pruned Ngrams into X and y, and saves the
# pruned Ngrams to disk.
# Input:
# idx
# an integer vector from 0 to length(dat)
# Nmax
# maximum size of Ngrams to create
# train
# TRUE for train data, FALSE for test
# Output:
# none (data saved to disk and status messages printed to console)
combineChunks <- function(idx, Nmax, train) {
if (train == TRUE) {
# If train, set folder fname to 'train'
fname <- "train"
} else {
# If test, set folder fname to 'test'
fname <- "test"
}
# Loop over Ngram size: j
for (j in 1:Nmax) {
print(paste0("Combining ", j, "-grams"))
# Initialize output
out_dts <- NULL
# Loop over the values of idx: i
for(i in 1:(length(idx)-1)) {
print(paste0("Combining chunk ", i, " of ", length(idx)-1))
# Load 'dts_j_i.rda' (j-grams, chunk i) from disk
load(file=paste0("../data/", fname, "/chunks/dts_", j, "_", i,
".rda"))
# Combine dts for (j, i) with output, and sum any identical ngrams
out_dts <-
rbindlist(
list(out_dts, dts)
)[, lapply(.SD, sum, na.rm=TRUE), by=ngram]
}
# Rename output
dts <- out_dts
rm(list=c("out_dts"))
setkey(dts, ngram)
# Save 'dts_total_j.rda' to disk
file_name <-
paste0("../data/", fname, "/total/dts_total_", j, ".rda")
save(dts, file=file_name)
print(paste0("Saved ", file_name))
# Prune Ngrams
# Ngram frequencies follow Zipf's Law, so there's a relatively small
# number of entries with large counts. Most of the rows consist of very
# small counts, so we can achieve significant memory savings by
# truncating our Ngram tables to include only those entries with a count
# larger than 4. Don't prune the test data, though, because that will
# skew accuracy by filtering out low-frequency terms.
if (train == TRUE) {
dts <- dts[count > 4]
}
# Split Ngrams up into input (X) and prediction (y)
print("Splitting Ngrams into X and y")
if (train == TRUE) {
spl <- "_"
} else {
spl <- " "
}
if (j == 1) {
dts <- dts[, ':=' (
X="",
y=ngram
), by=ngram]
} else {
dts <- dts[, ':=' (
X=paste(
head(strsplit(ngram, split=spl)[[1]], j-1),
collapse=spl
),
y=tail(strsplit(ngram, split=spl)[[1]], 1)
), by=ngram]
}
# Original ngram column not needed and takes up a lot of memory
dts$ngram <- NULL
setkey(dts, X, y)
# Save 'dts_pruned_j.rda' to disk
file_name <-
paste0("../data/", fname, "/pruned/dts_pruned_", j, ".rda")
save(dts, file=file_name)
print(paste0("Saved ", file_name))
# Remove unneeded object to reclaim memory
rm(list=c("dts"))
}
}
################################################################################
# This is the main function to call the other functions to prepare the data.
# Input:
# train
# TRUE for train data, FALSE for test
# Output:
# none (data saved to disk and status messages printed to console)
prepareData <- function(train=TRUE) {
tic <- Sys.time()
# Maximum size of Ngrams
Nmax <- 5
# To save time, only call getData the first time we attempt analysis, and
# then save the separated train and test sets to disk.
if(file.exists("../data/dat.rda")) {
print("Loading dat.rda")
load("../data/dat.rda")
} else {
zip_file <- "../Coursera-SwiftKey.zip"
pattern <- "en_US.*.txt"
dat <- getData(zip_file, pattern)
save(dat, file="../data/dat.rda")
}
if (train == TRUE) {
# If train, set dat to train data and folder fname to 'train'
dat <- dat$train
fname <- "train"
} else {
# If test, set dat to test data and folder fname to 'test'
dat <- dat$test
fname <- "test"
}
print(paste0("Set output directory to '", fname, "'"))
idx <- getIdx(length(dat))
analyzeChunks(dat, idx, Nmax, train)
combineChunks(idx, Nmax, train)
# Package our Ngrams into a single list to make loading simpler
print("Packaging Ngrams into single list")
# Initialize output
dts_list <- vector("list", Nmax)
# Loop over Ngram size: i
for (i in 1:Nmax) {
load(paste0("../data/", fname, "/pruned/dts_pruned_", i, ".rda"))
# Set the key to (X, y)
setkey(dts, X, y)
dts_list[[i]] <- dts
rm(list=c("dts"))
}
# Rename output
dts <- dts_list
rm(list=c("dts_list"))
file_name <- paste0("../data/", fname, "/dts.rda")
save(dts, file=file_name)
print(paste0("Saved ", file_name))
toc <- Sys.time()
print(paste0("Done! ", delta_t(tic, toc)))
}