-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.R
More file actions
72 lines (58 loc) · 2.61 KB
/
server.R
File metadata and controls
72 lines (58 loc) · 2.61 KB
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
#
# This is the server logic of a Shiny web application. You can run the
# application by clicking 'Run App' above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
# Define server logic required to draw a histogram
suppressWarnings(library(tm))
suppressWarnings(library(stringr))
suppressWarnings(library(shiny))
# Load Quadgram,Trigram & Bigram Data frame files
quadgram <- readRDS("quadgram.RData")
trigram <- readRDS("trigram.RData")
bigram <- readRDS("bigram.RData")
mesg <<- ""
# Cleaning of user input before predicting the next word
Predict <- function(x) {
xclean <- removeNumbers(removePunctuation(tolower(x)))
xs <- strsplit(xclean, " ")[[1]]
# Back Off Algorithm
# Predict the next term of the user input sentence
# 1. For prediction of the next word, Quadgram is first used (first three words of Quadgram are the last three words of the user provided sentence).
# 2. If no Quadgram is found, back off to Trigram (first two words of Trigram are the last two words of the sentence).
# 3. If no Trigram is found, back off to Bigram (first word of Bigram is the last word of the sentence)
# 4. If no Bigram is found, back off to the most common word with highest frequency 'the' is returned.
if (length(xs)>= 3) {
xs <- tail(xs,3)
if (identical(character(0),head(quadgram[quadgram$unigram == xs[1] & quadgram$bigram == xs[2] & quadgram$trigram == xs[3], 4],1))){
Predict(paste(xs[2],xs[3],sep=" "))
}
else {mesg <<- "Next word is predicted using 4-gram."; head(quadgram[quadgram$unigram == xs[1] & quadgram$bigram == xs[2] & quadgram$trigram == xs[3], 4],1)}
}
else if (length(xs) == 2){
xs <- tail(xs,2)
if (identical(character(0),head(trigram[trigram$unigram == xs[1] & trigram$bigram == xs[2], 3],1))) {
Predict(xs[2])
}
else {mesg<<- "Next word is predicted using 3-gram."; head(trigram[trigram$unigram == xs[1] & trigram$bigram == xs[2], 3],1)}
}
else if (length(xs) == 1){
xs <- tail(xs,1)
if (identical(character(0),head(bigram[bigram$unigram == xs[1], 2],1))) {mesg<<-"No match found. Most common word 'the' is returned."; head("the",1)}
else {mesg <<- "Next word is predicted using 2-gram."; head(bigram[bigram$unigram == xs[1],2],1)}
}
}
shinyServer(function(input, output) {
output$prediction <- renderPrint({
result <- Predict(input$inputString)
output$text2 <- renderText({mesg})
result
});
output$text1 <- renderText({
input$inputString});
}
)