Applying Alchemy API

In order to use the Alchemy API we will first create a function that makes the requests to the service:

import requests 
import json 
 
apikey = "YOUR_KEY" 
 
url = "https://gateway-a.watsonplatform.net/calls/text/TextGetEmotion" 
 
def get_alchemy(apikey,text): 
    pms = {'apikey' : apikey, 'outputMode' : 'json', "text" : text} 
    result = requests.post(url, data = pms) 
    try: 
        emotions = json.loads(result.text)['docEmotions'] 
    except: 
        emotions = None 
    return(emotions) 

The free version of the alchemy API is limited to 1000 requests per month. For our analysis, we will take the first 1000 comments:

alchemy = df_comments.head(1000) 

Then, we apply the alchemy API on each message in our dataset using the apply method. We store the results in a column called 'alchemy':

alchemy['alchemy'] = alchemy.apply(lambda x: get_alchemy(apikey,x['message']),axis=1) 

Emotions are returned in the JSON format, which is stored as a string in the dataframe. We will create a new dataframe containing only the results of emotions predictions:

emotions = [] 
 
for p in alchemy['alchemy']: 
   if not p == None: 
        emotions.append(p) 
 
df_emotions = pd.DataFrame(emotions) 

Then, we convert all the values into a numeric data type:

df_emotions = df_emotions.apply(pd.to_numeric, errors='ignore') 

Next, we compute a mean for each emotion:

df_emotions_means = df_emotions.transpose().apply(np.mean,axis=1) 

In the end, we can visualize the results:

df_emotions_means.plot(kind='bar', legend=False)  

The following histogram shows the distribution of the emotions that we extracted. It is a list of positive and negative emotions:

We see that the sadness emotion is the biggest chunk; this could be due to the period of analysis that had lots of negative events in the world. We also see that joy is the next biggest emotion. We will now take a look at some of the example verbatims and see the type of emotion (with the score) it is associated with:

  • "Superb!" Joy 0.87
  • y sister gave me one for X-mas but I won't use it because it gives Google 24/7 access to listen to you without you knowing it. It's a microphone into your life. Google watches every thing you do already they just put up servers about 5 miles away and they ran fiber optics to the air force base. Google is not a good search engine if you want to be private. Google knows your location all the time just like a cell phone. I won't buy a cell phone that you can't take the sim card out of Sadness 0.66
  • You do not build up what is wrong nor encourage someone to do wrong. When they realize it was wrong they will hate u for going along with it the gay lifestyle is destructive. Disgust 0.66
  • "Hey Google, do you want feedback on your products or not? I don't see a clear and easy way to do this." Anger 0.26
  • "I thought I asked you to take Bartons Pet Boarding House off of your company's list? Now you send me a junk text that you are adding that company to the Review Boosterapp? Even on News Years Day when no one should really be working!!" Fear 0.27
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset