-
Notifications
You must be signed in to change notification settings - Fork 0
/
5_searchword.R
145 lines (122 loc) · 3.69 KB
/
5_searchword.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
137
138
139
140
141
142
143
144
145
library(sqldf)
library(data.table)
library(dplyr)
gc(reset=T)
answer <- searchword(wordinput)
head(answer,5)
gc()
### comparing dplyr and sqldf
load('word3c.Rda')
userinput = "love"
class(word3c)
system.time (result <- filter(word3c, grepl("^love", word3c$word) )) # data table 3-ngram 3.41sec 3.41sec
rm(result)
setDF(word3c)
gc()
class(word3c)
system.time (result <- filter(word3c, grepl("^love", word3c$word) )) #$data frame 3.55sec 3.37sec 1416902 rows
rm(result)
setDT(word3c)
gc()
class(word3c)
system.time(result <- sqldf("select * from word3c where word like 'love%'")) #data table 1.43sec 1.25sec
rm(result)
setDF(word3c)
gc()
class(word3c)
system.time(result <- sqldf("select * from word3c where word like 'love%'")) #data frame 1.30sec 1.25sec
### line below is what was used for testing the the next word query
load("word2a.Rda")
load("word2b.Rda")
load("word3a.Rda")
load("word3b.Rda")
load("word4a.Rda")
load("word4b.Rda")
load("word5a.Rda")
load("word5b.Rda")
searchword <- function(userinput)
{
###word2
wordtable <- data.frame(word=character(),count=double())
startTime <- Sys.time()
result1 <-sqldf(paste0("select * from word2a where word like '",userinput , "%'"))
if (length(result1[,1]) > 0 )
{
print(result1[1,1])
wordtable <- result1
}
result2 <- sqldf(paste0("select * from word2b where word like '",userinput , "%'"))
if (length(result2[,1]) > 0)
{
print(result2[1,1])
wordtable <- result2
}
##word3
result3 <- sqldf(paste0("select * from word3a where word like '",userinput , "%'"))
if (length(result3[,1]) > 0)
{
print(result3[1,1])
wordtable <- rbind(wordtable,result3)
}
result4 <- sqldf(paste0("select * from word3b where word like '",userinput , "%'"))
if (length(result4[,1]) > 0)
{
print(result4[1,1])
wordtable <- rbind(wordtable,result4)
}
##word4
result5 <- sqldf(paste0("select * from word4a where word like '",userinput , "%'"))
if (length(result5[,1]) > 0)
{
print(result5[1,1])
wordtable <- rbind(wordtable,result5)
}
result6 <- sqldf(paste0("select * from word4b where word like '",userinput , "%'"))
if (length(result6[,1]) > 0)
{
print(result6[1,1])
wordtable <- rbind(wordtable,result6)
}
##word5
result7 <- sqldf(paste0("select * from word5a where word like '",userinput , "%'"))
if (length(result7[,1]) > 0)
{
print(result7[1,1])
wordtable <- rbind(wordtable,result7)
}
result8 <- sqldf(paste0("select * from word5b where word like '",userinput , "%'"))
if (length(result8[,1]) > 0)
{
print(result8[1,1])
wordtable <- rbind(wordtable,result8)
}
##word6
result9 <- sqldf(paste0("select * from word6a where word like '",userinput , "%'"))
if (length(result9[,1]) > 0)
{
print(result9[1,1])
wordtable <- rbind(wordtable,result9)
}
result10 <- sqldf(paste0("select * from word6b where word like '",userinput , "%'"))
if (length(result10[,1]) > 0)
{
print(result10[1,1])
wordtable <- rbind(wordtable,result10)
}
##word7
result11 <- sqldf(paste0("select * from word7a where word like '",userinput , "%'"))
if (length(result11[,1]) > 0)
{
print(result11[1,1])
wordtable <- rbind(wordtable,result11)
}
result12 <- sqldf(paste0("select * from word7b where word like '",userinput , "%'"))
if (length(result12[,1]) > 0)
{
print(result12[1,1])
wordtable <- rbind(wordtable,result12)
}
print(round(as.double(difftime(Sys.time(), startTime,u="secs")), digits = 2))
returnValue(wordtable)
}
########################################33