As the part of Data Capstone Project, this milestone report demonstrates the work done on exploratory data analysis and modeling. To get started with the Data Science Capstone Project.I’ve download the Coursera Swiftkey Dataset. After extraction, I have chosen to work with folder en_US which contains following three files:
en_US.blogs.txt en_US.news.txt en_US.twitter.txt.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(doParallel)
## Loading required package: foreach
## Loading required package: iterators
## Loading required package: parallel
library(stringi)
library(tm)
## Loading required package: NLP
library(ggplot2)
##
## Attaching package: 'ggplot2'
## The following object is masked from 'package:NLP':
##
## annotate
library(wordcloud)
## Loading required package: RColorBrewer
library(SnowballC)
jobcluster <- makeCluster(detectCores())
invisible(clusterEvalQ(jobcluster, library(tm)))
invisible(clusterEvalQ(jobcluster, library(stringi)))
invisible(clusterEvalQ(jobcluster, library(wordcloud)))
blogs <- readLines("final/en_US/en_US.blogs.txt", encoding = 'UTF-8', skipNul = TRUE)
news <- readLines("final/en_US/en_US.news.txt", encoding = 'UTF-8', skipNul = TRUE)
## Warning in readLines("final/en_US/en_US.news.txt", encoding = "UTF-8", skipNul =
## TRUE): incomplete final line found on 'final/en_US/en_US.news.txt'
twitter <- readLines("final/en_US/en_US.twitter.txt", encoding = 'UTF-8', skipNul = TRUE)
rawstats <- data.frame(
File = c("blogs","news","twitter"),
t(rbind(sapply(list(blogs,news,twitter),stri_stats_general),
TotalWords = sapply(list(blogs,news,twitter),stri_stats_latex)[4,]))
)
print(rawstats)
## File Lines LinesNEmpty Chars CharsNWhite TotalWords
## 1 blogs 899288 899288 206824382 170389539 37570839
## 2 news 77259 77259 15639408 13072698 2651432
## 3 twitter 2360148 2360148 162096241 134082806 30451170
set.seed(39)
sampleData <- list()
sampleTwitter <- twitter[sample(1:length(twitter),10000)]
sampleNews <- news[sample(1:length(news),10000)]
sampleBlogs <- blogs[sample(1:length(blogs),10000)]
sampleData <- c(sampleTwitter,sampleNews,sampleBlogs)
writeLines(sampleData, "./final/sampleData.txt")
rm(rawstats,blogs,news,twitter, sampleData, sampleBlogs, sampleNews, sampleTwitter)
directory <- file.path(".", "final")
us_files <- Corpus(DirSource(directory))
# remove more transforms
toSpace <- content_transformer(function(x, pattern) gsub(pattern, " ", x))
us_files <- tm_map(us_files, toSpace, "/|@|\\|")
# convert to lowercase
us_files <- tm_map(us_files, content_transformer(tolower))
# remove punctuation
us_files <- tm_map(us_files, removePunctuation)
# remove numbers
us_files <- tm_map(us_files, removeNumbers)
# strip whitespace
us_files <- tm_map(us_files, stripWhitespace)
# remove english stop words
us_files <- tm_map(us_files, removeWords, stopwords("english"))
# initiate stemming
us_files <- tm_map(us_files, stemDocument)
unigramTokenizer <- function(x) {
NGramTokenizer(x, Weka_control(min = 1, max = 1))
}
unigrams <- DocumentTermMatrix(us_files, control = list(tokenize = unigramTokenizer))
BigramTokenizer <- function(x) {
NGramTokenizer(x, Weka_control(min = 2, max = 2))
}
bigrams <- DocumentTermMatrix(us_files, control = list(tokenize = BigramTokenizer))
TrigramTokenizer <- function(x) {
NGramTokenizer(x, Weka_control(min = 3, max = 3))
}
trigrams <- DocumentTermMatrix(us_files, control = list(tokenize = TrigramTokenizer))
unigrams_frequency <- sort(colSums(as.matrix(unigrams)),decreasing = TRUE)
unigrams_freq_df <- data.frame(word = names(unigrams_frequency), frequency = unigrams_frequency)
head(unigrams_freq_df, 10)
## word frequency
## said said 2921
## will will 2834
## one one 2718
## like like 2392
## get get 2368
## just just 2332
## time time 2174
## can can 2040
## year year 1969
## day day 1740
Plotting unigram frequency in the histogram
unigrams_freq_df %>%
filter(frequency > 1000) %>%
ggplot(aes(reorder(word,-frequency), frequency)) +
geom_bar(stat = "identity") +
ggtitle("Unigrams with frequencies > 1000") +
xlab("Unigrams") + ylab("Frequency") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
Bigrams
bigrams_frequency <- sort(colSums(as.matrix(bigrams)),decreasing = TRUE)
bigrams_freq_df <- data.frame(word = names(bigrams_frequency), frequency = bigrams_frequency)
head(bigrams_freq_df, 10)
## word frequency
## said said 2921
## will will 2834
## one one 2718
## like like 2392
## get get 2368
## just just 2332
## time time 2174
## can can 2040
## year year 1969
## day day 1740
Plotting bigrams frequency in the histogram
bigrams_freq_df %>%
filter(frequency > 100) %>%
ggplot(aes(reorder(word,-frequency), frequency)) +
geom_bar(stat = "identity") +
ggtitle("Bigrams with frequencies > 100") +
xlab("Bigrams") + ylab("Frequency") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
Trigrams
trigrams_frequency <- sort(colSums(as.matrix(trigrams)),decreasing = TRUE)
trigrams_freq_df <- data.frame(word = names(trigrams_frequency), frequency = trigrams_frequency)
head(trigrams_freq_df, 10)
## word frequency
## said said 2921
## will will 2834
## one one 2718
## like like 2392
## get get 2368
## just just 2332
## time time 2174
## can can 2040
## year year 1969
## day day 1740
Plotting trigrams frequency in the histogram
trigrams_freq_df %>%
filter(frequency > 10) %>%
ggplot(aes(reorder(word,-frequency), frequency)) +
geom_bar(stat = "identity") +
ggtitle("Trigrams with frequencies > 10") +
xlab("Trigrams") + ylab("Frequency") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
## Prediction strategies and plans for Shiny app On the basis of analysis, I am planning to use ngram dataframe to calculate the probabilities of the next word occuring with respect to previous words. For the Shiny app, the plan is to create an app with a simple interface where the user can enter a string of text. Our prediction model will then give a list of suggested words to update the next word.