-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.R
More file actions
155 lines (140 loc) · 4.64 KB
/
utility.R
File metadata and controls
155 lines (140 loc) · 4.64 KB
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
get_smaller_data <- function(org_filename, new_filename, keep) {
read_con <- file(org_filename, "rt")
write_con <- file(new_filename, "wt")
while (TRUE) {
lines <- readLines(read_con, 50000) # read 50000 line at a time
if (length(lines) < 1) {
break
} else {
prob <- runif(length(lines))
keep_indices <- which(prob < keep)
lines <- lines[keep_indices]
# retaining data for further analysis
writeLines(lines, write_con)
}
}
# close connections
close(read_con)
close(write_con)
}
count_num_lines <- function(filename) {
num_lines <- 0
read_con <- file(filename, "rt")
while (TRUE) {
lines <- readLines(read_con, 50000) # read 50000 line at a time
if (length(lines) < 1) {
break
} else {
num_lines <- num_lines + length(lines)
}
}
# close connections
close(read_con)
num_lines
}
count_words <- function(filename) {
count <- 0
read_con <- file(filename, "rt")
while (TRUE) {
lines <- readLines(read_con, 50000) # read 50000 line at a time
if (length(lines) < 1) {
break
} else {
count <- count + length(unlist(strsplit(lines, split = " ")))
}
}
# close connections
close(read_con)
count
}
# preprocessing data
preproccess <- function(input_file, output_file) {
print(input_file)
read_con <- file(input_file, "rt")
write_con <- file(output_file, "wt")
# get the list of bad words in English
profane_words <- read.csv(file = "en_bad_words.txt", header = FALSE)[, 1]
while (TRUE) {
lines <- readLines(read_con, 50000) # read 50000 line at a time
if (length(lines) < 1) {
break
} else {
lines <- gsub("[[:punct:]]", "", lines) # remove all punctuation in text
lines <- gsub("[[:digit:]]", "", lines) # remove all digits in text
lines <- tolower(lines) # convert all text into lower case
#lines <- gsub("[^[:alnum:]]", " ", lines) # remove all numeric or alphabet character
for (i in 1:((length(profane_words) / 100)+1)) {
pattern <- paste0("[", paste(profane_words[((i-1)*100 + 1) :
min(i * 100, length(profane_words))],
collapse = "|"),"]")
print(i)
lines <- gsub(pattern, "", lines)# remove profane words
}
lines <- gsub(" ", " ", lines) # remove extra space
writeLines(lines, write_con)
}
}
# close connections
close(read_con)
close(write_con)
}
# get n-grams distribution from the list of text files
get_ngrams_dist <- function(file_list, n) {
word_dist <- data.frame(ngram = c(), freq = c())
for (input_file in file_list) {
#print(input_file)
read_con <- file(input_file, "rt")
while (TRUE) {
lines <- readLines(read_con, 50000) # read 50000 line at a time
if (length(lines) < 1) {
break
} else {
word_count <- data.frame(as.table(textcnt(lines, n, method = "string")))
colnames(word_count) <- c("ngram", "freq")
word_dist <- rbind(word_dist, word_count)
word_dist <- aggregate(freq~ngram,data=word_dist,FUN=sum)
}
}
# close connections
close(read_con)
}
word_dist
}
# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols: Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
# then plot 1 will go in the upper left, 2 will go in the upper right, and
# 3 will go all the way across the bottom.
#
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
library(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots = length(plots)
# If layout is NULL, then use 'cols' to determine layout
if (is.null(layout)) {
# Make the panel
# ncol: Number of columns of plots
# nrow: Number of rows needed, calculated from # of cols
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
ncol = cols, nrow = ceiling(numPlots/cols))
}
if (numPlots==1) {
print(plots[[1]])
} else {
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
# Make each plot, in the correct location
for (i in 1:numPlots) {
# Get the i,j matrix positions of the regions that contain this subplot
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
layout.pos.col = matchidx$col))
}
}
}