### Quick Start: Local Server Source: https://github.com/jxmorris12/language_tool_python/blob/master/README.md Demonstrates how to initialize and use a local LanguageTool server for checking text. Ensure Java is installed and compatible with the LanguageTool version. ```python import language_tool_python with language_tool_python.LanguageTool("en-US") as tool: text = "A sentence with a error in the Hitchhiker's Guide tot he Galaxy" matches = tool.check(text) print(matches) print(tool.correct(text)) ``` -------------------------------- ### Local Server with Configuration Example Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/api-reference/LanguageTool.md Example of initializing LanguageTool with a custom configuration dictionary for the local server. ```python config = { "cacheSize": 1000, "pipelineCaching": True, "maxTextLength": 50000, } with LanguageTool("en-US", config=config) as tool: matches = tool.check("text") ``` -------------------------------- ### Local Server with Default Settings Example Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/api-reference/LanguageTool.md Example of initializing and using LanguageTool with a local server and default settings for English (US). ```python from language_tool_python import LanguageTool with LanguageTool("en-US") as tool: text = "This are a mistake." matches = tool.check(text) for match in matches: print(f"Error: {match.message}") print(f"Suggestions: {match.replacements}") ``` -------------------------------- ### Basic Server Configuration Example Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/api-reference/LanguageToolConfig.md A simple example demonstrating how to configure the LanguageTool server with basic settings like cache size and max text length, and then use it. ```python from language_tool_python import LanguageTool config = { "cacheSize": 2000, "maxTextLength": 100000, } with LanguageTool("en-US", config=config) as tool: matches = tool.check("large text") ``` -------------------------------- ### Install Development Dependencies Source: https://github.com/jxmorris12/language_tool_python/blob/master/CONTRIBUTING.md Installs all necessary packages for development, including optional dependencies. ```shell make install ``` -------------------------------- ### Custom Rules and Picky Mode Example Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/api-reference/LanguageTool.md Example demonstrating how to customize rules (disable/enable) and enable picky mode for stricter checking. ```python with LanguageTool("en-US") as tool: tool.disabled_rules.update({"MORFOLOGIK_RULE_EN_US"}) tool.enabled_rules.update({"EN_A_VS_AN"}) tool.picky = True matches = tool.check("text") ``` -------------------------------- ### Quick Start: Remote LanguageTool Server Source: https://github.com/jxmorris12/language_tool_python/blob/master/README.md Connects to a self-hosted LanguageTool server for text checking. Replace the example URL with your server's address. ```python import language_tool_python with language_tool_python.LanguageTool( "en-US", remote_server="https://your-lt-server.example.com", ) as tool: print(tool.check("This are bad.")) ``` -------------------------------- ### Installing Development Dependencies Source: https://github.com/jxmorris12/language_tool_python/blob/master/README.md Install the necessary dependencies for developing the language_tool_python library. ```bash # Install dev dependencies make install ``` -------------------------------- ### Remote Server Usage Example Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/api-reference/LanguageTool.md Example of initializing and using LanguageTool with a remote server endpoint. ```python with LanguageTool( "en-US", remote_server="https://languagetool.org/api/" ) as tool: print(tool.correct("This are bad.")) ``` -------------------------------- ### LanguageTool Configuration Example Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/INDEX.md Provides an example of how to configure LanguageTool with custom settings like cacheSize and maxTextLength using a dictionary passed to the config parameter during instantiation. ```python config = {"cacheSize": 1000, "maxTextLength": 50000} LanguageTool("en-US", config=config) ``` -------------------------------- ### Quick Start: Public LanguageTool API Source: https://github.com/jxmorris12/language_tool_python/blob/master/README.md Shows how to use the public LanguageTool API for text checking. This method does not require a local Java installation. ```python import language_tool_python with language_tool_python.LanguageToolPublicAPI("es") as tool: matches = tool.check("Se a hecho un esfuerzo.") print(matches) ``` -------------------------------- ### ServerError Example: Startup Failure Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/errors.md Handle ServerError specifically when the LanguageTool server fails to start. This can occur if Java is not installed or the LanguageTool installation is corrupted. ```python from language_tool_python import LanguageTool, ServerError try: with LanguageTool("en-US") as tool: tool.check("text") except ServerError as e: print(f"Failed to start server: {e}") # Possibly Java not installed, or corrupted LanguageTool installation ``` -------------------------------- ### Usage Example - Basic Server Configuration Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/api-reference/LanguageToolConfig.md Demonstrates how to configure and use LanguageTool with a basic configuration dictionary. ```APIDOC ## Usage Examples - Basic Server Configuration ### Example ```python from language_tool_python import LanguageTool config = { "cacheSize": 2000, "maxTextLength": 100000, } with LanguageTool("en-US", config=config) as tool: matches = tool.check("large text") ``` ``` -------------------------------- ### Install language_tool_python Source: https://github.com/jxmorris12/language_tool_python/blob/master/README.md Install or upgrade the language_tool_python package using pip. ```bash pip install --upgrade language_tool_python ``` -------------------------------- ### JavaError Example: Java Not Installed Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/errors.md Handle JavaError when the local LanguageTool server cannot be started due to Java not being found or not meeting version requirements. Provides a hint for installation. ```python from language_tool_python import LanguageTool, JavaError try: # Attempting to start local server without Java with LanguageTool("en-US") as tool: pass except JavaError as e: if "can't find Java" in str(e): print("Please install Java to use local LanguageTool server") print("Hint: sudo apt-get install openjdk-17-jre-headless") ``` -------------------------------- ### Get Languages Endpoint Response Example Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/endpoints.md Provides an example of the JSON array returned by the /v2/languages endpoint, listing supported languages with their codes. ```json [ { "name": "English", "code": "en", "longCode": "en-US" }, { "name": "English (British)", "code": "en", "longCode": "en-GB" }, { "name": "German", "code": "de", "longCode": "de-DE" } ] ``` -------------------------------- ### Use External LanguageTool Server Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/cli-reference.md Example demonstrating how to connect to an external LanguageTool server using a specific host and port. ```bash # Use external server language_tool_python -l en-US --remote-host api.example.com --remote-port 8080 file.txt ``` -------------------------------- ### Performance Tuning Configuration Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/api-reference/LanguageToolConfig.md Example of setting performance-related configuration options for the LanguageTool server. ```python config = { "cacheSize": 1000, "cacheTTLSeconds": 3600, "maxCheckThreads": 4, "maxCheckTimeMillis": 30000, "pipelineCaching": True, "maxPipelinePoolSize": 8, "pipelineExpireTimeInSeconds": 300, "pipelinePrewarming": True, } ``` -------------------------------- ### Headers and Security Configuration Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/api-reference/LanguageToolConfig.md Example of configuring security-related options like trusting X-Forwarded-For headers and enabling suggestions. ```python config = { "trustXForwardForHeader": True, "suggestionsEnabled": True, } ``` -------------------------------- ### Custom Server Configuration Example Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/endpoints.md Demonstrates how to configure custom server behavior, such as request limits and maximum text length, by passing a configuration dictionary to the LanguageTool constructor. ```python config = { "requestLimit": 100, # Limit requests "requestLimitPeriodInSeconds": 60, # Per minute "maxTextLength": 50000, # Max text size } with LanguageTool("en-US", config=config) as tool: # Server enforces these limits pass ``` -------------------------------- ### Health Check Endpoint Example Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/endpoints.md Shows how to check the health and responsiveness of the LanguageTool server using a GET request to the /v2/healthcheck endpoint. This is available in LanguageTool 4.2+. ```bash curl "http://localhost:8081/v2/healthcheck" ``` -------------------------------- ### Complete Workflow Example Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/api-reference/utils.md Demonstrates a complete workflow using LanguageTool, including checking text for errors, classifying the text quality using `classify_matches` and `TextStatus`, and applying auto-correction if necessary. ```python from language_tool_python import LanguageTool from language_tool_python.utils import classify_matches, TextStatus, correct with LanguageTool("en-US") as tool: text = "There is a bok on the table." matches = tool.check(text) # Classify text quality status = classify_matches(matches) print(f"Text status: {status}") # TextStatus.FAULTY # Show all matches for match in matches: print(f"{match.rule_id}: {match.message}") print(f" Suggestions: {match.replacements}") # Apply auto-correction if status != TextStatus.CORRECT: corrected = correct(text, matches) print(f"Corrected: {corrected}") ``` -------------------------------- ### Usage Example: Using Line and Column Information Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/api-reference/Match.md This example shows how to iterate through matches found in a multi-line text and use the `get_line_and_column` method to report the precise location of each error. ```APIDOC ### Using Line and Column Information ```python text = "This are bad.\nThat is also bad." matches = tool.check(text) for match in matches: line, col = match.get_line_and_column(text) print(f"{line}:{col}: {match.rule_id}: {match.message}") ``` ``` -------------------------------- ### Request Limiting Configuration Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/api-reference/LanguageToolConfig.md Example of configuring request limits and timeouts for the LanguageTool server. ```python config = { "requestLimit": 1000, "requestLimitInBytes": 10000000, "timeoutRequestLimit": 300, "requestLimitPeriodInSeconds": 3600, } ``` -------------------------------- ### Usage Example: Accessing Match Information Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/api-reference/Match.md This example demonstrates how to use the `LanguageTool` to check text and then access the attributes of the returned `Match` objects to display error details. ```APIDOC ## Usage Examples ### Accessing Match Information ```python from language_tool_python import LanguageTool with LanguageTool("en-US") as tool: text = "This are bad." matches = tool.check(text) if matches: match = matches[0] print(f"Error: {match.message}") print(f"Rule ID: {match.rule_id}") print(f"Position: {match.offset}-{match.offset + match.error_length}") print(f"Suggestions: {match.replacements}") ``` ``` -------------------------------- ### Path Configuration Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/api-reference/LanguageToolConfig.md Example of specifying paths for language models, dictionaries, and rule files in the LanguageTool server configuration. ```python config = { "languageModel": "/path/to/model.bin", "fasttextModel": "/path/to/fasttext.bin", "fasttextBinary": "/usr/bin/fasttext", "rulesFile": "/path/to/rules.xml", "lang-en-dictPath": "/path/to/dictionary.txt", } ``` -------------------------------- ### ServerError Example: Server Timeout Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/errors.md Catch ServerError when the server process starts but does not become ready within the expected timeout period. ```python try: # Server process starts but doesn't become ready tool = LanguageTool("en-US") except ServerError as e: if "did not become ready" in str(e): print("Server initialization timeout") ``` -------------------------------- ### Ignore Lines Starting with # Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/cli-reference.md Example of ignoring lines that begin with a '#' character, useful for markdown files. ```bash # Ignore lines starting with # language_tool_python --ignore-lines "^#" markdown.md ``` -------------------------------- ### Error Handling Example Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/api-reference/LanguageToolPublicAPI.md Provides an example of how to handle potential `RateLimitError` exceptions when using LanguageToolPublicAPI. The `try-except` block catches the error and prints a user-friendly message. ```APIDOC ### Error Handling ```python from language_tool_python import LanguageToolPublicAPI, RateLimitError try: with LanguageToolPublicAPI("en-US") as tool: matches = tool.check("Text") except RateLimitError: print("Rate limit exceeded. Please try again later.") ``` ``` -------------------------------- ### Limits Configuration Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/api-reference/LanguageToolConfig.md Example of setting limits for text length and error rates in the LanguageTool server configuration. ```python config = { "maxTextLength": 100000, "maxTextHardLength": 150000, "maxErrorsPerWordRate": 0.5, "maxSpellingSuggestions": 10, } ``` -------------------------------- ### Use Existing LanguageTool Installation Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/configuration.md Prevent LanguageTool from downloading by specifying the directory of an existing installation using LTP_JAR_DIR_PATH. ```bash export LTP_JAR_DIR_PATH=/path/to/LanguageTool-6.8 python script.py ``` -------------------------------- ### Custom Spellings Example Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/api-reference/LanguageTool.md Example of initializing LanguageTool with custom spellings, with an option to not persist them. ```python with LanguageTool( "en-US", new_spellings=["mycompany", "technobabble"], new_spellings_persist=False ) as tool: matches = tool.check("mycompany is great") ``` -------------------------------- ### Apply Corrections and Save to File Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/cli-reference.md Example of automatically applying corrections and redirecting the output to a new file. ```bash # Correct file and save language_tool_python -l en-US -a input.txt > output.txt ``` -------------------------------- ### Proxy Usage Example Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/api-reference/LanguageToolPublicAPI.md Illustrates how to configure LanguageToolPublicAPI to use a proxy server. The `proxies` dictionary specifies the HTTP and HTTPS proxy addresses for routing requests. ```APIDOC ### With Proxy ```python with LanguageToolPublicAPI( "en-US", proxies={ "http": "http://proxy.example.com:8080", "https": "http://proxy.example.com:8080" } ) as tool: matches = tool.check("Text to check") ``` ``` -------------------------------- ### Rules and Filtering Configuration Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/api-reference/LanguageToolConfig.md Example of configuring disabled rules, blocked referrers, and premium-only mode for the LanguageTool server. ```python config = { "disabledRuleIds": ["RULE1", "RULE2"], "blockedReferrers": ["example.com"], "premiumOnly": True, } ``` -------------------------------- ### View Corrected Output Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/cli-reference.md Example of applying corrections and viewing the corrected text directly in the console. ```bash # View corrected output language_tool_python -l en-US -a input.txt ``` -------------------------------- ### Basic Public API Usage Example Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/api-reference/LanguageToolPublicAPI.md Demonstrates the basic usage of LanguageToolPublicAPI to check text for grammatical errors. It initializes the tool for English and prints any detected matches. ```APIDOC ## Usage Examples ### Basic Public API Usage ```python from language_tool_python import LanguageToolPublicAPI with LanguageToolPublicAPI("en-US") as tool: matches = tool.check("This are not correct.") for match in matches: print(match) ``` ``` -------------------------------- ### Language-Specific Options Configuration Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/api-reference/LanguageToolConfig.md Example of setting language codes and dictionary paths for specific languages in the LanguageTool server configuration. ```python config = { "lang-en": "en", "lang-en-dictPath": "/path/to/en.dict", "lang-de": "de", "lang-de-dictPath": "/path/to/de.dict", } ``` -------------------------------- ### cURL Request for Getting Languages Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/endpoints.md Demonstrates how to fetch the list of supported languages from the LanguageTool server using a GET request to the /v2/languages endpoint. ```bash curl "http://localhost:8081/v2/languages" ``` -------------------------------- ### Get Server Configuration Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/api-reference/LanguageTool.md Retrieve the server configuration object. This is a read-only property and may be None if not set. ```python @property def config(self) -> Optional[LanguageToolConfig] ``` -------------------------------- ### Configure Privacy-Focused Local-Only Setup Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/configuration.md Set the LTP_JAR_DIR_PATH environment variable to a pre-installed LanguageTool directory to ensure no network downloads occur. This is suitable for privacy-focused or offline environments. ```python import os os.environ["LTP_JAR_DIR_PATH"] = "/opt/languagetool" # Pre-installed # Never downloads from network with LanguageTool("en-US") as tool: matches = tool.check("sensitive data") ``` -------------------------------- ### Full Request Flow for Checking Text in Python Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/endpoints.md Provides a comprehensive Python example demonstrating the typical request flow for checking text with the LanguageTool client. It covers initialization, language retrieval, text checking, and response parsing. ```python from language_tool_python import LanguageTool with LanguageTool("en-US") as tool: # Step 1-2: Initialization (automatic) # - Starts local server # - Calls GET /v2/languages # Step 3-4: Check text matches = tool.check("This are bad.") # Internally: POST /v2/check with params # Step 5: Automatic parsing for match in matches: print(match.message) # Parsed from response ``` -------------------------------- ### Spanish Checking Example Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/api-reference/LanguageToolPublicAPI.md Shows how to use LanguageToolPublicAPI for checking Spanish text. It initializes the tool with the 'es' language code and demonstrates the `correct` method to auto-correct a sentence. ```APIDOC ### Spanish Checking ```python with LanguageToolPublicAPI("es") as tool: corrected = tool.correct("Se a hecho un esfuerzo.") print(corrected) # "Se ha hecho un esfuerzo." ``` ``` -------------------------------- ### Specify Encoding for Legacy File Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/cli-reference.md Example of specifying 'latin-1' encoding when processing a legacy file. ```bash language_tool_python -c latin-1 legacy_file.txt ``` -------------------------------- ### Configure Custom Dictionary for Domain Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/configuration.md Initialize LanguageTool with a configuration dictionary that specifies a custom dictionary path and disables specific rules. This is useful for domain-specific terminology or style guides. ```python config = { "lang-en-dictPath": "/opt/dicts/medical.txt", "disabledRuleIds": "STYLE_BASED_RULE", } with LanguageTool("en-US", config=config) as tool: matches = tool.check("Medical terminology text") ``` -------------------------------- ### Ignore HTML Comments Source: https://github.com/jxmorris12/language_tool_python/blob/master/_autodocs/cli-reference.md Example of ignoring lines that start with '