Skip to Content
Learn
Using the Twitter API
Preparing Results for Analysis

Great! Now that we have the text we need, it’s time to prepare it so that we can send it to the Personality Insights (PI) API for analysis.

First, we’ll concatenate the text into one long string and then send it off to PI be analyzed. We’ll save the long string into a variable called text.

We also only want the tweets that are in English, so we’ll need to filter by language.

Note: The text retrieved from Twitter is in Unicode format, but we need UTF-8 format, so we’ll need to encode it. Thankfully, the encode() method in Python solves that problem, so we’ll use that.

Here’s how the modified code looks:

statuses = twitter_api.GetUserTimeline(screen_name=handle, count=200, include_rts=False) text = "" for status in statuses: if (status.lang =='en'): #English tweets text += status.text.encode('utf-8')

The code above does the following:

  1. Creates an empty text variable we’ll save tweets into
  2. Filters out English tweets
  3. Appends UTF-8 encoded tweets to the text variable using the += operator

Instructions

1.

Immediately after the statuses variable assignment, create an empty string variable called text. Use the example code above to help you.

2.

Modify the for loop by adding an if statement that filters the English language Tweets.

The JSON object retrieved from Twitter contains a lang property. We can filter by language by using status.lang and setting it equal to the two character abbreviation of the desired language (in this case, English):

status.lang == 'en'
3.

Inside of the if block, loop, use the += operator to add the encoded Tweets to the text variable. This will create the long string of text that will be sent to PI. Use the example above to help you.

Don’t worry if the output terminal to the right is empty. It’s not supposed to print anything at the moment!

4.

Congratulations! You’ve successfully built the first critical component of the Celebrity Match application: data retrieval from Twitter using the Twitter API.

To build the rest of the application, you’ll build the second important component: sending the Twitter data to the Personality Insights (PI) API for insight analysis.

In the next unit, you’ll get set up with IBM’s Bluemix and PI so that you can complete the Celebrity Match application!

Folder Icon

Take this course for free

Already have an account?