0

Can anyone please help me printing out a significant error message? Here's the situation:

I am trying to write a function which will use the DBpedia Spotlight REST API to analyze a text. After some trial and error I have discovered that when the text is more than approx 950 words, the request fails with a 414 return code.

The code I wrote (including error handling) is the following:

def do_spotlight_nlp(analytext: str) -> Optional[dict]:
    """Tries linking persontext to person in DBpedia"""
    texttofind = quote_plus(analytext)
    headers = {"Accept": "application/json"}
    request_url = (
        "http://api.dbpedia-spotlight.org/en/annotate?"
        f"text={texttofind}&confidence=0.5&support=1"
    )  # &types=DBpedia:Person to limit to Persons
    try:
        response = requests.get(
            url=request_url, headers=headers, timeout=5
        ).json()
        response.raise_for_status()
    except HTTPError as requestserror:
        articlelen = len(re.findall(r"\w+", analytext))
        print(f"Article is too long: {articlelen} words!")
        raise SystemExit(requestserror) from requestserror
    if len(response) == 7:
        return response

but I am not succeeding in printing out the root cause (HTTP 414) of the problem but rather get errors such as the following:

Traceback (most recent call last):
  File "/Users/bob/Documents/work/code/memaback/.venv/lib/python3.10/site-packages/requests/models.py", line 971, in json
    return complexjson.loads(self.text, **kwargs)
  File "/Users/bob/.pyenv/versions/3.10.9/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/Users/bob/.pyenv/versions/3.10.9/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Users/bob/.pyenv/versions/3.10.9/lib/python3.10/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/bob/Documents/work/code/memaback/src/memaback/mema_nlp_ensemble_experiment.py", line 308, in <module>
    main()
  File "/Users/bob/Documents/work/code/memaback/src/memaback/mema_nlp_ensemble_experiment.py", line 304, in main
    process_article(article_url)
  File "/Users/bob/Documents/work/code/memaback/src/memaback/mema_nlp_ensemble_experiment.py", line 279, in process_article
    spotresponse = do_spotlight_nlp(articledict["articletext"])
  File "/Users/bob/Documents/work/code/memaback/src/memaback/mema_nlp_ensemble_experiment.py", line 178, in do_spotlight_nlp
    ).json()
  File "/Users/bob/Documents/work/code/memaback/.venv/lib/python3.10/site-packages/requests/models.py", line 975, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Robert Alexander
  • 875
  • 9
  • 24

0 Answers0