MSc Product Design - Twitter App + Personality Insights API

Taking a quick break from desk research, I decided to experiment with the Watson API and make my own personality analyser using Python and the Twitter API. Inspired by the demo by IBM for showcasing the Personality Insight API, I made a basic Python application that samples the latest 200 tweets of a desired public account, flatten them into a single string and pass them to the API. Returned is 5 qualities and their values based on the analysis of the 200 tweets. Here's how you can make your own.

 Okay, so this is a little more than 5, but stick with me...

Okay, so this is a little more than 5, but stick with me...

First of, you'll need to set up a Python coding environment on your computer - I followed this guide to do this. I should point out now that this guide helps you set up to use Python3 on your computer when really we want to use Python2, but it also gives you steps on how to install the correct version of Python you'll need. The guide also sets you up to use the built in text editor Nano but you can use any text editor that registers Python - I use Sublime Text

Secondly, you'll need to create a Twitter application - you can do this here. Give it a name - I called mine MScPD-PersonalityInsightsTest - a description and a url (a placeholder one is fine). Don't worry about a callback URL for now - we're not looking to make this project live yet. Finally accept the T's & C's and your away.

Once your app is created, go to the keys and access tokens tab. Here you'll see your consumer key and your consumer secret - make a note of these since you'll need these later on. Scroll down the page and click the button to generate an access token. This will generate another two keys that you'll need later for the program to work. Finally make sure your access level is read and write.

code16.jpg

Next, you'll need a IBM Bluemix account for access to the Watson API - you can sign up for one here. Once you're in the dashboard of the console, scroll down to the bottom of the page and click the create service button.

From the options, select Personality Insights from the Watson category. Give your service and name and leave it unbound for now, and choose the "lite" option. Once created, click the drop down menu to the right of your credentials - Credentials1 if you didn't change it - and click to the view them. This will reveal the key and password we need when we need our app to access the Watson API. Make a note of them, and that's you (almost) ready to start coding the app!

Before we start coding, we need to make sure we have access to the packages required, and we'll use Pip again just as we did earlier (if you followed the set up guide). In the terminal, enter:

 This installs the twitter package we need...

This installs the twitter package we need...

 ...while this installs the package we need to interact with Watson.

...while this installs the package we need to interact with Watson.

With this installed, open up your text editor and enter the following:

 These are the two main functions that makes the program work.

These are the two main functions that makes the program work.

 This section calls the functions and prints the results into the terminal.

This section calls the functions and prints the results into the terminal.

The Python packages we import at the start are vital as they are dependencies for the python-twitter and watson-developer-cloudpackages we manually installed earlier. Without them, the python-twitter and watson-developer-cloudpackages won't function correctly.

When the analyse() function is called, it accesses the Twitter api via our app using the keys provided to gather the tweets we need from the Twitter handle we've selected - it gathers 200 tweets and excludes retweets so it's only interested in gathering what the user has tweeted. We then create an empty string to store the data we receive from Twitter and use an if statement to only include tweets that are in English and then encode them in UTF-8, which PI understands instead of Unicode, which Twitter understands.

The tweets are then pushed to PI - accessed via our credentials - who analyses the tweets and returns the data. By default, PI creates a tree structure for various categories. These categories are broken into Personality, Values, and Needs. These are then are broken into subcategories, and finally, broken into the the actual traits. We use the flatten() function to flatten the JSON structure that the analyse() function returns from PI.

We use the sorted() function to sort the results returned to us by PI. The results are sorted highest to lowest and show the 5 highest values according to PI based on the tweets. Finally, we then print the username of the account and the results to the console, and make sure to limit it to just 5 that we want to see.

Once you're finished coding and you're ready to run, open up your terminal again and type:

This will run your app in the terminal and feed back 5 traits and their values from the timeline of the selected handle!

 And there we have it - my Big 5 traits based on my tweets!

And there we have it - my Big 5 traits based on my tweets!

Just for fun, I analysed the personality of a number of key figures:

 Kanye West

Kanye West

 Theresa May

Theresa May

 Jeremy Corbyn

Jeremy Corbyn

 Tim Brown - IDEO

Tim Brown - IDEO

While there's still a lot of tweaking left to do, I think there's a lot of potential for me to use this as part of my project further down the line. I'd experimented with trying to post the results to Twitter, but I've not figured out how to parse the information correctly into 140 characters. This is something I'm going to keep tweaking through out my project. Ideally I'd love to add functionality so that people can tweet the service and get a response back - I think this will see me heading down the Twitter bot route. 

Big shout out to Codecademy's tutorial on the Watson API for getting me started and Digital Ocean's tutorial for getting me set up working in the terminal properly.