knitr::opts_knit$set(progress = TRUE, verbose = TRUE)
  library(cognizer)
  library(knitr)
  ALCHEMY_API_KEY <- Sys.getenv("ALCHEMY_API_KEY")
  LANG_TRANSLATE_USERNAME_PASSWORD <- Sys.getenv("LANG_TRANSLATE_USERNAME_PASSWORD")
  PERSONALITY_USERNAME_PASSWORD <- Sys.getenv("PERSONALITY_USERNAME_PASSWORD")
  TONE_USERNAME_PASSWORD <- Sys.getenv("TONE_USERNAME_PASSWORD")
  TEXT_TO_SPEECH_USERNAME_PASSWORD <- Sys.getenv("TEXT_TO_SPEECH_USERNAME_PASSWORD")
  IMAGE_API_KEY <- Sys.getenv("IMAGE_API_KEY")
  SPEECH_TO_TEXT_USERNAME_PASSWORD <- Sys.getenv("SPEECH_TO_TEXT_USERNAME_PASSWORD")


   # knitr hook function to allow an output.lines option
   # e.g.,
   #   output.lines=12 prints lines 1:12 ...
   #   output.lines=1:12 does the same
   #   output.lines=3:15 prints lines ... 3:15 ...
   #   output.lines=-(1:8) removes lines 1:8 and prints ... 9:n ...
   #   No allowance for anything but a consecutive range of lines
   #   https://stat.ethz.ch/pipermail/r-help/2014-October/422286.html

   hook_output <- knit_hooks$get("output")
   knit_hooks$set(output = function(x, options) {
      lines <- options$output.lines
      if (is.null(lines)) {
        return(hook_output(x, options))  # pass to default hook
      }
      x <- unlist(strsplit(x, "\n"))
      more <- "..."
      if (length(lines)==1) {        # first n lines
        if (length(x) > lines) {
          # truncate the output, but add ....
          x <- c(head(x, lines), more)
        }
      } else {
        x <- c(if (abs(lines[1])>1 | lines[1]<0) more else NULL,
               x[lines],
               if (length(x)>lines[abs(length(lines))]) more else NULL
              )
      }
      # paste these lines together
      x <- paste(c(x, ""), collapse = "\n")
      hook_output(x, options)
    })

R package to wrap function calls to IBM Watson services.

You must already have an active Bluemix ID and account to obtain credentials for a service; for more information, see Registering for Bluemix.

In addition to an active Bluemix ID, you must already have service credentials from Bluemix for each Watson Service you will be using through congizeR. Please follow the following steps for “Getting service credentials in Bluemix”.

Install

You can install:

if (packageVersion("devtools") < 1.6) {
  install.packages("devtools")
}
if (packageVersion("curl") < 0.9.9) {
  install_github("jeroenooms/curl")
}
devtools::install_github("ColumbusCollaboratory/cognizeR", auth_token = "token")
install.packages(c("rmsfact", "testthat"))

You may want to review the Watson Services documentation for those services available through the R package.

Authentication

All Watson services use basic authentication in the form of api keys or username-password combinations. To start using cognizeR functions, you will need to pass your authentication details to them as an argument. There are many ways to manage your passwords, and we do not want to impose any particular structure on this process. If no solution comes to mind, one approach is to use the R environment file to store your authentication details that can be easily and programmatically passed to the cognizeR functions.

If you already have .Renviron file in your home directory, then you can add something like

SERVICE_API_KEY = "key"

and/or

SERVICE_USERNAME_PASSWORD = "username:password"

(Notice the use of = as opposed <- when storing environment variables.) If not, then you can run the following commands to create and edit the file by inserting the name and value pairs of the environment variables in the above format:

r_env <- file.path(normalizePath("~"), ".Renviron")
if (!file.exists(r_env)) file.create(r_env)
file.edit(r_env)

After restarting R, you can then access the values of environment variables with

Sys.getenv("API_SERVICE_NAME")

cognizeR Watson Services Examples:

Text Processing

Alchemy Language

Sentiment Analysis -top-
text <- c("Columbus, Ohio is Awesome!", "Looking forward to UseR2017 in Brussels!")
result <- text_sentiment(text, YOUR_API_KEY)
str(result)
Keyword Extraction -top-
text <- c("Columbus, Ohio is Awesome!", "Looking forward to UseR2017 in Brussels!")
result <- text_keywords(text, YOUR_API_KEY)
str(result)
Emotion Analysis -top-
text <- c("Columbus, Ohio is Awesome!", "Looking forward to UseR2017 in Brussels!")
result <- text_emotion(text, YOUR_API_KEY)
str(result)
Language Detection -top-
text <- c("Columbus, Ohio is Awesome!", "Mirando hacia adelante a UseR2017 en Bruselas!")
result <- text_language(text, YOUR_API_KEY)
str(result)
Entity Extraction -top-
text <- c("Columbus, Ohio is Awesome!", "Looking forward to UseR2017 in Brussels!")
result <- text_entity(text, YOUR_API_KEY)
str(result)
Concept Tagging -top-
text <- "Columbus, Ohio is Awesome!"
result <- text_concept(text, YOUR_API_KEY)
str(result)
Relation Extraction -top-
text <- "Columbus, Ohio is Awesome!"
result <- text_relations(text, YOUR_API_KEY)
str(result)
Taxonomy Classification -top-
text <- "Columbus, Ohio is Awesome!"
result <- text_taxonomy(text, YOUR_API_KEY)
str(result)

Language Translate -top-

The following Web Services use IBM Bluemix Watson Services Username and Passwords as available on Bluemix Credentials in a colon deliminated string. LANG_TRANSLATE_USERNAME_PASSWORD is a username:password string as defined for each Bluemix Watson Services.

text <- c("Mirando hacia adelante a UseR2017 en Bruselas!")
result <- text_translate(text, LANG_TRANSLATE_USERNAME_PASSWORD)
str(result)

Personality Insights -top-

The following Web Services use IBM Bluemix Watson Services Username and Passwords as available on Bluemix Credentials in a colon deliminated string. PERSONALITY_USERNAME_PASSWORD is a username:password string as defined for each Bluemix Watson Services.

text <- paste(replicate(1000, rmsfact::rmsfact()), collapse = ' ') #Ten Richard Stallman Facts used for Personality Insights.
result <- text_personality(text, PERSONALITY_USERNAME_PASSWORD)
str(result)

Tone Analyzer -top-

The following Web Services use IBM Bluemix Watson Services Username and Passwords as available on Bluemix Credentials in a colon deliminated string. TONE_USERNAME_PASSWORD is a username:password string as defined for each Bluemix Watson Services.

text <- c("Columbus, Ohio is Awesome!")
result <- text_tone(text, TONE_USERNAME_PASSWORD)
str(result)

Text-to-Speech -top-

The following Web Services use IBM Bluemix Watson Services Username and Passwords as available on Bluemix Credentials in a colon deliminated string. TEXT_TO_SPEECH_USERNAME_PASSWORD is a username:password string as defined for each Bluemix Watson Services.

junk <- dir(path = ".", pattern = "*.ogg") 
file.remove(junk)
text <- c("Columbus, Ohio is Awesome!")
text_audio(text, TEXT_TO_SPEECH_USERNAME_PASSWORD, directory = '.')
filenames <- list.files(".", pattern="*.ogg", full.names=FALSE)

The .ogg audio file is written to the current directory.

You can listen to the example audio file in the repository: r filenames[1]

Image Visual Recognition

Classification of Image -top-

The following Web Services use IBM Bluemix Watson Services IMAGE_API_KEY specific to Image processsing.

image_face_path <- system.file("extdata/images_faces", package = "cognizer")
images <- list.files(image_face_path, full.names = TRUE)
image_classes <- image_classify(images, IMAGE_API_KEY)
str(image_classes)
Detect Faces in Image -top-

The following Web Services use IBM Bluemix Watson Services IMAGE_API_KEY specific to Image processsing.

image_face_path <- system.file("extdata/images_faces", package = "cognizer")
images <- list.files(image_face_path, full.names = TRUE)
image_faces <- image_detectface(images, IMAGE_API_KEY)
str(image_faces)
Detect Text in Image -top-

The following Web Services use IBM Bluemix Watson Services IMAGE_API_KEY specific to Image processsing.

image_text_path <- system.file("extdata/images_text", package = "cognizer")
images <- list.files(image_text_path, full.names = TRUE)
image_text<- image_detecttext(images, IMAGE_API_KEY)
str(image_text)

Audio Processing

Speech to Text -top-

The following Web Services use IBM Bluemix Watson Services Username and Passwords as available on Bluemix Credentials in a colon deliminated string. SPEECH_TO_TEXT_USERNAME_PASSWORD is a username:password string as defined for each Bluemix Watson Services.

audio_path <- system.file("extdata/audio", package = "cognizer")
audios <- list.files(audio_path, full.names = TRUE)
audio_transcript <- audio_text(audios, SPEECH_TO_TEXT_USERNAME_PASSWORD)
str(audio_transcript)


ColumbusCollaboratory/cognizer documentation built on May 6, 2019, 12:49 p.m.