New to Dream to Learn? Check out our Quick Start guide!  

2COMMENTS0RECOMMENDS

Tone Analysis - With a bit of Color! Building a Basic STEM Lab for Kids

30
POSTED IN: Building Bridges from R to IBM Watson

Spring Break - Building a STEM Lab for My Kids...

I'm putting together a lab for my kids.  Was thinking about what the intersection of IBM Watson services (my day job is an IBM-er) and things my kids like and are interested in...

 

Inside... Inside Out

We recently watched the movie "Inside Out" - a wonderfully crafted Pixar/Disney production - https://en.wikipedia.org/wiki/Inside_Out_%282015_film%29

if you've not seen it (and you really should) the synopsis is: "Riley s a happy, hockey-loving 11-year-old Midwestern girl, but her world turns upside-down when she and her parents move to San Francisco. Riley's emotions -- led by Joy - try to guide her through this difficult, life-changing event. However, the stress of the move brings Sadness to the forefront. When Joy and Sadness are inadvertently swept into the far reaches of Riley's mind, the only emotions left in Headquarters are Anger, Fear and Disgust."

 

IMAGES: the images below belong to Pixar Animation Studios and Walt Disney Pictures - and are presented here to make the educational subject matter more interesting.  Images should NOT be used in anything commercial unless you receive permission from the media owner(s)

http://copyright.gov/fair-use/ "Educational multimedia projects created under these guidelines incorporate students' or educators' original material, such as course notes or commentary, together with various copyrighted media formats including but not limited to, motion media, music, text material, graphics, illustrations, photographs and digital software which are combined into an integrated presentation. "

https://en.wikipedia.org/wiki/Fair_use  /// Information here is my own personal blog, and does not necessarily reflect the views of my employer.

 

Tone Analyzer - from IBM Watson

"The Tone Analyzer service enables people to discover and understand, and revise the impact of tone in their content. It uses linguistic analysis to detect and interpret emotional, social, and language cues found in text. "

you can try the demo here:

https://tone-analyzer-demo.mybluemix.net/

When you send the query ('utterance') to IBM Watson Tone Service - you get back something like this, once you clean it up:

query <- "I am super scared and nervous about bees"
 

  • anger   0.01
  • disgust   0.02
  • fear   0.99
  • joy   0.03
  • sadness   0.02

First Prototype - Key Steps

So basically - this first, rough prototype

  1. Takes a query or 'utterance' (typing for now, but will wire up speech in V2)
  2. Sends to the Tone Analyzer service
  3. Receives the Results
  4. Extracts the pieces we want (the 5 traits above)
  5. Sorts them highest amplitude to lowest
  6. Tests to see if the threshold of signal is high enough to 'present'
  7. Presents image of the emotion (images are property of studio)
  8. Presents a bar plot of the 5 traits

 

Life is wonderful! I love to code build stuff

 

I am scared - I need my tinfoil hat

 

 

Don't pick your nose - it's really gross

 

I hate you and I'm going to burn your house down

 

 

I cant stop crying.  The world is such a dull place

 

 

I was content. But then I tripped and fell over. I yelled

 

 

Instructions are coming so you can try!

I'm going to clean this up (step by step for you) in V2.  (it's 2am now so I'm going to bed)- and make a STEM LAB out of it.  If you know how to install software, and are willing to explore with your kid - you can build this yourself in an hour or two.  The Services are here https://console.ng.bluemix.net/ and you can sign up for free to try.  R is Free.  R Studio Is free.  Bluemix trial and Watson services are free to try.

 

Happy Coding!

 

 

Here is the Code... Version 1

######################################################
### Experimental Code.  Experimental R Interface for IBM Watson Services -
### Focus: IBM Watson Tone Analyzer is a service that helps people comprehend, consume, and revise the language tone of their writing for more efficient communications
### https://tone-analyzer-demo.mybluemix.net/
### http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/tone-analyzer/
### http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/apis/tone-analyzer-apis.html
### https://github.com/rustyoldrake/R_Scripts_for_Watson - Ryan Anderson  - this is my test code.  Use at your own peril!  Representations do not necessarily represent the views of my employer
### Inside Out is a 2015 American 3D computer-animated comedy-drama adventure film produced by Pixar Animation Studios and released by Walt Disney Pictures. All media and images belong to Pixar/Disney and used under fair use (teaching and research)
######################################################

library(RCurl) # to talk to Watson - REST APIS # install.packages("RCurl") # if the package is not already installed
library(httr) # comms
library(XML)  # comms and data
library(data.table) # data shaping
library(reshape2) # data shaping
library(tidyr) # data cleaning
library(dplyr) # data cleaning
library(png) # for the presenting of images

######### Housekeeping And Authentication
setwd("/Users/ryan/Documents/Service - Tone Analyzer")
getwd()
source("keys.R") # this files is where you put your Access Credentials from Bluemix (username and password)

base_url_TON = "https://gateway.watsonplatform.net/tone-analyzer-beta/api"

### GET - Basic Test
getURL("https://gateway.watsonplatform.net/tone-analyzer-beta/api/v3/tone?version=2016-02-11&text=hello",userpwd = username_password_TON )

### Function to process output from API and table
tidyResponse <- function(data)
{
  data <- as.data.frame(strsplit(as.character(data),"\"score\""))
  data <- data[-c(1), ] # remove dud first row
  data  <- gsub("\"tone_id\":","",data)
  data  <- gsub(":","",data)
  data  <- gsub("\"","",data)
  data  <- gsub("_big5","",data)
  data <- data.frame(data)
  data <- data.frame(do.call('rbind', strsplit(as.character(data$data),',',fixed=TRUE)))
  data <- data[,-c(3:6), ] # remove dud first row
  data <- data[c("X2", "X1")]
  data$X1 <- as.character.numeric_version(data$X1) # not sure why, but coercing to numbers requires this
  data$X1 <- as.numeric(data$X1)
  data$X1 <- round((data$X1),2)
  setnames(data,c("trait","signal"))
  return(data)
}


### FUNCTION to post data to Tone Analyzer and return results
process_data_to_tone <- function(text)
{
  response <- POST(url="https://gateway.watsonplatform.net/tone-analyzer-beta/api/v3/tone?version=2016-02-11",
                   authenticate(username_TON,password_TON),
                   add_headers("Content-Type"="text/plain","charset"="UTF-8"),
                   body=text )

  response_text <- content(response, "text", encoding = "UTF-8")  # or encoding = "ISO-8859-1"
  abc <- tidyResponse(response_text)
  return(abc)
}


process_data_to_tone("I am certainly going to make this project a success") # not perfect, but good enough for now!
query <- "I am super scared and nervous about bees"
process_data_to_tone(query)

# trait signal
# 1              anger   0.01
# 2            disgust   0.02
# 3               fear   0.99
# 4                joy   0.03
# 5            sadness   0.02
# 6         analytical   0.00
# 7          confident   0.00
# 8          tentative   0.00
# 9           openness   0.21
# 10 conscientiousness   0.22
# 11      extraversion   0.76
# 12     agreeableness   0.37
# 13       neuroticism   0.99


 
### FUNCTION to recieve results of analysis, pull out 5 traits, and show image and plot
display_results <- function(results){
  results <- data.frame(results[1:5,]) # just the gang :)
  results <- results[with(results, order(-signal)),] # reorder
  results <- data.frame(results,stringsAsFactors=FALSE) # stringsAsFactors=FALSE keeps junky factors out
  results
 
  main_trait <- paste(results$trait[1]) # paste kills levels
  main_signal <- results$signal[1]
  signal_threshold <- 0.40 # this is level that we present
 
 
  ### Inside Out is a 2015 American 3D computer-animated comedy-drama adventure film produced by Pixar Animation Studios and released by Walt Disney Pictures. All media and images belong to Pixar/Disney and used under fair use (teaching and research)
  ### These PNG mages belong to studios - for education purposes only (fair use) - be good!
 
  ## Switch Statement
  if(main_signal>signal_threshold)
  {
    switch(main_trait,
           "joy" = image <- readPNG("joy.png"),
           "sadness" = image <- readPNG("sad.png"),
           "anger" = image <- readPNG("anger.png"),
           "disgust" = image <- readPNG("disgust.png"),
           "fear" = image <- readPNG("fear.png")
    )
  } else{image <- readPNG("all.png")}
 
  #image <- readPNG("anger.png")
  par(mfrow=c(1, 2))
  plot(1:1, type="n", main="Tone Results", xlab="", ylab="", asp=1)
  lim <- par()
  rasterImage(image, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4])
 
  barplot(results$signal, main="Tone Analysis",
          xlab="", ylab=query,
          names.arg=results$trait,
          border="red",las=3, srt=45)
}


###  OK!  LET'S DO STUFF
###  OK!  LET'S DO STUFF
###  OK!  LET'S DO STUFF

 


### OK - let's throw a few softballs - one for each inside out character (my kids love the movie!)

query <- "Life is wonderful! I love to code build stuff"
analysis <- process_data_to_tone(query)
display_results(analysis)

query <- "I am scared - I need my tinfoil hat"
analysis <- process_data_to_tone(query)
display_results(analysis)

query <- "Dont pick your nose - it's really gross"
analysis <- process_data_to_tone(query)
display_results(analysis)

query <- "I hate you and I'm going to burn your house down"
analysis <- process_data_to_tone(query)
display_results(analysis)

query <- "I cant stop crying.  The world is such a dull place"
analysis <- process_data_to_tone(query)
display_results(analysis)


query <- "I was content. But then I tripped and fell over. I yelled"
analysis <- process_data_to_tone(query)
display_results(analysis)


graphics.off()

 

 

Before you can comment, you need to sign-up or login

Jake Madden

responded April 2016

(last followup April 2016)
Very cool! Nice work :)

I can see a future where our messenger apps change colors for each side depending on the tone of the conversation.
Ryan Anderson

responded April 2016

Post Author
thanks Jake!

About the Author

Ryan Anderson

Ryan Anderson

Hi! I like to play with data, analytics and hack around with robots and gadgets in my garage. Lately I've been learning about machine learning.

About this blog

This is an informal blog that explores tools, code and tricks that group members have developed to engage IBM Watson cognitive computing services - from the R Programming Language. Packages include RCURL to access Watson APIs - for services that include Natural Language Classifier and Speech to Text. THIS IS MY PERSONAL BLOG - it does not represent the views of my employer. Code is presented as 'use at your own risk' (it has lots of bugs)

Created: September 13, 2015

English

Up Next