Now that we have a cleansed data, it’s time to tokenize the words. We would identify appropriate tokens such as words, punctuation, and numbers. Then we structure the words for auto suggestion.
As suggested in the course content, writing a function for N-Grams that takes size and returns structured data set.
## nGrams function
nGramFn <- function(corpusData, ng) {
options(mc.cores = 1)
nGramTokenizer <- function(nData) RWeka::NGramTokenizer(nData, Weka_control(min = ng,
max = ng, delimiters = " \\r\\n\\t.,;:\"()?!"))
tdMatrix <- TermDocumentMatrix(corpusData, control = list(tokenize = nGramTokenizer,
removePunctuation = TRUE, stopwords = TRUE))
tdMatrix <- as.data.frame(apply(tdMatrix, 1, sum))
summary(tdMatrix)
# colnames(tdMatrix) <- c('Frequency')
return(tdMatrix)
}
## Filter Dataframe
filterData <- function(nDataFrame) {
nDataFrame <- as.data.frame(cbind(rownames(nDataFrame), nDataFrame[, 1]))
colnames(nDataFrame) <- c("Word", "Frequency")
nDataFrame <- nDataFrame[order(nDataFrame$Frequency, decreasing = TRUE),
]
print(head(nDataFrame))
nDataFrame <- nDataFrame[1:10, ]
return(nDataFrame)
}
## Write the function inside nGramFn() again since it cannot be found during
## knitting, but no error if run chunk-by-chunk.
suppressAll(library("RWeka"))
corpusData = llply(corpus, sample, size = 100)
nGramTokenizer <<- function(nData) NGramTokenizer(nData, Weka_control(min = ng,
max = ng, delimiters = " \\r\\n\\t.,;:\"()?!"))
tdMatrix <- TermDocumentMatrix(corpusData, control = list(tokenize=nGramTokenizer,
removePunctuation = TRUE,
stopwords = TRUE))
tdMatrix <- as.data.frame(apply(tdMatrix, 1, sum))
summary(tdMatrix)
nGram1 <- llply(corpusData, function(x) {
nGramFn(x, ng = 1)
})
nGram2 <- llply(corpusData, function(x) {
nGramFn(x, ng = 2)
})
nGram3 <- llply(corpusData, function(x) {
nGramFn(x, ng = 3)
})
nG1 <- llply(nGram1, function(x) {
filterData(x)
})
Word Frequency
268 das 9
632 heut 9
845 mal 9
596 gute 8
668 immer 8
1130 sie 8
Word Frequency
197 beim 9
1515 schon 9
1777 und 9
342 dabei 8
525 erklärt 8
557 euro 8
Word Frequency
226 heut 6
80 dass 5
177 geht 5
103 echt 4
199 glaub 4
250 ist 4
nG2 <- llply(nGram2, function(x) {
filterData(x)
})
Word Frequency
887 ich mag 4
894 ich weiß 3
277 cruz de 2
324 dass ihr 2
326 dass jemand 2
342 de tenerif 2
Word Frequency
806 es gibt 4
538 deutsch kunden 3
1633 milliarden euro 3
64 am freitag 2
76 ämter gehalt 2
211 bayern münchen 2
Word Frequency
169 freude am 2
260 ich freu 2
369 mal ne 2
485 schon mal 2
593 vielen dank 2
1 abend neue 1
nG3 <- llply(nGram3, function(x) {
filterData(x)
})
Word Frequency
261 cruz de tenerif 2
1389 santa cruz de 2
1 a die prinzipien 1
2 a la neil 1
3 aaaaber diesem dackelkind 1
4 ab sozialspiel das 1
Word Frequency
76 ämter gehalt festanstellung 2
303 betrieblich zusatzrente dienstwagen 2
847 festanstellung betrieblich zusatzrente 2
965 gehalt festanstellung betrieblich 2
1161 heidi van thiel 2
1839 posten ämter gehalt 2
Word Frequency
1 abend neue abenteuer 1
2 aber vorteil überwiegen 1
3 abgechillt absprung geschafft 1
4 absprung geschafft vorm 1
5 abwechslung mal mittwoch 1
6 ach dacht wärst 1