-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_3.R
95 lines (91 loc) · 3.92 KB
/
app_3.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
# setwd("C:\\Users\\spfeiffe\\OneDrive - Environmental Protection Agency (EPA)\\_mine\\Shiny_apps\\_not_active_at_the_moment\\demo_(Latin)")
#
#testdict <- c('M', 'b', 'c', 'Mi', 'Mic', 'f')
#allPossibleLatinRoots <- testdict[which(sapply(testdict, function(X){startsWith("Mickey", X)}))]
#thisLatinRoot <- allPossibleLatinRoots[max(sapply(allPossibleLatinRoots, nchar))]
#
ui <- fluidPage (
textInput (
"userEnteredString",
"Enter exact Latin verb(-form) here:",
value=""
),
uiOutput("translationGuide")
)
server <- function(input, output)
{
##################################################################################
# Load constants
##################################################################################
output$translationGuide <- renderText("Loading constants...")
require(assertthat)
require(stringi)
if (!exists("dict"))
{
dict <- as.matrix(read.table("dict/all_regular.tsv", header=FALSE, fileEncoding="UTF-8-BOM", sep='\t', quote='"')[,1:4]) # conj, root, meaning, present_stem/perfect_stem/PPP
}
if (!exists("casePoss"))
{
casePoss <- as.matrix(read.csv("case_poss/all_conj_not_dep.csv", header=FALSE))
}
allEndings <- unique(casePoss[,1]) # all possible endings
output$translationGuide <- renderTable (
{
if (nchar(trimws(input$userEnteredString)) == 0)
{
###
###
###
paste0("Waiting.....")
###
###
###
} else {
if (length(which(sapply(dict[,2], function(X){startsWith(input$userEnteredString, X)}))) != 0)
{
allPossibleLatinRoots <- unique(dict[which(sapply(dict[,2], function(X){startsWith(input$userEnteredString, X)})), 2])
if (length(which(sapply(allPossibleLatinRoots,nchar) == nchar(input$userEnteredString))) > 0)
{
APLRWANTWI <- # All Possible Latin Roots Which Are Not The Word Itself
allPossibleLatinRoots[-which(sapply(allPossibleLatinRoots,nchar) == nchar(input$userEnteredString))]
} else {
APLRWANTWI <- allPossibleLatinRoots
}
thisLatinRoot <- APLRWANTWI[which(sapply(APLRWANTWI,nchar) == max(sapply(APLRWANTWI,nchar)))]
thisLatinEnding <- substr(input$userEnteredString, nchar(thisLatinRoot)+1, nchar(input$userEnteredString))
dictHits <- dict[which(dict[,2] == thisLatinRoot), ]
outMat <- matrix(nrow=1, ncol=4, data=c(paste0(thisLatinRoot, "-"), paste0("-", thisLatinEnding)))
if ("matrix" %in% class(dictHits)) # 2 or more rows returned
{
outMat[1,3] <- stringi::stri_flatten(c(dictHits[1,1], dictHits[1,4]), collapse=" ")
outMat[1,4] <- dictHits[1,3]
for (i in 2:nrow(dictHits))
{
outMat <- rbind(outMat, c("", "", stringi::stri_flatten(c(dictHits[i,1], dictHits[i,4]), collapse=" "), dictHits[1,3]))
}
} else { # 1 row returned
outMat[1,3] <- stringi::stri_flatten(c(dictHits[1], dictHits[4]), collapse=" ")
outMat[1,4] <- dictHits[3]
}
###
###
###
unique(outMat)
###
###
###
} else {
###
###
###
paste0("This word does not appear to be in this app's dictionary - make sure it is spelled correctly, is a regular verb, and if in a form derived from the PPP, does not include a space or linking verb.")
###
###
###
}
}
},
colnames=FALSE
)
}
shinyApp(ui=ui, server=server)