【问题标题】:IBM Watson ToneAnalyzerV3 WatsonException: Error: Invalid JSON input, Code: 400IBM Watson ToneAnalyzerV3 WatsonException:错误:无效的 JSON 输入,代码:400
【发布时间】:2017-05-11 17:02:45
【问题描述】:

当我有短文本但我给它一个较长的文本时,下面的代码集可以工作:https://pastebin.com/PLinyN65

import os
import json
from watson_developer_cloud import ToneAnalyzerV3 
from glob import glob

with open(credentials.txt) as cred:
    data = cred.readlines()
    UserName = data[0]
    PassWord = data[1]

tone_analyzer = ToneAnalyzerV3(
                username=UserName,
                password=PassWord,
                version='2016-05-19')

pth = '../input/*.txt'
txtFiles = glob(pth)
for file in txtFiles:
    baseName = os.path.basename(file) 
    name = baseName.split('.')

with open(file) as f:
    txt = f.read()

    with open('../output/{}.json'.format(name[0]), 'w') as outFile:
        json.dump(tone_analyzer.tone(text=txt, sentences='false'), outFile, separators=(',', ': '), indent=2)

我得到错误:

WatsonException                           Traceback (most recent call last)
<ipython-input-6-d948e97e307e> in <module>()
      8 
      9         with open('../output/{}.json'.format(name[0]), 'w') as outFile:
---> 10             json.dump(tone_analyzer.tone(text=txt, sentences='false'), outFile, separators=(',', ': '), indent=2)
     11 
     12     print('{}\n'.format(name[0]))

/Library/Python/2.7/site-packages/watson_developer_cloud/tone_analyzer_v3.pyc in tone(self, text, tones, sentences)
     51         data = {'text': text}
     52         return self.request(method='POST', url='/v3/tone', params=params,
---> 53                             json=data, accept_json=True)

/Library/Python/2.7/site-packages/watson_developer_cloud/watson_developer_cloud_service.pyc in request(self, method, url, accept_json, headers, params, json, data, files, **kwargs)
    318             else:
    319                 error_message = self._get_error_message(response)
--> 320             raise WatsonException(error_message)

WatsonException: Error: Invalid JSON input, Code: 400

当我尝试在此网络链接上手动输入文本时:https://tone-analyzer-demo.mybluemix.net/

我得到错误:request entity too large

请告知我如何从 ToneAnalyzerV3 获得所需的 json 格式的音调分析分数。

【问题讨论】:

    标签: python json ibm-watson watson tone-analyzer


    【解决方案1】:

    查看 Tone Analyzer API Explorer,您可以看到每个请求可以处理的文本量是有限制的:

    音调分析器服务最多支持 128 KB 的文本,或大约 1000 句子。少于三个单词的句子不能被分析。

    Tone Analyzer API Explorer

    您提供的文本为 165,743 字节 (168 KB),比服务支持的要大。我尝试在 API Explorer 中输入上面提供的文本并收到与上面相同的错误:

    {
      "code": 400,
      "sub_code": "C00013",
      "error": "Invalid JSON input"
    }
    

    作为一种解决方案,您需要使用小于最大 128 KB 文本的文本输入。

    【讨论】:

      猜你喜欢
      • 2020-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-12
      • 2021-09-16
      • 1970-01-01
      相关资源
      最近更新 更多