pyLanguagetool 0.11.0

repository·master·Indexed 41 days ago

https://github.com/findus23/pylanguagetool

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

Tokens
3.5K
Snippets
4
Records
12
Agent score
48%

What's inside pyLanguagetool

  1. Configure pyLanguagetool via CLI, Env Vars, or Config File

    master

    Configuration follows a specific hierarchy: Command-line arguments > Environment variables > Configuration file (~/.config/pyLanguagetool.conf) > Defaults.

    Configuration File

    The config file uses ConfigArgParse syntax. Supported formats include:

    • key=value
    • flag=true
    • stuff=[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-url attribute (ensure it ends with /v2/).

  2. Install pyLanguagetool

    master

    pyLanguagetool 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 pylanguagetool
  3. Use the pyLanguagetool CLI for basic text checking

    master

    The 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
  4. Use the pylanguagetool CLI

    master

    The pylanguagetool CLI 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 -c or --clipboard flag 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 --clipboard
  5. Configure pylanguagetool via environment variables or config file

    master

    The CLI uses configargparse, allowing you to set configurations through multiple layers:

    1. Command Line Flags: Highest priority.
    2. Environment Variables: Many flags support corresponding environment variables (e.g., API_URL, VERBOSE, CLIPBOARD, TEXTLANG, RULES, API_KEY).
    3. Configuration File: The tool looks for a default config file at ~/.config/pyLanguagetool.conf.

    It uses a CustomConfigFileParser to handle these settings.

  6. Reference: pyLanguagetool CLI options

    master

    The following command-line arguments and environment variables are available for configuring the pylanguagetool CLI. 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]
  7. Configure pylanguagetool CLI options

    master

    The 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). Use auto to 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 --lang is set to auto.
    • -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-rules or --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 to https://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).