【问题标题】:Is outputMode Still Supported In alchemy_language.entitiesalchemy_language.entities 中是否仍支持 outputMode
【发布时间】:2016-12-31 01:25:52
【问题描述】:

我有这个继承的代码,它在 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)

根据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 真的还存在吗?如果是这样,entities 参数有什么问题?

【问题讨论】:

  • 您在使用 watson_developer_cloud 吗?

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


【解决方案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>

【讨论】:

    猜你喜欢
    • 2012-12-21
    • 2018-08-02
    • 2018-09-02
    • 2021-01-12
    • 2011-12-24
    • 1970-01-01
    • 2021-01-16
    • 2017-11-20
    • 1970-01-01
    相关资源
    最近更新 更多