The aim of the milestone report was to examine and provide some basic statistical summaries of the numerous internet data sources provided which included blogs, tweets and news. Once the intial exploratory data analysis gave us an idea of the contents withing, the next steps involved data cleaning and sampling for the test set. Once the data was free of noise, most frequent word combinations (ngrams) were constructed and visualized, these word combinations would then be utilized in the creation of the prediction model.
library(dplyr)
library(stringi)
library(tm)
library(RWeka)
library(ggplot2)
news<-'D:/Documents/aao1009/Capstone Project/final/en_US/en_US.news.txt'
blogs<-'D:/Documents/aao1009/Capstone Project/final/en_US/en_US.blogs.txt'
twitter<-'D:/Documents/aao1009/Capstone Project/final/en_US/en_US.twitter.txt'
blog_size_MB<-file.size(blogs)/(1024^2)
bcon<-file(blogs, open = "r")
blines<-readLines(bcon, encoding = "UTF-8")
total_blog_characters<-sum(nchar(blines))
blog_word_count<-sum(stri_count_words(blines))
close(bcon)
twitter_size_MB<-file.size(twitter)/(1024^2)
tcon<-file(twitter, open = "r")
tlines<-readLines(tcon, encoding ="UTF-8")
total_twitter_characters<-sum(nchar(tlines))
twitter_word_count<-sum(stri_count_words(tlines))
close(tcon)
news_size_MB<-file.size(news)/(1024^2)
ncon<-file(news, open = "r")
nlines<-readLines(ncon, encoding = "UTF-8")
total_news_characters<-sum(nchar(nlines))
news_word_count<-sum(stri_count_words(nlines))
close(ncon)
## blog_size_MB total_blog_characters blog_word_count
## 1 200.4242 206824505 37546239
## twitter_size_MB total_twitter_characters twitter_word_count
## 1 159.3641 162096031 30093372
## news_size_MB total_news_characters news_word_count
## 1 196.2775 15639408 2674536
1% of each data type was used in order to create a training set
set.seed(100)
stwitter<-sample(tlines, 0.01*length(tlines), replace=FALSE)
snews<-sample(nlines, 0.01*length(nlines), replace=FALSE)
sblogs<-sample(blines, 0.01*length(blines), replace=FALSE)
The sampled data was used to create a corpus with several transformations such as removal of the non text characters, stopwords and white spaces
corp<-VCorpus(VectorSource(c(sblogs, snews, stwitter)), readerControl=list(reader=readPlain,language="en"))
corp<- VCorpus(VectorSource(sapply(corp, function(row) iconv(row, "latin1", "ASCII", sub=""))))
corp<-tm_map(corp, removePunctuation)
corp<-tm_map(corp, stripWhitespace)
corp<-tm_map(corp, content_transformer(tolower))
corp<-tm_map(corp, removeNumbers)
corp<-tm_map(corp, PlainTextDocument)
corp<-tm_map(corp, removeWords, stopwords("english"))
To explore word frequencies, N-grams models were created using the RWeka package
UniTokenizer<- function(x) NGramTokenizer(x, Weka_control(min = 1, max = 1))
BiTokenizer<- function(x) NGramTokenizer(x, Weka_control(min = 2, max = 2))
TriTokenizer<- function(x) NGramTokenizer(x, Weka_control(min = 3, max = 3))
Uni<- TermDocumentMatrix(corp, control = list(tokenize = UniTokenizer))
Bi<- TermDocumentMatrix(corp, control = list(tokenize = BiTokenizer))
Tri<- TermDocumentMatrix(corp, control = list(tokenize = TriTokenizer))
freq_frame <- function(tdm){
freq <- sort(rowSums(as.matrix(tdm)), decreasing=TRUE)
freq_frame <- data.frame(word=names(freq), freq=freq)
return(freq_frame)
}
UniDense <- removeSparseTerms(Uni, 0.999)
UniDenseSorted <- freq_frame(UniDense)
BiDense <- removeSparseTerms(Bi, 0.999)
BiDenseSorted <- freq_frame(BiDense)
TriDense <- removeSparseTerms(Tri, 0.999)
TriDenseSorted <- freq_frame(TriDense)
Combinations of words were then visualized, displaying top 20 unigrams and bigrams
Top20Uni <- ggplot(data = UniDenseSorted[1:20,], aes(x = reorder(word, -freq), y = freq)) + geom_bar(stat="identity")
Top20Uni <- Top20Uni + labs(x = "N-gram", y = "Word Frequency", title = "Top 20 Unigrams")
Top20Uni <- Top20Uni + theme(axis.text.x=element_text(angle=90))
Top20Uni
Top20Bi <- ggplot(data = BiDenseSorted[1:20,], aes(x = reorder(word, -freq), y = freq)) + geom_bar(stat="identity")
Top20Bi <- Top20Bi + labs(x = "N-gram", y = "Word Frequency", title = "Top 20 Bigrams")
Top20Bi <- Top20Bi + theme(axis.text.x=element_text(angle=90))
Top20Bi
Word combination frequency seems to decline, as more words are added into the equation, for example frequencies for individual words are much higher than bigrams.
The newly constructed corpus of word combinations will be used for predicting the next word in a sequence. For the creation of the app, I would use a greater sample size, in order to increse the predictive power and accuracy. Furthermore, longer word combinations would be explored using trigrams and quadragrams. Once the model is working as intended, processing speed would be tested to ensure a response is provided within a resonable time.