Skip to content

Commit

Permalink
Merge pull request #126 from ibmtjbot/dev
Browse files Browse the repository at this point in the history
Support for IAM authentication, new Tone Analyzer API
  • Loading branch information
jweisz authored Jan 15, 2019
2 parents 131368d + 0e7d50a commit 0f16e88
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 31 deletions.
4 changes: 2 additions & 2 deletions bootstrap/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ not have one already."
echo "2. Log in to Bluemix and create an instance of the Watson services you plan
to use. The Watson services are listed on the Bluemix dashboard, under
\"Catalog\". The full list of Watson services used by TJBot are:"
echo "Conversation (renamed to Assistant), Language Translator, Speech to Text, Text to Speech,"
echo "Assistant, Language Translator, Speech to Text, Text to Speech,"
echo "Tone Analyzer, and Visual Recognition"
echo "3. For each Watson service, click the \"Create\" button on the bottom right
of the page to create an instance of the service."
Expand All @@ -368,7 +368,7 @@ echo ""
read -p "Would you like to run hardware tests at this time? [y/N] " choice </dev/tty
case "$choice" in
"y" | "Y")
$TJBOT_DIR/runTests.sh $TJBOT_DIR
$TJBOT_DIR/bootstrap/runTests.sh $TJBOT_DIR
;;
*) ;;
esac
Expand Down
39 changes: 32 additions & 7 deletions recipes/conversation/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,51 @@ exports.credentials = {};
// Watson Assistant
// https://www.ibm.com/watson/services/conversation/
exports.credentials.assistant = {
password: '',
username: ''
// username/password authentication -- if your service uses this method,
// uncomment these two lines and comment the 'apikey' line below
// username: '',
// password: '',
// IAM authentication -- fill in your API key below
apikey: 'FILL IN YOUR API KEY HERE',
// service URL -- change this if the URL is different in your authentication credentials
url: 'https://gateway.watsonplatform.net/assistant/api/'
};

// Watson Speech to Text
// https://www.ibm.com/watson/services/speech-to-text/
exports.credentials.speech_to_text = {
password: '',
username: ''
// username/password authentication -- if your service uses this method,
// uncomment these two lines and comment the 'apikey' line below
// username: '',
// password: '',
// IAM authentication -- fill in your API key below
apikey: 'FILL IN YOUR API KEY HERE',
// service URL -- change this if the URL is different in your authentication credentials
url: 'https://stream.watsonplatform.net/speech-to-text/api/'
};

// Watson Text to Speech
// https://www.ibm.com/watson/services/text-to-speech/
exports.credentials.text_to_speech = {
password: '',
username: ''
// username/password authentication -- if your service uses this method,
// uncomment these two lines and comment the 'apikey' line below
// username: '',
// password: '',
// IAM authentication -- fill in your API key below
apikey: 'FILL IN YOUR API KEY HERE',
// service URL -- change this if the URL is different in your authentication credentials
url: 'https://stream.watsonplatform.net/text-to-speech/api/'
};

// Watson Visual Recognition
// https://www.ibm.com/watson/services/visual-recognition/
exports.credentials.visual_recognition = {
api_key: ''
// username/password authentication -- if your service uses this method,
// uncomment these two lines and comment the 'apikey' line below
// username: '',
// password: '',
// IAM authentication -- fill in your API key below
apikey: 'FILL IN YOUR API KEY HERE',
// service URL -- change this if the URL is different in your authentication credentials
url: 'https://gateway.watsonplatform.net/visual-recognition/api'
};
5 changes: 4 additions & 1 deletion recipes/sentiment_analysis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ You can also change the colors that TJBot shines. The table below shows the colo
| Anger | Red |
| Joy | Yellow |
| Fear | Magenta |
| Disgust | Green |
| Sadness | Blue |

You can change these colors by editing the `shineForEmotion()` function.

Also note that the Tone Analyzer API returns three language tones: `analytical`, `confident`, and `tentative`. Try modifying the recipe to do something new depending on which language tone is dominant.

> 💡 Hint: you may want to eliminate the `filter()` on `tone.document_tone.tones` to be able to examine the language tones.
## Troubleshoot
If the LED does not light up, you can try moving the power from 3.3 to 5 volts. If neither the 3.3v or 5v pins work, you will need a 1N4001 diode. The diode is inserted between the power pin of the LED (the shorter of the two middle pins) and the 5v pin on the Raspberry Pi.

Expand Down
10 changes: 8 additions & 2 deletions recipes/sentiment_analysis/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ exports.credentials = {};
// Watson Tone Analyzer
// https://www.ibm.com/watson/services/tone-analyzer/
exports.credentials.tone_analyzer = {
password: '',
username: ''
// username/password authentication -- if your service uses this method,
// uncomment these two lines and comment the 'apikey' line below
// username: '',
// password: '',
// IAM authentication -- fill in your API key below
apikey: 'FILL IN YOUR API KEY HERE',
// service URL -- change this if the URL is different in your authentication credentials
url: 'https://gateway.watsonplatform.net/tone-analyzer/api/'
};

// Twitter
Expand Down
41 changes: 24 additions & 17 deletions recipes/sentiment_analysis/sentiment.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,29 @@ function shineFromTweetSentiment() {
console.log("Analyzing tone of " + TWEETS.length + " tweets");

tj.analyzeTone(text).then(function(tone) {
tone.document_tone.tone_categories.forEach(function(category) {
if (category.category_id == "emotion_tone") {
// find the emotion with the highest confidence
var max = category.tones.reduce(function(a, b) {
return (a.score > b.score) ? a : b;
});

// make sure we really are confident
if (max.score >= CONFIDENCE_THRESHOLD) {
shineForEmotion(max.tone_id);
}
}
// find the tone with the highest confidence
// only consider the emotional tones (anger, fear, joy, sadness)
// each tone looks like this:
// {
// "score": 0.6165,
// "tone_id": "sadness",
// "tone_name": "Sadness"
// }

var emotionalTones = tone.document_tone.tones.filter(function(t) {
return t.tone_id == 'anger' || t.tone_id == 'fear' || t.tone_id == 'joy' || t.tone_i == 'sadness';
});

if (emotionalTones.length > 0) {
var maxTone = emotionalTones.reduce(function(a, b) {
return (a.score > b.score) ? a : b;
});

// make sure we really are confident
if (maxTone.score >= CONFIDENCE_THRESHOLD) {
shineForEmotion(maxTone.tone_id);
}
}
});
} else {
console.log("Not enough tweets collected to perform sentiment analysis");
Expand All @@ -128,14 +138,11 @@ function shineForEmotion(emotion) {
case 'anger':
tj.shine('red');
break;
case 'joy':
tj.shine('yellow');
break;
case 'fear':
tj.shine('magenta');
break;
case 'disgust':
tj.shine('green');
case 'joy':
tj.shine('yellow');
break;
case 'sadness':
tj.shine('blue');
Expand Down
10 changes: 8 additions & 2 deletions recipes/speech_to_text/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ exports.credentials = {};
// Watson Speech to Text
// https://www.ibm.com/watson/services/speech-to-text/
exports.credentials.speech_to_text = {
password: '',
username: ''
// username/password authentication -- if your service uses this method,
// uncomment these two lines and comment the 'apikey' line below
// username: '',
// password: '',
// IAM authentication -- fill in your API key below
apikey: 'FILL IN YOUR API KEY HERE',
// service URL -- change this if the URL is different in your authentication credentials
url: 'https://stream.watsonplatform.net/speech-to-text/api/'
};

0 comments on commit 0f16e88

Please sign in to comment.