Sample program not working

I tried the sample program

import language_tool
lang_tool = language_tool.LanguageTool(“en-US”)
text = “A sentence with a error in the Hitchhiker’s Guide tot he Galaxy”
matches = lang_tool.check(text)
len(matches)

but it’s giving errors

Traceback (most recent call last):
File “C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\language_tool-0.3-py3.8.egg\language_tool_init_.py”, line 490, in get_languages
languages = cache[“languages”]
KeyError: ‘languages’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\language_tool-0.3-py3.8.egg\language_tool_init_.py”, line 310, in _get_root
with urlopen(url, data, cls._TIMEOUT) as f:
File “C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py”, line 222, in urlopen
return opener.open(url, data, timeout)
File “C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py”, line 531, in open
response = meth(req, response)
File “C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py”, line 640, in http_response
response = self.parent.error(
File “C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py”, line 569, in error
return self._call_chain(*args)
File “C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py”, line 502, in _call_chain
result = func(*args)
File “C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py”, line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “E:/partha/langtool_test.py”, line 2, in
lang_tool = language_tool.LanguageTool(“en-US”)
File “C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\language_tool-0.3-py3.8.egg\language_tool_init_.py”, line 194, in init
self.language = LanguageTag(language)
File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\language_tool-0.3-py3.8.egg\language_tool_init
.py", line 411, in new
return str.new(cls, cls.normalize(tag))
File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\language_tool-0.3-py3.8.egg\language_tool_init
.py", line 431, in normalize
languages = {l.lower().replace(“-”, "
“): l for l in get_languages()}
File “C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\language_tool-0.3-py3.8.egg\language_tool_init_.py”, line 492, in get_languages
languages = LanguageTool.get_languages()
File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\language_tool-0.3-py3.8.egg\language_tool_init
.py”, line 290, in _get_languages
for e in cls.get_root(url, num_tries=1):
File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\language_tool-0.3-py3.8.egg\language_tool_init
.py", line 316, in _get_root
raise Error(“{}: {}”.format(cls._url, e))
language_tool.Error: http://127.0.0.1:8081: HTTP Error 400: Bad Request

Process finished with exit code 1

Why???

Hi,

The problem with language_tool and language_check python LT wrappers is they were written to read the responses in XML, which isn’t supported by the new versions of LanguageTool. Right now, LT uses JSON-based format.

The walkaround I used to fix this:

  • download an older version of LanguageTool, namely LanguageTool-3.5.zip (the last version that supported XML)
  • go to site-packages/language_tool folder on your PC (the place where your local python packages reside)
  • replace the current LanguageTool version with the one you downloaded

Please keep in mind that older versions may not provide all the current functionality.