Getting Started with TJBot

Pages
Contributors: SFUptownMaker, jweisz, maryam
Favorited Favorite 1

Sense the Tone of Twitter

Our next project uses Watson's Tone Analyzer to analyze recent tweets and get a sense of the overall emotional tone of them.

Create a Tone Analyzer Instance

Repeat the steps used on the previous page to create a "Speech to Text" instance with the "Tone Analyzer" service. Again, leave the default values in place and hit "Create".

Next, repeat the steps used to fetch the credentials from the IBM Cloud website and insert them into the file on the Raspberry Pi. This time, you'll find the "config.default.js" file in "Desktop/tjbot/recipes/sentiment_analysis". However, don't close it when you're done with this step.

Create a Twitter App to Access Recent Tweets

You'll need a Twitter account for this step, so go to Twitter and sign up for one if you don't already have one.

Now go to the Twitter apps site and log in. You should end up on a page that looks like this:

Twitter apps page

Click the "Create an app" button to be taken to the page seen below. Fill in the blanks as you see fit (note that the application name must be unique, so you can't reuse "TJBot"), click the checkbox, and then click the "Create your Twitter application" button.

Create an app page

You'll then be presented with this page, detailing some information from the creation process.

Created app

Right beneath the name of your app (in big bold letters at the top of the page) you'll see a tab titled "Keys and Access Tokens". Click on this to bring up this page:

Keys and access tokens

Now we need to grab a couple of pieces of information from this page for pasting into the config file we opened up above. As you can see below, there are four pieces of Twitter-related information that we need to collect. We'll start with the "consumer_key" and "consumer_secret". Both of these have the same name on the website as they do in the config file; copy and paste them from the website into the config file.

Twitter info to collect

Next we'll need to get the "access_token_key" and "access_token_secret" fields. To do that, scroll down on the webpage until you see the "Create my access token" button. Click it and you'll get this screen:

Access tokens

Copy and paste the "access_token_key" and "access_token_secret" into the config file. Then "Save As..." the file and name it "config.js". You're ready for the next step!

Launch the App!

It's time to check out the recipe in action! As before, open a terminal window on the Raspberry Pi and type out these commands:

cd Desktop/tjbot/recipes/sentiment_analysis
npm install
node sentiment.js

Remember, the npm install step will take some time to run, so be patient. After you run the node sentiment.js step, you should have a screen that looks like this:

sentiment.js run

It'll take a few moments for the app to gather enough tweets (at least 100) for it to run the Tone Analyzer, but when it does, it will print out a line stating the current emotion surrounding the keyword it is searching for, as well as changing the LED color.

When you're done, just hit ctrl+c to quit the script.