我正在尝试使用Watson Natural Language Understanding返回语料库中每个句子的实体。
(我无法生成完全可重现的代码,因为我使用的数据集是私有的。)
我的脚本如下所示:
list.Add(b);
list = list.OrderByDescending(i => i).ToList();
这在语料库中的大约第400个句子中工作正常,然后抛出以下错误:
import json
from watson_developer_cloud import NaturalLanguageUnderstandingV1
from watson_developer_cloud.natural_language_understanding_v1 import Features, EntitiesOptions
import pandas as pd
from utils import *
_DATA_PATH = "data/example_data.csv"
_IBM_NLU_USERNAME = "<username>"
_IBM_NLU_PASSWORD = "<password>"
X = [string1, string2, ... ]
nlu = NaturalLanguageUnderstandingV1(username=_IBM_NLU_USERNAME,
password=_IBM_NLU_PASSWORD,
version="2018-03-16")
def ibm_ner_recognition(sentence):
"""
Input -- sentence, string to conduct NER on
-- feats, this will always be entities
Return -- list of entities in the sentence
"""
response = nlu.analyze(text=sentence,
features=Features(entities=EntitiesOptions()))
output = json.loads(json.dumps(response))
entities = []
for result in output["entities"]:
entities.append(result["type"])
return entities
entities = []
for sent in X:
sent_entities = ibm_ner_recognition(sent)
entities.append(sent_entities)
我找到导致此内容的字符串如下:
WatsonApiException Traceback (most recent call last)
<ipython-input-57-8632adb20778> in <module>()
5 for sent in X:
6 print(sent)
----> 7 sent_entities = ibm_ner_recognition(sent)
8 entities.append(sent_entities)
9 end = t.default_timer()
<ipython-input-13-d132b33efc76> in ibm_ner_recognition(sentence)
9 """
10 response = nlu.analyze(text=sentence,
---> 11
features=Features(entities=EntitiesOptions()))
12 output = json.loads(json.dumps(response))
13 entities = []
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/watson_developer_cloud/natural_language_understanding_v1.py in analyze(self, features, text, html, url, clean, xpath, fallback_to_raw, return_analyzed_text, language, limit_text_characters, **kwargs)
202 params=params,
203 json=data,
--> 204 accept_json=True)
205 return response
206
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/watson_developer_cloud/watson_service.py in request(self, method, url, accept_json, headers, params, json, data, files, **kwargs)
446 error_info = self._get_error_info(response)
447 raise WatsonApiException(response.status_code, error_message,
--> 448 info=error_info, httpResponse=response)
WatsonApiException: Error: Server Error cannot analyze: downstream issue, Code: 500 , X-dp-watson-tran-id: gateway01-474786453 , X-global-transaction-id: 7ecac92c5aff58601c4caa95
我决定单独通过s = "Liz Saville Roberts."
运行此字符串。没有错误,它成功捕获了实体。
问题摘要
当我在语料库中循环时,我得到一个Watson NLU给我一个下游错误的句子。但是,Watson NLU在循环之外单独接收该句子时是成功的。
注意事项
这不是我语料库中的最后一句话。
根据我的付款计划,我没有用完Watson。
修改
我再次重新开始循环。这一次上面的句子有效(循环达到了大约第3500个字符串),所以似乎有一些气质行为。