-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.R
55 lines (55 loc) · 1.95 KB
/
func.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
## Check NGrams
Check5Gram <- function(x, n5, getNrows) {
words <- whatislast (x, 4)
match <- subset(n5, word1 == words[1] & word2 == words[2]
& word3 == words[3] & word4 == words[4])
match <- subset(match, select=c(word5, freq))
match <- match[order(-match$freq), ]
sumfreq <- sum(match$freq)
match$freq <- round(match$freq / sumfreq * 100)
colnames(match) <- c("nextword","n5.MLE")
if (nrow(match) < getNrows) {
getNrows <- nrow(match)
}
match[1:getNrows,]
}
Check4Gram <- function(x, n4, getNrows) {
words <- whatislast (x, 3)
match <- subset(n4, word1 == words[1] & word2 == words[2]
& word3 == words[3])
match <- subset(match, select=c(word4, freq))
match <- match[order(-match$freq), ]
sumfreq <- sum(match$freq)
match$freq <- round(match$freq / sumfreq * 100)
colnames(match) <- c("nextword","n4.MLE")
if (nrow(match) < getNrows) {
getNrows <- nrow(match)
}
match[1:getNrows, ]
}
Check3Gram <- function(x, n3, getNrows) {
words <- whatislast (x, 2)
match <- subset(n3, word1 == words[1] & word2 == words[2])
match <- subset(match, select=c(word3, freq))
match <- match[order(-match$freq), ]
sumfreq <- sum(match$freq)
match$freq <- round(match$freq / sumfreq * 100)
colnames(match) <- c("nextword","n3.MLE")
if (nrow(match) < getNrows) {
getNrows <- nrow(match)
}
match[1:getNrows, ]
}
Check2Gram <- function(x, n2, getNrows) { # n4 df should already exist
words <- whatislast (x, 1)
match <- subset(n2, word1 == words[1])
match <- subset(match, select=c(word2, freq))
match <- match[order(-match$freq), ]
sumfreq <- sum(match$freq)
match$freq <- round(match$freq / sumfreq * 100)
colnames(match) <- c("nextword","n2.MLE")
if (nrow(match) < getNrows) {
getNrows <- nrow(match)
}
match[1:getNrows, ]
}