tetragramModel <- memoise(function(s){
matches = uni_dict[1:3]
query_corpus <- corpus(s)
query_tokens <- quanteda::tokenize(toLower(query_corpus), ngrams = 1, removeNumbers=TRUE, removePunct=TRUE,
removeTwitter = TRUE, removeSeparators = TRUE)
l = length(query_tokens[[1]])
if (l >= 3){
query_tokens <- tail(query_tokens[[1]], 3)
if(!FALSE %in% (query_tokens %chin% names(uni_dict))){
query <- uni_dict[query_tokens]
query <- as.numeric(paste0(query[1],query[2], query[3]))
matches <- subset(tetragram_dt, trigram_int == query, select = word)[[1]]
if (!FALSE %in% (query_tokens[2:3] %chin% names(uni_dict)) && length(matches) < 3){
query <- uni_dict[query_tokens[2:3]]
query <- as.numeric(paste0(query[1],query[2]))
matches <- c(matches, subset(trigram_dt, bigram_int == query, select = word)[[1]])
if (!FALSE %in% (query_tokens[3] %chin% names(uni_dict)) && length(matches) < 3){
query <- as.numeric(paste0(uni_dict[[query_tokens[3]]]))
matches <- c(matches, subset(bigram_dt, unigram_int == query, select = word)[[1]])
}
}
}
else{
matches <- unigram_dt$word[3]
}
}
else if (l == 2){
query_tokens <- tail(query_tokens[[1]], 2)
if(!FALSE %in% (query_tokens %chin% names(uni_dict))){
query <- uni_dict[query_tokens]
query <- as.numeric(paste0(query[1],query[2]))
matches <- subset(trigram_dt, bigram_int == query, select = word)[[1]]
if (!FALSE %in% (query_tokens[2] %chin% names(uni_dict)) && length(matches) < 3){
query <- as.numeric(paste0(uni_dict[[query_tokens[2]]]))
matches <- c(matches, subset(bigram_dt, unigram_int == query, select = word)[[1]])
}
}
else{
matches <- unigram_dt$word[2]
}
}
else if(l == 1){
query_tokens <- tail(query_tokens[[1]], 1)
if (!FALSE %in% (query_tokens %chin% names(uni_dict))){
query <- as.numeric(paste0(uni_dict[[query_tokens]]))
matches <- subset(bigram_dt, unigram_int == query, select = word)[[1]]
}
else{
matches <- uni_dict[1:3]
}
}
if(length(matches)> 0 && length(matches) <=5){
nextWords <- unique(names(uni_dict[matches]))
#nextWords <- nextWords[2]
}
else if (length(matches) > 5){
nextWords <- unique(names(uni_dict[matches]))[1:5]
}
else {
nextWords <- "Sorry, couldn't find the suitable next word! \n Please Enter the Text"
}
return (nextWords)
})
I have no idea why its not responding. I strongly believe that the data files are not loading on the server. but no idea why they are not.
I really appreciate your help and support in this regard.
Jagan.