2

我正在尝试使用 Watson Natural Language Understanding 返回语料库中每个句子的实体。

(我无法生成完全可重现的代码,因为我使用的数据集是私有的。)

我的脚本如下所示:

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)

这可以正常工作到语料库中的第 400 句,然后抛出以下错误:

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."

我决定自己运行这个字符串ibm_ner_recognition。没有错误,它成功捕获了实体。

问题总结

循环浏览我的语料库时,我遇到了一个句子,其中 Watson NLU 给了我一个下游错误。但是,当在循环之外自行接收该句子时,Watson NLU 是成功的。

注意事项

  1. 这不是我语料库中的最后一句话。

  2. 根据我的付款计划,我没有用完 Watson。

编辑

我再次重新运行循环。这次上面的句子起作用了(循环达到了大约第 3500 个字符串),所以似乎有一些喜怒无常的行为。

4

0 回答 0