diff --git a/inst/shiny/capture_speech/babelfish.png b/inst/shiny/capture_speech/babelfish.png index ff1a477..9192294 100644 Binary files a/inst/shiny/capture_speech/babelfish.png and b/inst/shiny/capture_speech/babelfish.png differ diff --git a/inst/shiny/capture_speech/server.R b/inst/shiny/capture_speech/server.R index 1f88a3c..312a2d2 100644 --- a/inst/shiny/capture_speech/server.R +++ b/inst/shiny/capture_speech/server.R @@ -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 @@ -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()) diff --git a/inst/shiny/capture_speech/ui.R b/inst/shiny/capture_speech/ui.R index 1e29b2a..e786950 100644 --- a/inst/shiny/capture_speech/ui.R +++ b/inst/shiny/capture_speech/ui.R @@ -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")) ), @@ -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(