Skip to content

Commit

Permalink
Add NLP to demo
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkEdmondson1234 committed Dec 9, 2017
1 parent bf5818a commit 0fbc214
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
Binary file modified inst/shiny/capture_speech/babelfish.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
74 changes: 74 additions & 0 deletions inst/shiny/capture_speech/server.R
Expand Up @@ -18,6 +18,40 @@ function(input, output, session){
translation()
})

output$nlp_sentences <- renderTable({
req(nlp())

nlp()$sentences[[1]]

})

output$nlp_tokens <- renderTable({
req(nlp())

## only a few otherwise it breaks formatting
nlp()$tokens[[1]][, c("content","beginOffset","tag","mood","number")]

})

output$nlp_entities <- renderTable({
req(nlp())

nlp()$entities[[1]]

})

output$nlp_misc <- renderTable({
req(nlp())

data.frame(
language = nlp()$language,
text = nlp()$text,
documentSentimentMagnitude = nlp()$documentSentiment$magnitude,
documentSentimentScore = nlp()$documentSentiment$score
)

})

input_audio <- reactive({
req(input$audio)
a <- input$audio
Expand Down Expand Up @@ -122,6 +156,46 @@ function(input, output, session){

})

nlp <- reactive({
req(get_api_text())
req(input$nlp)

nlp_lang <- switch(input$nlp,
none = NULL,
input = substr(input$language, start = 0, stop = 2),
trans = input$translate # not activated from ui.R dropdown as entity analysis only available on 'en' at the moment
)

if(is.null(nlp_lang)){
return(NULL)
}

## has to be on supported list of NLP language codes
if(!any(nlp_lang %in% c("en", "zh", "zh-Hant", "fr",
"de", "it", "ja", "ko", "pt", "es"))){
message("Unsupported NLP language, switching to 'en'")
nlp_lang <- "en"
}

message("Calling NLP API")
shinyjs::show(id = "api",
anim = TRUE,
animType = "fade",
time = 1,
selector = NULL)

nnn <- gl_nlp(get_api_text(), language = nlp_lang)

message("API returned: ", nnn$text)
shinyjs::hide(id = "api",
anim = TRUE,
animType = "fade",
time = 1,
selector = NULL)
nnn

})

observe({

req(translation())
Expand Down
14 changes: 13 additions & 1 deletion inst/shiny/capture_speech/ui.R
Expand Up @@ -49,6 +49,13 @@ shinyUI(
"Italian" = "it",
"Norwegian" = "nb",
"Swedish" = "sv")),
helpText("Send the text to the Natural Language API for NLP analysis below."),
selectInput("nlp", "Perform NLP", choices = c("No NLP" = "none",
"NLP" = "input"
#,
#"On Translated Text" = "trans"
)
),
helpText("Many more languages are supported in the API but I couldn't be bothered to put them all in - see here:",
a(href="https://cloud.google.com/speech/docs/languages", "Supported languages"))
),
Expand All @@ -63,7 +70,12 @@ shinyUI(
h2("Transcribed text"),
p(textOutput("result_text")),
h2("Translated text"),
p(textOutput("result_translation"))
p(textOutput("result_translation")),
h2("NLP"),
tableOutput("nlp_sentences"),
tableOutput("nlp_tokens"),
tableOutput("nlp_entities"),
tableOutput("nlp_misc")
)
),
helpText(
Expand Down

0 comments on commit 0fbc214

Please sign in to comment.