使用 Python 从 IBM Watson Concept Insights 调用 annotate_text

Calling annotate_text from IBM Watson Concept Insights with Python

我写了一组 Python 函数来与 Bluemix/Watson Concept Insights API. I am able to generate a token and use it to get a result from the server, but the result stinks: it's nowhere near as good as what I get when I plug the same information into their Swagger testing utility 交互。

我怀疑我发送请求的方式有问题,但我不太清楚是什么问题。代码如下。首先,来自 event_insight_lib.py:

def importCredentials(filename='credentials.json'):
    if filename in [f for f in os.listdir('.') if os.path.isfile(f)]:
        data = json.load(open(filename))['concept_insights'][0]['credentials']
        return data

def generateToken(filename='credentials.json'):
    credentials = importCredentials(filename)
    r = requests.get("https://gateway.watsonplatform.net/authorization/api/v1/token\?url=https://stream.watsonplatform.net/concept-insights/api", auth=(credentials['username'], credentials['password']))
    if r.status_code == requests.codes.ok:
        return r.text

def annotateText(text, token, content_type = 'text/plain'):
    base_url='https://watson-api-explorer.mybluemix.net/concept-insights/api/v2/graphs/wikipedia/en-20120601/annotate_text'
    headers = {'X-Watson-Authorization-Token': token, 'Content-Type': content_type}
    r = requests.post(base_url, headers=headers, data={'body': text})
    return r.text

这些方法由event_insight.py执行:

token = event_insight_lib.generateToken()
ret = event_insight_lib.annotateText("""long string being concept-analyzed...""", token)
    print(ret)

输出差异的完整演示是 here. The full codebase is here。我对 Requests 库不是很有经验:Pythonic 端是否存在细微错误?

IBM 文档的相关部分是 here

正如@engineerc 所建议的那样,您将 dict() 作为 data 发送。引用您的评论 data=text.encode(encoding='UTF-8', errors='ignore') 是您问题的解决方案。


另一方面,请不要使用 https://watson-api-explorer.mybluemix.net,它是我们用来托管 swagger 文档的代理应用程序。
服务 url 是: https://gateway.watsonplatform.net/concept-insights/api

此外,我们有一个 python-sdk 支持 ConceptInsightsannotate_text 调用。

这是一个 pip 模块,所以你会做:

pip install watson-developer-cloud

调用annotate_text很简单:

import json
from watson_developer_cloud import ConceptInsightsV2 as ConceptInsights


concept_insights = ConceptInsights(
    username='YOUR SERVICE USERNAME',
    password='YOUR SERVICE PASSWORD')

annotations = concept_insights.annotate_text('IBM Watson won the Jeopardy television show hosted by Alex Trebek')
print(json.dumps(annotations, indent=2))