Page 11 - MSDN Magazine, May 2018
P. 11
Note that the algorithm correctly identi- fied that the contents of simple_text1 had a negative sentiment (-0.8) and that the state- ment is quite subjective (0.9). Additionally, the algorithm correctly inferred the posi- tive sentiment of simple_text2 (0.625) and its highly subjective nature (1.0).
However, the algorithm does have sig- nificant difficulties parsing the more subtle nuances of human language. Sarcasm, for instance, is not only hard to detect, but may throw off the results. Imagine a scenario where a restaurant extracts reviews from an online review site like Yelpandautomaticallypublishesreviews with a positive sentiment on their Web site and social media. Enter the following code into an empty cell and execute it:
sample_customer_review1 =
TextBlob("The burgers at this place will make you ill with joy.")
print(sample_customer_review1.sentiment)
sample_customer_review2 = TextBlob("Whenever I want to take a sick day, I eat here the night before and it is always a sure fire win!")
print(sample_customer_review2.sentiment)
Clearly the sentiment of these two reviews are negative. Yet the algorithm seems to think otherwise, with both reviews scoring as having positive senti- ment scores, 0.15 and 0.26 respectively. In this case, the restaurant would likely not want either of these reviews high- lighted on any platform. NLP systems have yet to grasp a good understanding of sarcasm, although there is a lot of research currently being done in this area (thesarcasmdetector.com/about).
Connecting to Twitter
So far, I have only run small bits of text through the TextBlob analyzer. A more practical use of this technology is to feed it user-generated data, ideally in near-real time. Fortunately Twitter, with its approxi- mately 327 million active users (bit.ly/2E3lYKl), provides a constant stream of text to analyze.
To connect with Twitter’s API, I need to register an application with Twitter to generate the necessary credentials. In a browser, go to apps.twitter.com and, if needed, log in with your Twitter credentials. Click the Create New App button to bring up the Create an application form as shown in Figure 2. Enter a name, description and a Web site for the app. For the purposes msdnmagazine.com
Figure 2 The Twitter Create Application Form
Figure 3 Twitter Application Keys and Access Tokens Screen
May 2018 7