-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prediction.R
63 lines (41 loc) · 1.96 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
library(quanteda)
library(data.table)
library(readtext)
Prediction <- function(inputFile, rmv = 1) {
## This function makes the prediction look up table
## inputFile: the ngram csv file generated from quanteda
## rmv: threshold to remove low frequency words (default is 1)
nGram <- fread(inputFile, select = c('feature', 'frequency'))
nGram <- nGram[nGram$frequency > rmv]
nGram <- nGram[, query := strsplit(feature, " [^ ]+$")][]
nGram <- nGram[, predict := sub('.* (.*)$','\\1', feature)][]
fwrite(nGram, paste0(sub('.csv', '', inputFile), 'Pred.csv'))
}
# Generate nGrams (n = 2:7)
Prediction('/Users/shovitraj/Data/nGram/uniGram.csv')
Prediction('/Users/shovitraj/Data/nGram/biGram.csv')
Prediction('/Users/shovitraj/Data/nGram/triGram.csv')
Prediction('/Users/shovitraj/Data/nGram/quadGram.csv')
Prediction('/Users/shovitraj/Data/nGram/quintGram.csv')
Prediction('/Users/shovitraj/Data/nGram/sexGram.csv')
Prediction('/Users/shovitraj/Data/nGram/septGram.csv')
uni<- read.csv('uniGramPred.csv')
bi<- read.csv('biGramPred.csv')
tri<- read.csv('triGramPred.csv')
quad<- read.csv('quadGramPred.csv')
quint<- read.csv('quintGramPred.csv')
six<- read.csv('sexGramPred.csv')
sept<- read.csv('septGramPred.csv')
nGramPrediction <- rbind(uni, bi, tri, quad, quint, six, sept )
dim(nGramPrediction)
names(nGramPrediction)
write.csv(nGramPrediction, file = 'nGramPrediction.csv', row.names = F)
# Read in all predictions
nGram <- fread('nGramPrediction.csv', select = c('query', 'predict', 'frequency'))
nGram <- nGram[order(-frequency)]
# Filter out frequency < 5 word combinations to avoid most common short words like "the"
nGram_filter <- nGram[frequency >= 5]
fwrite(nGram_filter, file = 'predictionTable.csv')
# Filter the unique queries and frequency >=5
nGramUnique <- nGram[(!duplicated(nGram$query)) & (frequency >= 5)]
fwrite(nGramUnique, file = 'predictionTableUnique.csv')