Emotion Analysis Using Watson NLU

IBM Watson Natural Language Understanding (NLU) offers a powerful tool for emotion analysis, enabling users to detect specific emotions from text data, such as joy, anger, sadness, fear, or disgust. Here is a stepwise illustration of how the Watson NLU service can be used to perform emotion analysis:

  1. Setting Up the Watson NLU Service: Login to your IBM Cloud account, navigate to the Watson services in the IBM Cloud catalog, select Watson Natural Language Understanding, and create a new service instance. If you do not have an IBM Cloud account, refer to the instructions in Section Accessing IBM Cloud Services to create an account and access services. For learning purposes, beginners should consider the “Start free plan”.
  2. Prepare Text Data: Provide the text that you want to analyse for emotions. The example in Exhibit 25.24 uses the sample text: "iphone 16 is truly amazing! I’ve never been happier with a purchase. But the delivery was very late, and that was disappointing."
  3. Make an API Request: With your API key and service URL, you can make a request to the Watson NLU service for emotion analysis. Exhibit 25.24 provides an example of how you can do this using Python.
  4. Review the Output: The response from Watson NLU will include the emotion scores for the entire document, as well as for individual sentences or key sections of the text.
  5. Interpreting the Results:
    • Joy: The score is 0.66, indicating a strong positive emotion, likely associated with the praise for the iPhone.
    • Sadness: The score of 0.31 reflects the user’s disappointment with the delayed delivery.
    • Other Emotions: Disgust, anger, and fear have lower scores, indicating these emotions are less dominant in the text.

By using Watson NLU for emotion analysis, businesses and researchers can automate the understanding of human emotions, allowing them to make data-driven decisions and respond more effectively to customer needs and market trends.

Emotions Analysis using IBM Watson Natural Language Understanding
import requests
import json
from requests.auth import HTTPBasicAuth

# Watson NLU API details
api_key = 'YOUR_API_KEY'
url = 'https://api.eu-gb.natural-language-understanding.watson.cloud.ibm.com/instances/e1560be7-a6db-4e1c-aea5-82898f2487be/v1/analyze?version=2021-08-01'

# Text to be analysed
text = '''
iphone 16 is truly amazing! I’ve never been happier with a purchase. 
But the delivery was very late, and that was disappointing.
'''
# Specify the features to be analysed - in this case, emotion
data = {
    "text": text,
    "features": {
        "emotion": {}
    }
}

# Make the API request with Basic Authentication
response = requests.post(url, auth=HTTPBasicAuth('apikey', api_key), json=data)

# Parse the response
emotion_data = response.json()
print(json.dumps(emotion_data, indent=2)) 
{
  "usage": {
    "text_units": 1,
    "text_characters": 131,
    "features": 1
  },
  "language": "en",
  "emotion": {
    "document": {
      "emotion": {
        "sadness": 0.307645,
        "joy": 0.66182,
        "fear": 0.033113,
        "disgust": 0.02233,
        "anger": 0.022124
      }
    }
  }
}

Exhibit 25.24 Emotions Analysis using IBM Watson Natural Language Understanding. Jupyter notebook.


Previous     Next

Use the Search Bar to find content on MarketingMind.