在alchemy_language.entities

时间:2016-08-24 04:17:28

标签: python-2.7 python-3.x ibm-watson alchemyapi

我有这个继承的代码,它在Python 2.7中成功地返回xml中的结果,然后由ElementTree解析。

result = alchemyObj.TextGetRankedNamedEntities(text)

root = ET.fromstring(result)

我正在将程序更新到Python 3.5并尝试这样做,以便我不需要修改结果的xml解析:

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允许在json default和xml之间进行选择。但是,我收到了这个错误:

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'

outputMode实际上是否仍然存在?如果是这样,实体参数有什么问题?

1 个答案:

答案 0 :(得分:1)

watson-developer-cloud似乎没有实体的此选项。允许的设置是:

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

您可以尝试使用requests直接访问API。例如:

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

应该返回:

<?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>