/
sandbox.R
219 lines (179 loc) · 6.72 KB
/
sandbox.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
library(tm)
library(SnowballC)
library(ggplot2)
library(wordcloud)
library(cluster)
library(fpc)
fetch_capstone_data <- function() {
data_dir = "./data"
zipfile <- file.path("data", "Coursera-SwiftKey.zip")
file_url <- "https://d396qusza40orc.cloudfront.net/dsscapstone/dataset/Coursera-SwiftKey.zip"
unzipped_data = "data/final"
# check for data zip
if (!file.exists(data_dir)){
print(sprintf("Creatiing dir: %s", data_dir))
dir.create(data_dir)
}
# check for zip and download the data
if (!file.exists(zipfile)) {
print(sprintf("Downloading data: %s", zipfile))
download.file(file_url, destfile=zipfile, method = "curl")
}
# unzip the data
if(!file.exists(unzipped_data)){
print(sprintf("Unzipping data: %s", zipfile))
unzip(zipfile, exdir = data_dir)
}
list.files(data_dir)
}
add_meta_data_to_docs <- function(tm_corpus, tag, value) {
i <- 1
tm_corpus <- tm_map(tm_corpus, function(x) {
meta(x, tag=tag) <- value
x
})
tm_corpus
}
newline_text_file_to_corpus <- function(filename,
nlines=10) {
lines <- scan(file=filename, what="", sep="\n", nlines = nlines)
t_corpus <- Corpus(VectorSource(lines))
t_corpus
}
load_all_data <- function(nlines=10) {
tweets <- newline_text_file_to_corpus(filename="./data/final/en_US/en_US.twitter.txt",
nlines=nlines)
tweets <- preprocess_entries(tweets)
tweets <- add_meta_data_to_docs(tweets, "doc_type", "twitter")
blogs <- newline_text_file_to_corpus(filename="./data/final/en_US/en_US.blogs.txt",
nlines=nlines)
blogs <- preprocess_entries(blogs)
blogs <- add_meta_data_to_docs(blogs, "doc_type", "blog")
news <- newline_text_file_to_corpus(filename="./data/final/en_US/en_US.news.txt",
nlines=nlines)
news <- add_meta_data_to_docs(news, "doc_type", "news")
news <- preprocess_entries(blogs)
docs <- c(tweets, blogs, news)
docs
}
preprocess_entries <- function(docs) {
docs <- tm_map(docs, removePunctuation) # *Removing punctuation:*
docs <- tm_map(docs, removeNumbers) # *Removing numbers:*
docs <- tm_map(docs, tolower) # *Converting to lowercase:*
docs <- tm_map(docs, removeWords, stopwords("english")) # *Removing "stopwords"
docs <- tm_map(docs, stemDocument) # *Removing common word endings* (e.g., "ing", "es")
docs <- tm_map(docs, stripWhitespace) # *Stripping whitespace
docs <- tm_map(docs, PlainTextDocument)
docs
}
explore_data <- function(docs) {
dtm <- DocumentTermMatrix(docs)
print("Using findFreqTerms")
print(findFreqTerms(dtm, lowfreq=20))
freq <- colSums(as.matrix(dtm))
ord <- order(freq)
print("most frequent")
print(freq[tail(ord, n=10)])
# dtms <- removeSparseTerms(dtm, 0.01)
}
plot_word_frequencies <- function(dtm) {
print("using colsums/head")
freq <- sort(colSums(as.matrix(dtm)), decreasing=TRUE)
head(freq, 14)
wf <- data.frame(word=names(freq), freq=freq)
print(head(wf))
p <- ggplot(subset(wf, freq>15), aes(word, freq))
p <- p + geom_bar(stat="identity")
p <- p + theme(axis.text.x=element_text(angle=45, hjust=1))
p
}
plot_wordcloud <- function(dtm) {
freq <- sort(colSums(as.matrix(dtm)), decreasing=TRUE)
set.seed(142)
wordcloud(names(freq), freq, min.freq=10, scale=c(5, .1), colors=brewer.pal(6, "Dark2"))
}
plot_wordcloud_top_n <- function(dtm, max.words=15) {
freq <- sort(colSums(as.matrix(dtm)), decreasing = TRUE)
set.seed(142)
dark2 <- brewer.pal(6, "Dark2")
wordcloud(names(freq), freq, max.words=max.words, rot.per=0.2, colors=dark2)
}
hierarchical_cluster <- function(dtm) {
d <- dist(t(dtm), method="euclidian")
fit <- hclust(d=d, method="ward")
fit
plot(fit, hang=-1)
}
kmeans_plot <- function(dtm) {
d <- dist(t(dtm), method="euclidian")
kfit <- kmeans(d, 2)
clusplot(as.matrix(d), kfit$cluster, color=T, shade=T, labels=2, lines=0)
}
# test code
test_read_tweets <- function() {
lines <- scan(file="./data/final/en_US/en_US.twitter.txt", what="", sep="\n", nlines = 10)
# only the first 10 lines
t_corpus <- Corpus(VectorSource(lines))
t_corpus[[1]]$content
# stemming
t_corpus <- tm_map(t_corpus, stemDocument)
t_corpus[[1]]$content
# "How are you? Btw thank for the RT. You gonna be in DC anytim soon? Love to see you. Been way, way too long."
# lower case
t_corpus <- tm_map(t_corpus, tolower)
t_corpus[[1]]
# grep/search
tm_filter(t_corpus,
FUN = function(x) any(grep("RT", content(x))))[[1]]$content
# document term matrix
dtm <- DocumentTermMatrix(t_corpus)
inspect(dtm)
# dictionary
}
test_reuters <- function() {
# dictionary
reut21578 <- system.file("texts", "crude", package = "tm")
reuters <- VCorpus(DirSource(reut21578),
readerControl = list(reader = readReut21578XMLasPlain))
inspect(DocumentTermMatrix(reuters,
list(dictionary = c("prices", "crude", "oil"))))
}
# quiz #1
find_longest_line <- function(){
max(sapply(readLines("./data/final/en_US/en_US.twitter.txt"), nchar))
max(sapply(readLines("./data/final/en_US/en_US.blogs.txt"), nchar))
max(sapply(readLines("./data/final/en_US/en_US.news.txt"), nchar))
}
twitter_word_count_love_hate <- function() {
# lines <- scan(file="./data/final/en_US/en_US.twitter.txt", what="", sep="\n")
lines <- readLines("./data/final/en_US/en_US.twitter.txt")
t_corpus <- Corpus(VectorSource(lines))
t_corpus <- tm_map(t_corpus, tolower)
t_corpus <- tm_map(t_corpus, PlainTextDocument)
has_love <- tm_filter(t_corpus,
FUN = function(x) any(grep("love", content(x))))
has_hate <- tm_filter(t_corpus,
FUN = function(x) any(grep("hate", content(x))))
length(has_love) / length(has_hate)
# has biostats
has_love <- tm_filter(t_corpus,
FUN = function(x) any(grep("biostats", content(x))))[[1]]$content
}
hacking_with_weka <- function() {
NGramTokenizer("a b a c a b b", Weka_control(min = 2, max = 2))
NGramTokenizer(PlainTextDocument("a b a c a b b"), Weka_control(min = 2, max = 2))
PlainTextDocument("a b a c a b b")
content(PlainTextDocument("a b a c a b b"))
}
# twitter_word_count_phrase <- function() {
#
# }
get_head_tail_of_ngram <- function() {
# Datums after pruning
datums$df_ngram_2 <- readRDS("data/pruned_50p_term_doc_matrix_2_ngram_df.rds")
datums$df_ngram_2 <- mutate(datums$df_ngram_2,
word=as.character(word),
words=strsplit(word, " "),
root_word=head(words, 1),
rest=tail(words, -1))
}