This is an initail report for the Capstone Course for Johns Hopkins’s Data Scientist Certificate Program. The goal of this task is to understand the basic relationships in the data and prepare to build a predictive input model, we have been supplied with a dataset from SwiftKey containing twitter, news and blog data in four languages. For this report we’ll focus on the English documnets.
In the following package we will require the following packages: stringi, ggplot2, tm, wordcloud, and RWeka.
The readLines function from base R can be used to read in the data.
#read in blogs, twitter and news
Blogs1 <- readLines("C:/Users/KUIPERS/Desktop/RStudio/datasciencecoursera/NLP/final/en_US/en_US.blogs.txt", encoding = "UTF-8", skipNul = TRUE, warn = FALSE)
Twitter1 <- readLines("C:/Users/KUIPERS/Desktop/RStudio/datasciencecoursera/NLP/final/en_US/en_US.twitter.txt",encoding = "UTF-8", skipNul = TRUE, warn = FALSE)
News1 <- readLines("C:/Users/KUIPERS/Desktop/RStudio/datasciencecoursera/NLP/final/en_US/en_US.news.txt", encoding = "UTF-8", skipNul = TRUE, warn = FALSE)
length(Blogs1)
## [1] 899288
length(Twitter1)
## [1] 2360148
length(News1)
## [1] 77259
Here we perform an exploratory analysis of the data, understanding the distribution of words and relationship between the words in the corpora. Since the data is large, we will use a 10% sample from each file.
summary(nchar(Blogs1))
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1 47 156 230 329 40833
qplot(nchar(Blogs1),main = "Blogs",xlab="No. of Words",ylab="Frequency",binwidth=10)
summary(nchar(Twitter1))
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 2.00 37.00 64.00 68.68 100.00 140.00
qplot(nchar(Twitter1),main = "Twitter",xlab="No. of Words",ylab="Frequency",binwidth=10)
summary(nchar(News1))
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 2.0 111.0 186.0 202.4 270.0 5760.0
qplot(nchar(News1),main = "News",xlab="No. of Words",ylab="Frequency",binwidth=10)
Notice that that the News and Blogs documents are highly skewed in terms of the number of characters per line, with just a small percentage of lines with a large number of characters. Twitter has a maximum of 140 characters per line.
Since this is a computationally intensive process, we will use only a random sample of .1 percent of the data to create ngrams. We set a seed to ensure the random sample of the data provides exactly the same data each time the code is run.
set.seed(123)
Blogs2 <- sample(Blogs1, size=length(Blogs1)*.001, replace=FALSE)
Twitter2 <- sample(Twitter1, size=length(Twitter1)*.001, replace=FALSE)
News2 <- sample(News1, size=length(News1)*.001, replace=FALSE)
data1 = c(Blogs2, Twitter2, News2)
Create a corpus, which is a collection of text documents:
data2 <- sapply(data1, function(x) iconv(enc2utf8(x), sub = "byte"))
data2 <- (data2[!is.na(data2)])
#Convert to a Volatile Corpus which realizes a semantics as known from most R objects and then conduct some basic data cleaning
Corpus1 <- VCorpus(VectorSource(data2))
Corpus1 <- tm_map(Corpus1, tolower)
Corpus1 <- tm_map(Corpus1, removePunctuation)
Corpus1 <- tm_map(Corpus1, removeNumbers)
Corpus1 <- tm_map(Corpus1, stripWhitespace)
Corpus1 <- tm_map(Corpus1, PlainTextDocument)
#UniTokens, BiTokens and TriTokens
uniToken <- function(x) NGramTokenizer(x, Weka_control(min = 1, max = 1))
biToken <- function(x) NGramTokenizer(x, Weka_control(min = 2, max = 2))
uniTm <- TermDocumentMatrix(Corpus1, control = list(tokenize = uniToken))
UniTm <- removeSparseTerms(uniTm, 0.99)
biTm <- TermDocumentMatrix(Corpus1, control = list(tokenize = biToken))
biTm <- removeSparseTerms(biTm, 0.99)
Freq1 <- sort(rowSums(as.matrix(uniTm)), decreasing = TRUE)
uniDF <- data.frame(word = names(Freq1), freq = Freq1)
wordcloud(words = uniDF$word, freq = uniDF$freq, max.words=200,
random.order=FALSE, rot.per=0.1, use.r.layout=FALSE, ordered.colors=FALSE, colors=brewer.pal(6, "Dark2"), main = "Word Cloud")
# The most common words(unigrams)
head(uniDF,20)
## word freq
## the the 2936
## and and 1613
## for for 834
## you you 824
## that that 681
## with with 454
## was was 437
## have have 407
## this this 406
## are are 357
## but but 333
## not not 303
## just just 282
## all all 268
## your your 251
## from from 232
## what what 231
## will will 229
## one one 224
## out out 220
# The most common word pairs (bigrams)
Freq2 <- sort(rowSums(as.matrix(biTm)), decreasing = TRUE)
biDF <- data.frame(word = names(Freq2), freq = Freq2)
head(biDF, 20)
## word freq
## of the of the 261
## in the in the 239
## for the for the 157
## to the to the 144
## on the on the 143
## to be to be 100
## at the at the 90
## i have i have 86
## and the and the 85
## and i and i 82
## is a is a 78
## i was i was 76
## have a have a 75
## want to want to 68
## i am i am 67
## it was it was 67
## for a for a 66
## if you if you 64
## in a in a 63
## thanks for thanks for 61
In the future I plan will be to:
More effectively clean and organize corpus and code
Find a way to create more efficent code, I expect this involves finding a way to get the readtext and quanteda vignette package to work properly.
Develop a predictive model