pyLanguagetool 0.11.0
repository·master·Indexed 41 days ago
https://github.com/findus23/pylanguagetoolA Python library and CLI tool that interfaces with the LanguageTool JSON API to provide advanced spellchecking and grammar support for multiple languages. It includes an HTTP interface via the pylanguagetool.api module, a command-line interface for checking text from files, pipes, or the clipboard, and converters for preparing text formats like HTML.
What's inside pyLanguagetool
- pyLanguagetool provides a Pythonic interface and a Command Line Interface (CLI) for the LanguageTool JSON API. It allows users to perform advanced grammar and spellchecking across a wide variety of supported languages.
Configure pyLanguagetool via CLI, Env Vars, or Config File
masterConfiguration follows a specific hierarchy: Command-line arguments > Environment variables > Configuration file (
~/.config/pyLanguagetool.conf) > Defaults.Configuration File
The config file uses
ConfigArgParsesyntax. Supported formats include:key=valueflag=truestuff=[a,b,c]
Privacy and Custom Servers
By default, text is sent via HTTPS to the official LanguageTool server. To use your own local or private server, use the
--api-urlattribute (ensure it ends with/v2/).Install pyLanguagetool
masterpyLanguagetool is a Python library and CLI tool that interfaces with the LanguageTool JSON API for spellchecking and grammar support. You can install it using standard Python package managers.
# Using pip pip install pylanguagetool # Or using pipenv pipenv install pylanguagetoolUse the pyLanguagetool CLI for basic text checking
masterThe CLI can check text provided via pipes, files, stdin, or the system clipboard. It returns a list of detected errors and possible replacements.
Usage Patterns
- Pipe text:
echo "text" | pylanguagetool - Read from file:
pylanguagetool filename.txt - Read from stdin:
pylanguagetool < filename.txt - Read from clipboard:
pylanguagetool -c
# pipe text to pylanguagetool echo "This is a example" | pylanguagetool # read text from a file pylanguagetool textfile.txt # read text from stdin pylanguagetool < textfile.txt # read text from the systems clipboard pylanguagetool -c- Pipe text:
Handle LaTeX files with pylanguagetool
masterThe CLI does not support
.texfiles directly. To check LaTeX content, you should convert it to plain text first using a tool likedetexand then pipe it topylanguagetool.$ detex filename.tex | pylanguagetoolUse the pylanguagetool CLI
masterThe
pylanguagetoolCLI allows you to check text for grammar and spelling errors using the LanguageTool API. You can provide input via files, standard input (piping), or the system clipboard.Input Methods:
- Files: Pass one or more filenames as positional arguments.
- Pipe: Pipe text directly into the command (e.g.,
cat file.txt | pylanguagetool). - Clipboard: Use the
-cor--clipboardflag to check text currently in your system clipboard.
Exit Codes:
0: No errors found.1: Errors were found.2: No input provided (usage error).3: Unsupported file type (e.g., LaTeX).
pylanguagetool file1.txt file2.txt echo "Some text" | pylanguagetool pylanguagetool --clipboardUse the pylanguagetool CLI
masterThepylanguagetool.climodule provides command-line interface tools for running language checks directly from your terminal.Use pylanguagetool converters
masterThepylanguagetool.convertersmodule contains utilities for converting text between different formats (such as HTML or plain text) to prepare them for language checking.Use the pylanguagetool HTTP API
masterThepylanguagetool.apimodule provides an HTTP interface for interacting with LanguageTool. Use this module to expose LanguageTool capabilities over a web service.Configure pylanguagetool via environment variables or config file
masterThe CLI uses
configargparse, allowing you to set configurations through multiple layers:- Command Line Flags: Highest priority.
- Environment Variables: Many flags support corresponding environment variables (e.g.,
API_URL,VERBOSE,CLIPBOARD,TEXTLANG,RULES,API_KEY). - Configuration File: The tool looks for a default config file at
~/.config/pyLanguagetool.conf.
It uses a
CustomConfigFileParserto handle these settings.Reference: pyLanguagetool CLI options
masterThe following command-line arguments and environment variables are available for configuring the
pylanguagetoolCLI. Note that many arguments can be set via their corresponding environment variables.$ pylanguagetool --help usage: pylanguagetool [-h] [-V] [-v] [-a API_URL] [--no-color] [-c] [-s] [-t {txt,html,md,markdown,rst,ipynb,json,xliff}] [-u] [-r] [--rule-categories] [-l LANG] [-m MOTHER_TONGUE] [-p PREFERRED_VARIANTS] [-e ENABLED_RULES] [-d DISABLED_RULES] [--enabled-categories ENABLED_CATEGORIES] [--disabled-categories DISABLED_CATEGORIES] [--enabled-only] [--picky] [-U USERNAME] [-P API_KEY] [--pwl PWL] [input files ...] positional arguments: input files input file(s) options: -h, --help show this help message and exit -V, --version print version and exit -v, --verbose verbose output [env var: VERBOSE] -a, --api-url API_URL the URL of the v2 languagetool API, should end with '/v2/' [env var: API_URL] --no-color don't color output [env var: NO_COLOR] -c, --clipboard get text from system clipboard [env var: CLIPBOARD] -s, --single-line check every line on its own [env var: SINGLE_LINE] -t, --input-type {txt,html,md,markdown,rst,ipynb,json,xliff} if not plaintext [env var: INPUT_TYPE] -u, --explain-rule print URLs with more information about rules [env var: EXPLAIN_RULE] -r, --rules show the matching rules [env var: RULES] --rule-categories show the the categories of the matching rules [env var: RULE_CATEGORIES] -l, --lang LANG A language code like en or en-US, or auto to guess the language automatically (see preferredVariants below). For languages with variants (English, German, Portuguese) spell checking will only be activated when you specify the variant, e.g. en-GB instead of just en. [env var: TEXTLANG] -m, --mother-tongue MOTHER_TONGUE A language code of the user's native language, enabling false friends checks for some language pairs. [env var: MOTHER__TONGUE] -p, --preferred-variants PREFERRED_VARIANTS Comma-separated list of preferred language variants. The language detector used with language=auto can detect e.g. English, but it cannot decide whether British English or American English is used. Thus this parameter can be used to specify the preferred variants like en-GB and de-AT. Only available with language=auto. [env var: PREFERRED_VARIANTS] -e, --enabled-rules ENABLED_RULES IDs of rules to be enabled, comma-separated [env var: ENABLED_RULES] -d, --disabled-rules DISABLED_RULES IDs of rules to be disabled, comma-separated [env var: DISABLED_RULES] --enabled-categories ENABLED_CATEGORIES IDs of categories to be enabled, comma-separated [env var: ENABLED_CATEGORIES] --disabled-categories DISABLED_CATEGORIES IDs of categories to be disabled, comma-separated [env var: DISABLED_CATEGORIES] --enabled-only enable only the rules and categories whose IDs are specified with --enabled-rules or --enabled-categories --picky if enabled, additional rules will be activated -U, --username USERNAME For languagetool.org Premium API. Your username/email as used to log in at languagetool.org [env var: USERNAME] -P, --api-key API_KEY For languagetool.org Premium API. [env var: API_KEY] --pwl, --personal-word-list PWL File name of personal dictionary. A private dictionary can be used to add special words that would otherwise be marked as spelling errors. [env var: PERSONAL_WORD_LIST]Configure pylanguagetool CLI options
masterThe CLI supports various flags to customize the checking process, language detection, and output format. Many of these can also be set via environment variables.
Language & Rules
-l,--lang: Language code (e.g.,en,en-US). Useautoto guess the language.-m,--mother-tongue: User's native language to enable 'false friends' checks.-p,--preferred-variants: Comma-separated list of preferred variants (e.g.,en-GB,en-US). Only works when--langis set toauto.-e,--enabled-rules: Comma-separated IDs of rules to enable.-d,--disabled-rules: Comma-separated IDs of rules to disable.--enabled-only: Enable only the rules/categories specified via--enabled-rulesor--enabled-categories.--picky: Activates additional rules.--pwl,--personal-word-list: Path to a file containing a personal dictionary to prevent false spelling errors.
API & Authentication
-a,--api-url: The LanguageTool v2 API URL (defaults tohttps://languagetool.org/api/v2/).-U,--username: Username/email for LanguageTool.org Premium API.-P,--api-key: API key for LanguageTool.org Premium API.
Output Formatting
-r,--rules: Show the matching rule IDs and descriptions.--rule-categories: Show the categories of the matching rules.-u,--explain-rule: Print URLs providing more information about the triggered rules.-s,--single-line: Check every line on its own (useful for line-by-line processing).--no-color: Disable colored terminal output.-v,--verbose: Enable verbose output (prints configuration and Python version).