我有以下代码,其中的用户名,密码和URL值已更新为我直接从IBM Watson Service凭据复制的值。出于某种原因,我得到以下错误。
代码:
import requests
import json
def analyze_tone(text):
username = 'username-copied-from-watson'
password = 'service-password-copied-from-watson'
watsonUrl = 'https://gateway.watsonplatform.net/tone-analyzer/api'
headers = {"content-type": "text/plain"}
data = text
try:
r = requests.post(watsonUrl, auth=(username,password),headers = headers,
data=data)
return r.text
except:
return False
def welcome():
message = "Welcome to the IBM Watson Tone Analyzer\n"
print(message + "-" * len(message) + "\n")
message = "How it works"
print(message)
message = "Perhaps a bit too aggressive in your emails? Are your blog posts a little too friendly? Tone Analyzer might be able to help. The service uses linguistic analysis to detect and interpret emotional, social, and writing cues found in text."
print(message)
print()
print("Have fun!\n")
def display_results(data):
data = json.loads(str(data))
print(data)
for i in data['document_tone']['tone_categories']:
print(i['category_name'])
print("-" * len(i['category_name']))
for j in i['tones']:
print(j['tone_name'].ljust(20),(str(round(j['score'] * 100,1)) + "%").rjust(10))
print()
print()
def main():
welcome()
data = input("Enter some text to be analyzed for tone analysis by IBM Watson (Q to quit):\n")
if len(data) >= 1:
if data == 'q'.lower():
exit
results = analyze_tone(data)
if results != False:
display_results(results)
exit
else:
print("Something went wrong")
main()
错误:
Enter some text to be analyzed for tone analysis by IBM Watson (Q to quit):
"I love hate you"
{u'sub_code': u'C00010', u'code': 404, u'error': u'Not Found'} Traceback (most recent call last): File "test.py", line 58, in <module> main() File "test.py", line 53, in main
display_results(results)
答案 0 :(得分:1)
您的代码似乎存在一些问题。 API for the Watson Tone Analyzer service将以下列为API端点:
https://gateway.watsonplatform.net/tone-analyzer/api/v3/{method}
您错过了第3版v3
,但我没有看到您提供任何方法。您还需要传递API版本。
我建议使用official Python SDK for the IBM Watson services。当您浏览该GitHub仓库时,您会发现example for the tone analyzer。