2016-08-24 86 views
1

我有這個繼承的代碼,它在Python 2.7中成功返回XML中的結果,然後由ElementTree進行分析。是outputMode仍然受支持在alchemy_language.entities

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默認的選擇和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實際上是否仍然存在?如果是這樣,實體參數有什麼問題?

+0

是否使用watson_developer_cloud? –

回答

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>