python - 尝试调用 AlchemyLanguage API

标签 python api ibm-cloud alchemyapi

我已经编写了用 Python 调用 Bluemix 的 AlchemyLanguage API 的代码。我需要关键字和实体,但它仅显示文本文件的第一个关键字和第一个实体。我哪里出错了?

import requests    
import urllib    
import urllib2

def call_alchemy_api(text, API_KEY):

    payload = {'outputMode':'json','extract':'entities,keywords','sentiment':'1','maxRetrieve':'1', 'url':'https://www.ibm.com/us-en/'}
    payload['apikey'] = API_KEY
    encoded_text = urllib.quote_plus(text)
    payload['text'] = text
    data = urllib.urlencode(payload)
    url = 'https://gateway-a.watsonplatform.net/calls/text/TextGetCombinedData'
    req = urllib2.Request(url, data)
    response = urllib2.urlopen(req)
    return response

if __name__ == "__main__":

    api_key = 'xxxxxxxxxxxxxxxxxxxxxmyapi'
    f = open('in0.txt','r')
    text = f.read()

    print text
    response = call_alchemy_api(text, api_key)
    print response.read()

最佳答案

更改 maxRetrieve 关键字的值。

示例:

payload = {'outputMode':'json','extract':'entities,keywords','sentiment':'1','maxRetrieve':'3', 'url':'https://www.ibm.com/us-en/'}

API链接:

http://www.ibm.com/watson/developercloud/alchemy-language/api/v1/

关于python - 尝试调用 AlchemyLanguage API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38304159/

相关文章:

python - 如何在正则表达式搜索中排除模式字符串

python - 从 apache 日志中删除 2 次后获取 IP 的正则表达式

android - Android Youtube视频上传

ibm-cloud - 如何更改 Bluemix 文档?

ios - 调用中的额外参数 'username' - WatsonCloud SpeechToTextV1

python - 为什么 pip 不安装最新版本的包,即使 PyPI 上有更新版本?

python - 如何跨多列循环类似的 python pandas 代码

javascript - 如何同时追加和格式化行 Google Sheet API

javascript - 如何在我的 Viber 机器人中使用 bot.getUserDetails

db2 - 有什么方法可以将 DB2 备份文件恢复到 IBM DashDB 上吗?