Is outputMode Still Supported In alchemy_language.

2019-07-25 12:17发布

问题:

I have this inherited code which in Python 2.7 successfully returns results in xml that are then parsed by ElementTree.

result = alchemyObj.TextGetRankedNamedEntities(text)

root = ET.fromstring(result)

I am updating program to Python 3.5 and am attempting to do this so that I don't need to modify xml parsing of results:

result = alchemy_language.entities(outputMode='xml', text='text', max_
items='10'),

root = ET.fromstring(result)

Per http://www.ibm.com/watson/developercloud/alchemy-language/api/v1/#entities outputMode allows the choice between json default and xml. However, I get this error:

Traceback (most recent call last):
  File "bin/nerv35.py", line 93, in <module>
    main()
  File "bin/nerv35.py", line 55, in main
    result = alchemy_language.entities(outputMode='xml', text='text', max_items='10'),
TypeError: entities() got an unexpected keyword argument 'outputMode'

Does outputMode actually still exist? If so, what is wrong with the entities parameters?

回答1:

The watson-developer-cloud does not appear to have this option for Entities. The settings allowed are:

html
text
url
disambiguate
linked_data
coreference
quotations
sentiment
show_source_text
max_items
language
model

You can try accessing the API directly by using requests. For example:

import requests

alchemyApiKey = 'YOUR_API_KEY'
url = 'https://gateway-a.watsonplatform.net/calls/text/TextGetRankedNamedEntities'

payload = { 'apikey': alchemyApiKey,
            'outputMode': 'xml',
            'text': 'This is an example text. IBM Corp'
           }

r = requests.post(url,payload)

print r.text

Should return this:

<?xml version="1.0" encoding="UTF-8"?>
<results>
    <status>OK</status>
    <usage>By accessing AlchemyAPI or using information generated by AlchemyAPI, you are agreeing to be bound by the AlchemyAPI Terms of Use: http://www.alchemyapi.com/company/terms.html</usage>
    <url></url>
    <language>english</language>
    <entities>
        <entity>
            <type>Company</type>
            <relevance>0.961433</relevance>
            <count>1</count>
            <text>IBM Corp</text>
        </entity>
    </entities>
</results>