0

I am trying to work with the IBM Tone sentiment API but having difficulties getting started. I am following the documentation shown in https://cloud.ibm.com/apidocs/tone-analyzer?code=python. After running the following code:

pip install --upgrade "ibm-watson>=6.0.0"
from ibm_watson import ToneAnalyzerV3

I get the following error: No module named 'ibm_watson.natural_language_understanding_v3'

Even when trying to access Watson Tone Analyzer Customer Engagement endpoint to Natural Language Understanding (https://cloud.ibm.com/docs/natural-language-understanding?topic=natural-language-understanding-tone_analytics). On the page it refers me back to the initial tone analysis page. enter image description here

I would appreciate some guidance on how to get started with the IBM on any tone analysis endpoint

Bemz
  • 129
  • 1
  • 16
  • 1
    In the [link](https://cloud.ibm.com/apidocs/natural-language-understanding?code=python) you provided they said that "As of 24 February 2022, the Tone Analyzer tile will be removed from the IBM Cloud® Platform for new customers; only existing customers will be able to access the product. The service will no longer be available as of 24 February 2023." – Angerato Sep 28 '22 at 15:34
  • I understand hence I am trying to access Watson Tone Analyzer Customer Engagement endpoint thtrough Natural Language Understanding. I have outlined this in the above question @Angerato – Bemz Sep 28 '22 at 16:44

1 Answers1

1

I would recommend to use the NLU module directly. If you go to the REST API documentation for NLU Tone, on the right you can see sample code you can use.

It also explains in a link how to use the Tone model that replaces Tone Analyzer.

Basically you need to reference the model tone-classifications-XX-v1 where XX is either en or fr.

Here is some sample code and output based off of the documentation.

Change API and URL to the values in the NLU instance service page-> Manage tab.

import json
from ibm_watson import NaturalLanguageUnderstandingV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
from ibm_watson.natural_language_understanding_v1 import Features, ClassificationsOptions

apikey = 'APIKEY'
url = 'URL'

# Only 'en' and 'fr' available at the time of writing this.
language = 'en'
tone_model = f'tone-classifications-{language}-v1'

text = 'This is awesome! Thanks a lot! I am so happy this solved my problem.'

authenticator = IAMAuthenticator(apikey)
nlu = NaturalLanguageUnderstandingV1(
    version='2022-04-07',
    authenticator=authenticator
)

nlu.set_service_url(url)

response = nlu.analyze(
    text=text,
    features=Features(classifications=ClassificationsOptions(model=tone_model))).get_result()

print(json.dumps(response, indent=2))

This should generate something like this:

{
  "usage": {
    "text_units": 1,
    "text_characters": 68,
    "features": 1
  },
  "language": "en",
  "classifications": [
    {
      "confidence": 0.867106,
      "class_name": "satisfied"
    },
    {
      "confidence": 0.729703,
      "class_name": "excited"
    },
    {
      "confidence": 0.283219,
      "class_name": "polite"
    },
    {
      "confidence": 0.154289,
      "class_name": "sympathetic"
    },
    {
      "confidence": 0.029122,
      "class_name": "sad"
    },
    {
      "confidence": 0.013206,
      "class_name": "frustrated"
    },
    {
      "confidence": 0.005977,
      "class_name": "impolite"
    }
  ]
}
Simon O'Doherty
  • 9,259
  • 3
  • 26
  • 54