Package: language-tool-python
language_tool_python: a grammar checker for Python 📝Current LanguageTool version:5.5This is a Python wrapper forLanguageTool. LanguageTool is open-source grammar tool, also known as the spellchecker for OpenOffice. This library allows you to make to detect grammar errors and spelling mistakes through a Python script or through a command-line interface.Local and Remote ServersBy default,language_tool_pythonwill download a LanguageTool server.jarand run that in the background to detect grammar errors locally. However, LanguageTool also offers aPublic HTTP Proofreading APIthat is supported as well. Follow the link for rate limiting details. (Running locally won't have the same restrictions.)Usinglanguage_tool_pythonlocallyLocal server is the default setting. To use this, just initialize a LanguageTool object:importlanguage_tool_pythontool=language_tool_python.LanguageTool('en-US')# use a local server (automatically set up), language EnglishUsinglanguage_tool_pythonwith the public LanguageTool remote serverThere is also a built-in class for querying LanguageTool's public servers. Initialize it like this:importlanguage_tool_pythontool=language_tool_python.LanguageToolPublicAPI('es')# use the public API, language SpanishUsinglanguage_tool_pythonwith the another remote serverFinally, you're able to pass in your own remote server as an argument to theLanguageToolclass:importlanguage_tool_pythontool=language_tool_python.LanguageTool('ca-ES',remote_server='https://language-tool-api.mywebsite.net')# use a remote server API, language CatalanApply a custom list of matches withutils.correctIf you want to decide whichMatchobjects to apply to your text, usetool.check(to generate the list of matches) in conjunction withlanguage_tool_python.utils.correct(to apply the list of matches to text). Here is an example of generating, filtering, and applying a list of matches. In this case, spell-checking suggestions for uppercase words are ignored:>>>s="Department of medicine Colombia University closed on August 1 Milinda Samuelli">>>is_bad_rule=lambdarule:rule.message=='Possible spelling mistake found.'andlen(rule.replacements)andrule.replacements[0][0].isupper()>>>importlanguage_tool_python>>>tool=language_tool_python.LanguageTool('en-US')>>>matches=tool.check(s)>>>matches=[ruleforruleinmatchesifnotis_bad_rule(rule)]>>>language_tool_python.utils.correct(s,matches)'Department of medicine Colombia University closed on August 1 Melinda SamExample usageFrom the interpreter:>>>importlanguage_tool_python>>>tool=language_tool_python.LanguageTool('en-US')>>>text='A sentence with a error in the Hitchhiker’s Guide tot he Galaxy'>>>matches=tool.check(text)>>>len(matches)2...>>>tool.close()# Call `close()` to shut off the server when you're done.Check out someMatchobject attributes:>>>matches[0].ruleId,matches[0].replacements# ('EN_A_VS_AN', ['an'])('EN_A_VS_AN',['an'])>>>matches[1].ruleId,matches[1].replacements('TOT_HE',['to the'])Print aMatchobject:>>>print(matches[1])Line1,column51,RuleID:TOT_HE[1]Message:Didyoumean'to the'?Suggestion:tothe...Automatically apply suggestions to the text:>>>tool.correct(text)'A sentence with an error in the Hitchhiker’s Guide to the Galaxy'From the command line:$echo'This are bad.'>example.txt
$language_tool_pythonexample.txt
example.txt:1:1:THIS_NNS[3]:Didyoumean'these'?Closing LanguageToollanguage_tool_pythonruns a LanguageTool Java server in the background. It will shut the server off when garbage collected, for example when a createdlanguage_tool_python.LanguageToolobject goes out of scope. However, if garbage collection takes awhile, the process might not get deleted right away. If you're seeing lots of processes get spawned and not get deleted, you can explicitly close them:importlanguage_tool_pythontool=language_tool_python.LanguageToolPublicAPI('de-DE')# starts a process# do stuff with `tool`tool.close()# explicitly shut off the LanguageToolYou can also use a context manager (with .. as) to explicitly control when the server is started and stopped:importlanguage_tool_pythonwithlanguage_tool_python.LanguageToolPublicAPI('de-DE')astool:# do stuff with `tool`# no need to call `close() as it will happen at the end of the with statementClient-Server ModelYou can run LanguageTool on one host and connect to it from another.  This is useful in some distributed scenarios. Here's a simple example:server>>>importlanguage_tool_python>>>tool=language_tool_python.LanguageTool('en-US',host='0.0.0.0')>>>tool._url'http://0.0.0.0:8081/v2/'client>>>importlanguage_tool_python>>>lang_tool=language_tool_python.LanguageTool('en-US',remote_server='http://0.0.0.0:8081')>>>>>>>>>lang_tool.check('helo darknes my old frend')[Match({'ruleId':'UPPERCASE_SENTENCE_START','message':'This sentence does not start with an uppercase letter.','replacements':['Helo'],'offsetInContext':0,'context':'helo darknes my old frend','offset':0,'errorLength':4,'category':'CASING','ruleIssueType':'typographical','sentence':'helo darknes my old frend'}),Match({'ruleId':'MORFOLOGIK_RULE_EN_US','message':'Possible spelling mistake found.','replacements':['darkness','darkens','darkies'],'offsetInContext':5,'context':'helo darknes my old frend','offset':5,'errorLength':7,'category':'TYPOS','ruleIssueType':'misspelling','sentence':'helo darknes my old frend'}),Match({'ruleId':'MORFOLOGIK_RULE_EN_US','message':'Possible spelling mistake found.','replacements':['friend','trend','Fred','freed','Freud','Friend','fend','fiend','frond','rend','fr end'],'offsetInContext':20,'context':'helo darknes my old frend','offset':20,'errorLength':5,'category':'TYPOS','ruleIssueType':'misspelling','sentence':'helo darknes my old frend'})]>>>ConfigurationLanguageTool offers lots of built-in configuration options.Example: Enabling cachingHere's an example of using the configuration options to enable caching. Some users have reported that this helps performance a lot.importlanguage_tool_pythontool=language_tool_python.LanguageTool('en-US',config={'cacheSize':1000,'pipelineCaching':True})Example: Setting maximum text lengthHere's an example showing how to configure LanguageTool to set a maximum length on grammar-checked text. Will throw an error (which propagates to Python as alanguage_tool_python.LanguageToolError) if text is too long.importlanguage_tool_pythontool=language_tool_python.LanguageTool('en-US',config={'maxTextLength':100})Full list of configuration optionsHere's a full list of configuration options. See the LanguageToolHTTPServerConfigdocumentation for details.'maxTextLength' - maximum text length, longer texts will cause an error (optional)
'maxTextHardLength' - maximum text length, applies even to users with a special secret 'token' parameter (optional)
'secretTokenKey' - secret JWT token key, if set by user and valid, maxTextLength can be increased by the user (optional)
'maxCheckTimeMillis' - maximum time in milliseconds allowed per check (optional)
'maxErrorsPerWordRate' - checking will stop with error if there are more rules matches per word (optional)
'maxSpellingSuggestions' - only this many spelling errors will have suggestions for performance reasons (optional,
                          affects Hunspell-based languages only)
'maxCheckThreads' - maximum number of threads working in parallel (optional)
'cacheSize' - size of internal cache in number of sentences (optional, default: 0)
'cacheTTLSeconds' - how many seconds sentences are kept in cache (optional, default: 300 if 'cacheSize' is set)
'requestLimit' - maximum number of requests per requestLimitPeriodInSeconds (optional)
'requestLimitInBytes' - maximum aggregated size of requests per requestLimitPeriodInSeconds (optional)
'timeoutRequestLimit' - maximum number of timeout request (optional)
'requestLimitPeriodInSeconds' - time period to which requestLimit and timeoutRequestLimit applies (optional)
'languageModel' - a directory with '1grams', '2grams', '3grams' sub directories which contain a Lucene index
                  each with ngram occurrence counts; activates the confusion rule if supported (optional)
'word2vecModel' - a directory with word2vec data (optional), see
https://github.com/languagetool-org/languagetool/blob/master/languagetool-standalone/CHANGES.md#word2vec
'fasttextModel' - a model file for better language detection (optional), see
                  https://fasttext.cc/docs/en/language-identification.html
'fasttextBinary' - compiled fasttext executable for language detection (optional), see
                  https://fasttext.cc/docs/en/support.html
'maxWorkQueueSize' - reject request if request queue gets larger than this (optional)
'rulesFile' - a file containing rules configuration, such as .langugagetool.cfg (optional)
'warmUp' - set to 'true' to warm up server at start, i.e. run a short check with all languages (optional)
'blockedReferrers' - a comma-separated list of HTTP referrers (and 'Origin' headers) that are blocked and will not be served (optional)
'premiumOnly' - activate only the premium rules (optional)
'disabledRuleIds' - a comma-separated list of rule ids that are turned off for this server (optional)
'pipelineCaching' - set to 'true' to enable caching of internal pipelines to improve performance
'maxPipelinePoolSize' - cache size if 'pipelineCaching' is set
'pipelineExpireTimeInSeconds' - time after which pipeline cache items expire
'pipelinePrewarming' - set to 'true' to fill pipeline cache on start (can slow down start a lot)InstallationTo install via pip:$pipinstall--upgradelanguage_tool_pythonCustomizing Download URL or PathTo overwrite the host part of URL that is used to download LanguageTool-{version}.zip:$exportLTP_DOWNLOAD_HOST=[alternateURL]This can be used to downgrade to an older version, for example, or to download from a mirror.And to choose the specific folder to download the server to:$exportLTP_PATH=/path/to/save/language/toolThe default download path is~/.cache/language_tool_python/. The LanguageTool server is about 200 MB, so take that into account when choosing your download folder. (Or, if you you can't spare the disk space, use a remote URL!)PrerequisitesPython 3.6+LanguageTool(Java 8.0 or higher)The installation process should take care of downloading LanguageTool (it may
take a few minutes). Otherwise, you can manually downloadLanguageTool-stable.zipand unzip it
into where thelanguage_tool_pythonpackage resides.LanguageTool VersionAs of April 2020,language_tool_pythonwas forked fromlanguage-checkand no longer supports LanguageTool versions lower than 4.0.AcknowledgementsThis is a fork ofhttps://github.com/myint/language-check/that produces more easily parsable
results from the command-line.