我编写了一组 Python 函数来与 Bluemix/Watson 交互 Concept Insights API 。我能够生成一个 token 并使用它从服务器获取结果,但结果很糟糕:它远不如我将相同的信息插入到他们的 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 。完整的代码库是here 。我对 Requests 库不太有经验:Pythonic 端是否有一个微妙的错误?
IBM 文档的相关部分是 here .
最佳答案
正如 @engineerc 建议的那样,您将 dict()
作为 data
发送。引用您的评论 data=text.encode(encoding='UTF-8', error='ignore')
是您问题的解决方案。
另一方面,请不要使用 https://watson-api-explorer.mybluemix.net
,它是我们用来托管swagger 文档。
服务网址为:
https://gateway.watsonplatform.net/concept-insights/api
此外,我们还有一个支持 ConceptInsights
的 python-sdk以及 annotate_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))
关于python - 使用 Python 从 IBM Watson Concept Insights 调用 annotate_text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33194181/