1
import json
from watson_developer_cloud import NaturalLanguageUnderstandingV1
import watson_developer_cloud.natural_language_understanding.features.v1 \
as Features

natural_language_understanding = NaturalLanguageUnderstandingV1(
username="username",
password="password",
version="2017-02-27")

response = natural_language_understanding.analyze(
text="IBM is an American multinational technology company headquartered \
in Armonk, New York, United States, with operations in over 170 \
countries.",
features=[
Features.Entities(
  emotion=True,
  sentiment=True,
  limit=2
),
Features.Keywords(
  emotion=True,
  sentiment=True,
  limit=2
)
 ]
 )

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

I am new to IBM watson API .....i was trying this sample code provided by them by I was getting this error

TypeError: Object of type 'Entities' is not JSON serializable

Radiodef
  • 37,180
  • 14
  • 90
  • 125
Pulkit Kedia
  • 83
  • 2
  • 10

3 Answers3

3

I fixed this issues by dumping response.result instead of just response.

The API guide wrongly says to use: print(json.dumps(response, indent=2))

When reviewing the docstring in the source code I found the DetailedResponsetype contains 'result, headers and HTTP status code'.

I think the example in the API documentation needs to be updated so it doesn't mislead people.

1

All depends on what you insert in your text parameter. Do you use the same text?

I used the example from the API reference with the same phrase for this answer... but, JSON knows only how to handle Unicode strings, not byte sequences. Either transform into Unicode (json.dumps(response.decode("utf-8"), indent=2)), or if is one integer array (json.dumps(list(response))). You also can try print(json.dumps(list(response.values()))).

So, this is one step-by-step for using the NLU service with Python.

IBM Cloud (New name for IBM Bluemix)

  • Create one account (Now, you can create without a credit card and use the LITE plan for Watson and other Services!)
  • Catalog -> Watson -> Natural Language Understanding service -> Create -> Service Credentials

In your PC, after installed Python, try to run the command in the CMD/Terminal:

pip install --upgrade watson-developer-cloud

Using the same code provided from the API reference:

import json
from watson_developer_cloud import NaturalLanguageUnderstandingV1
import watson_developer_cloud.natural_language_understanding.features.v1 \
  as Features

natural_language_understanding = NaturalLanguageUnderstandingV1(
  username="username from the NLU -> Service Credentials",
  password="passoword from the NLU -> Service Credentials",
  version="2017-02-27")

response = natural_language_understanding.analyze(
  text="IBM is an American multinational technology company headquartered \
    in Armonk, New York, United States, with operations in over 170 \
    countries.",
  features=[
    Features.Entities(
      emotion=True,
      sentiment=True,
      limit=2
    ),
    Features.Keywords(
      emotion=True,
      sentiment=True,
      limit=2
    )
  ]
)

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

And the return when I run the command python NLUAnalyze.py in CMD is:

{
  "usage": {
    "text_units": 1,
    "text_characters": 148,
    "features": 2
  },
  "language": "en",
  "keywords": [
    {
      "text": "American multinational technology",
      "sentiment": {
        "score": 0.0,
        "label": "neutral"
      },
      "relevance": 0.993518,
      "emotion": {
        "sadness": 0.085259,
        "joy": 0.026169,
        "fear": 0.02454,
        "disgust": 0.088711,
        "anger": 0.033078
      }
    },
    {
      "text": "New York",
      "sentiment": {
        "score": 0.0,
        "label": "neutral"
      },
      "relevance": 0.613816,
      "emotion": {
        "sadness": 0.166741,
        "joy": 0.228903,
        "fear": 0.057987,
        "disgust": 0.050965,
        "anger": 0.054653
      }
    }
  ],
  "entities": [
    {
      "type": "Company",
      "text": "IBM",
      "sentiment": {
        "score": 0.0,
        "label": "neutral"
      },
      "relevance": 0.33,
      "emotion": {
        "sadness": 0.085259,
        "joy": 0.026169,
        "fear": 0.02454,
        "disgust": 0.088711,
        "anger": 0.033078
      },
      "disambiguation": {
        "subtype": [
          "SoftwareLicense",
          "OperatingSystemDeveloper",
          "ProcessorManufacturer",
          "SoftwareDeveloper",
          "CompanyFounder",
          "ProgrammingLanguageDesigner",
          "ProgrammingLanguageDeveloper"
        ],
        "name": "IBM",
        "dbpedia_resource": "http://dbpedia.org/resource/IBM"
      },
      "count": 1
    }
  ]
}
Sayuri Mizuguchi
  • 5,250
  • 3
  • 26
  • 53
  • Thank you for responding ....i tried both the methods suggested by you......... (json.dumps(response.decode("utf-8"), indent=2)) and (json.dumps(list(response))) but none of them worked – Pulkit Kedia Nov 17 '17 at 14:53
  • is there any other way ?? – Pulkit Kedia Nov 17 '17 at 14:54
  • can you please show one part of what you want to send? and I'll see the type that you're use and try to help u – Sayuri Mizuguchi Nov 17 '17 at 15:07
  • I am sending the default text set by IBM reference code...."IBM is an American multinational technology company headquartered \ in Armonk, New York, United States, with operations in over 170 \ countries." ---------This one – Pulkit Kedia Nov 17 '17 at 15:51
  • Can you please copy my code and replace with your service credentials? Just for one test. – Sayuri Mizuguchi Nov 17 '17 at 15:54
  • but without the ( - ) in your username, just "219xxxc8xxxxxxxxxxxxxx31" and "cz7xxxxVsFp". What is your python version? – Sayuri Mizuguchi Nov 17 '17 at 16:33
  • please delete your comment with your credentials for your self security – Sayuri Mizuguchi Nov 17 '17 at 16:35
  • I had tried it without the ( - )................my version is python 3.6.3 – Pulkit Kedia Nov 17 '17 at 16:37
  • So, let's go to some steps... try to install the watson-developer-cloud again with the pip install in the `cmd`, then, create one new folder in your `Desktop`, install the Sublime Text (code editor), open and paste my CODE, and replace with your service credentials. After it, `Ctrl + S` and save with: `NLUExample.py`. Open your cmd and navigate to the folder in your desktop `cd Desktop/yourFolder`, and type the command: `python NLUExample.py` – Sayuri Mizuguchi Nov 17 '17 at 16:42
  • i tried "pip3 install --upgrade json " and i am getting an error...... "Could not find a version that satisfies the requirement json (from versions: ) No matching distribution found for json"............can this be a reason?? – Pulkit Kedia Nov 17 '17 at 16:50
  • In this case not, is just one message that don't have new version. So, if my steps didn't work, try to use this code `print(json.dumps(list(response.values())))` instead of `print(json.dumps(response, indent=2))`. If don't work yet, try to reinstall Python in your computer – Sayuri Mizuguchi Nov 17 '17 at 16:52
  • python3 archive.py --- > " python3: can't open file 'archive.py': [Errno 2] No such file or directory" – Pulkit Kedia Nov 17 '17 at 17:12
1

Got the solution from the IBM developer works here is the link

just replace

features=[
   Features.Entities(
          emotion=True,
          sentiment=True,
           limit=2
    ),
   Features.Keywords(
           emotion=True,
           sentiment=True,
           limit=2
    )
]

with :

features=Features(entities=EntitiesOptions(
                      emotion=True, sentiment=True,limit=2), 
               keywords=KeywordsOptions(
                      emotion=True, sentiment=True,limit=2
                                ))

this is due to the changes done in v 1 python sdk Here is the link showing the changes made in v 1 python sdk

Pulkit Kedia
  • 83
  • 2
  • 10