LanguageTool Python Wrapper
language_tool_python is an actively maintained Python wrapper for LanguageTool, an open-source grammar, style, and spell checker. It facilitates grammar checking by either running a local LanguageTool Java server, utilizing the public LanguageTool API, or connecting to a custom remote LanguageTool server. The library sees regular minor and patch releases, ensuring active development and updates.
Common errors
-
FileNotFoundError: [Errno 2] No such file or directory: 'java'
cause The Java Runtime Environment (JRE) is either not installed or not correctly configured in your system's PATH, which is required to run the local LanguageTool server.fixInstall a compatible JRE (Java 17 or newer is recommended for recent LanguageTool versions) and ensure the `java` executable is available in your system's PATH. On Linux/macOS, check `java --version`. On Windows, ensure Java's `bin` directory is in the Path environment variable. -
ModuleNotFoundError: No module named 'language_tool_python'
cause The `language-tool-python` library is not installed in your current Python environment, or you are running the script in a different environment.fixInstall the library using `pip install language-tool-python`. If already installed, verify your Python environment by checking `pip list` or `conda list`. -
ValueError: proxies works only with remote_server
cause You have provided the `proxies` argument to `language_tool_python.LanguageTool` without also specifying the `remote_server` argument. Proxies are only used for remote HTTP connections.fixRemove the `proxies` argument if you intend to use a local LanguageTool server, or add the `remote_server` argument with a valid URL if you intend to use a remote server with proxies. -
AttributeError: 'Match' object has no attribute 'some_invalid_attribute'
cause You are trying to access an attribute on a `Match` object that does not exist or has a different name than expected. This can happen from typos or outdated examples.fixRefer to the official documentation or the `Match` object's `__dict__` for available attributes. Common attributes include `ruleId`, `message`, `replacements`, `offset`, `errorLength`, `context`, `category`.
Warnings
- gotcha A Java Runtime Environment (JRE) is required on your system to run the local LanguageTool server (the default mode). Specific Java versions are required depending on the LanguageTool version: Java >= 9 for LT < 6.6, and Java >= 17 for LT >= 6.6.
- gotcha The `proxies` parameter in the `LanguageTool` constructor is only effective when `remote_server` is also specified. Passing `proxies` without `remote_server` will raise a `ValueError`.
- deprecated Using the public LanguageTool API (`LanguageToolPublicAPI`) is subject to rate limits. For intensive or production use, it is highly recommended to run your own local LanguageTool server or connect to a custom remote server.
- breaking As of April 2020 (starting with version 2.x), `language-tool-python` no longer supports LanguageTool server versions older than 4.0.
Install
-
pip install language-tool-python
Imports
- LanguageTool
import language_tool_python.LanguageTool
from language_tool_python import LanguageTool
- LanguageToolPublicAPI
from language_tool_python import PublicAPI
from language_tool_python import LanguageToolPublicAPI
Quickstart
import language_tool_python
# For local server (requires Java JRE installed)
# Ensure Java Runtime Environment (JRE) is installed and accessible in your PATH
try:
with language_tool_python.LanguageTool('en-US') as tool:
text = "This are a example of bad grammer."
matches = tool.check(text)
print("Original text:", text)
print("Detected errors:", matches)
corrected_text = tool.correct(text)
print("Corrected text:", corrected_text)
except Exception as e:
print(f"Could not initialize local LanguageTool server: {e}")
print("Trying public API...")
# For public LanguageTool API
# Note: Public API has rate limits. For heavy usage, consider a local/remote server.
try:
with language_tool_python.LanguageToolPublicAPI('en-US') as tool:
text_public = "He don't like it."
matches_public = tool.check(text_public)
print("\nOriginal text (Public API):", text_public)
print("Detected errors (Public API):", matches_public)
corrected_text_public = tool.correct(text_public)
print("Corrected text (Public API):", corrected_text_public)
except Exception as e:
print(f"Could not use public LanguageTool API: {e}")
print("Check your internet connection or API rate limits.")