-
Notifications
You must be signed in to change notification settings - Fork 0
/
prediction.R
136 lines (114 loc) · 4.58 KB
/
prediction.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
loadNgrams <- function(dir, updateProgress = NULL, callback = NULL) {
types <- c("blogs", "news", "tweets")
uni <- rbind(
preprocessGram(paste(dir, "/", types[1], "-unigram.RData", sep = "")),
preprocessGram(paste(dir, "/", types[2], "-unigram.RData", sep = "")),
preprocessGram(paste(dir, "/", types[3], "-unigram.RData", sep = ""))
)
if (is.function(updateProgress)) {
updateProgress(detail = "Loaded unigrams")
}
bi <- rbind(
preprocessGram(paste(dir, "/", types[1], "-bigram.RData", sep = "")),
preprocessGram(paste(dir, "/", types[2], "-bigram.RData", sep = "")),
preprocessGram(paste(dir, "/", types[3], "-bigram.RData", sep = ""))
)
if (is.function(updateProgress)) {
updateProgress(detail = "Loaded bigrams")
}
tri <- rbind(
preprocessGram(paste(dir, "/", types[1], "-trigram.RData", sep = "")),
preprocessGram(paste(dir, "/", types[2], "-trigram.RData", sep = "")),
preprocessGram(paste(dir, "/", types[3], "-trigram.RData", sep = ""))
)
if (is.function(updateProgress)) {
updateProgress(detail = "Loaded trigrams")
}
tetra <- rbind(
preprocessGram(paste(dir, "/", types[1], "-tetragram.RData", sep = "")),
preprocessGram(paste(dir, "/", types[2], "-tetragram.RData", sep = "")),
preprocessGram(paste(dir, "/", types[3], "-tetragram.RData", sep = ""))
)
if (is.function(updateProgress)) {
updateProgress(detail = "Loaded tetragrams")
}
penta <- rbind(
preprocessGram(paste(dir, "/", types[1], "-pentagram.RData", sep = "")),
preprocessGram(paste(dir, "/", types[2], "-pentagram.RData", sep = "")),
preprocessGram(paste(dir, "/", types[3], "-pentagram.RData", sep = ""))
)
if (is.function(updateProgress)) {
updateProgress(detail = "Loaded pentagrams")
}
if (is.function(callback)) {
callback()
}
}
preprocessGram <- function(fileName) {
n <- as.data.frame(readRDS(fileName), stringsAsFactors = FALSE)
s <- strsplit(as.character(n[1,1]), split = " ")[[1]]
names <- c()
for (i in 1:length(s)) {
names <- c(names, i)
}
if (length(s) > 1) {
n <- cbind(colsplit(n$ngram, split = " ", names = names), n$count)
}
colnames(n) <- c(names, "count")
n
}
sanitize <- function(text) {
corpus <- Corpus(VectorSource(text))
rm(text)
## Replace some special characters with a space
toSpace <- content_transformer(function(x, pattern) {
gsub(pattern, " ", x)
})
corpus <- tm_map(corpus, toSpace, "/|@|\\|")
## convert to lower case, remove numbers and punctuation
corpus <- tm_map(corpus, content_transformer(tolower))
## remove stopwords before punctuation removed
## corpus <- tm_map(corpus, removeWords, stopwords("english"))
## remove numbers, punctuation, and whitespace
corpus <- tm_map(corpus, removeNumbers)
corpus <- tm_map(corpus, removePunctuation)
corpus <- tm_map(corpus, stripWhitespace)
## Stem the document with SnowballC
corpus <- tm_map(corpus, stemDocument)
corpus
}
predict <- function(phrase, uni, bi, tri, tetra, penta) {
phrase <- sanitize(phrase)[[1]]$content
words <- strsplit(x = phrase, split = " ")[[1]]
length <- length(words)
matches <- data.frame()
if (length == 0) {
"NA"
}
if (length >= 1) {
## match against bi words and take last two columns
bi_matches <- filter(bi, bi["1"] == words[length])[,c(2:3)] %>% mutate("source" = "bi")
colnames(bi_matches) <- c("prediction", "count", "source")
matches <- rbind(matches, bi_matches)
}
if (length >= 2) {
## match against tri and take the last two columns
tri_matches <- filter(tri, tri["1"] == words[length - 1], tri["2"] == words[length])[,c(3:4)] %>% mutate("source" = "tri")
colnames(tri_matches) <- c("prediction", "count", "source")
matches <- rbind(matches, tri_matches)
}
if (length >= 3) {
## match against tetra and take the last two columns
tetra_matches <- filter(tetra, tetra["1"] == words[length - 2], tetra["2"] == words[length - 1], tetra["3"] == words[length])[,c(4:5)] %>% mutate("source" = "tetra")
colnames(tetra_matches) <- c("prediction", "count", "source")
matches <- rbind(matches, tetra_matches)
}
if (length >= 4) {
penta_matches <- filter(penta, penta["1"] == words[length - 3], penta["2"] == words[length - 2], penta["3"] == words[length - 1], penta["4"] == words[length])[,c(5:6)] %>% mutate("source" = "penta")
colnames(penta_matches) <- c("prediction", "count", "source")
matches <- rbind(matches, penta_matches)
}
r <- matches %>% group_by(prediction) %>% summarise(count = sum(count))
colnames(r) <- c("prediction", "count")
r
}