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
We also only want the tweets that are in English, so we’ll need to filter by language.
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:
- Creates an empty
textvariable we’ll save tweets into
- Filters out English tweets
- Appends UTF-8 encoded tweets to the
textvariable using the
Immediately after the
statuses variable assignment, create an empty string variable called
text. Use the example code above to help you.
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'
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!
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!