Skip to content

Commit f091b98

Browse files
committed
update documentation and add link to public api
1 parent 562f1e9 commit f091b98

4 files changed

Lines changed: 43 additions & 11 deletions

File tree

‎README.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,33 @@
22

33
This is a Python wrapper for [LanguageTool](languagetool.org). 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.
44

5+
## Local and Remote Servers
6+
7+
By default, `language_tool_python` will download a LanguageTool server `.jar` and run that in the background to detect grammar errors locally. However, LanguageTool also offers a [Public HTTP Proofreading API](http://wiki.languagetool.org/public-http-api) that is supported as well. Follow the link for rate limiting details. (Running locally won't have the same restrictions.)
8+
9+
### Using `language_tool_python` locally
10+
11+
Local server is the default setting. To use this, just initialize a LanguageTool object:
12+
13+
> import language_tool_python
14+
> tool = language_tool_python.LanguageTool('en-US') # use a local server (automatically set up), language English
15+
16+
### Using `language_tool_python` with the public LanguageTool remote server
17+
18+
There is also a built-in class for querying LanguageTool's public servers. Initialize it like this:
19+
20+
> import language_tool_python
21+
> tool = language_tool_python.LanguageToolPublicAPI('es') # use the public API, language Spanish
22+
23+
### Using `language_tool_python` with the another remote server
24+
25+
Finally, you're able to pass in your own remote server as an argument to the `LanguageTool` class:
26+
27+
> import language_tool_python
28+
> tool = language_tool_python.LanguageTool('ca-ES', remote_server='https://language-tool-api.mywebsite.net') # use a remote server API, language Catalan
29+
30+
31+
532
## Example usage
633

734
From the interpreter:

‎language_tool_python/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77

88
from .language_tag import LanguageTag
99
from .match import Match
10-
from .server import LanguageTool
10+
from .server import LanguageTool, LanguageToolPublicAPI
1111
from . import utils

‎language_tool_python/__main__.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ def parse_args():
6060

6161

6262
class RulesAction(argparse.Action):
63-
6463
def __call__(self, parser, namespace, values, option_string=None):
6564
getattr(namespace, self.dest).update(values)
6665

@@ -103,9 +102,10 @@ def main():
103102
encoding = args.encoding or 'utf-8'
104103

105104
remote_server = None
106-
if args.remote_host is not None and args.remote_port is not None:
107-
remote_server = {'host': args.remote_host,
108-
'port': args.remote_port}
105+
if args.remote_host is not None:
106+
remote_server = args.remote_host
107+
if args.remote_port is not None:
108+
remote_server += ':{}'.format(args.remote_port)
109109
lang_tool = LanguageTool(
110110
motherTongue=args.mother_tongue,
111111
remote_server=remote_server,

‎language_tool_python/server.py‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
from .match import Match
1414
from .utils import *
1515

16-
download_lt()
17-
1816
class LanguageTool:
1917

2018
"""Main class used for checking text against different rules."""
@@ -33,9 +31,8 @@ class LanguageTool:
3331
def __init__(self, language=None, motherTongue=None, remote_server=None):
3432
if remote_server is not None:
3533
self._remote = True
36-
self._HOST = remote_server["host"]
37-
self._port = remote_server["port"]
38-
self._url = 'http://{}:{}/v2/'.format(self._HOST, self._port)
34+
if remote_server[-1] == '/': remote_server = remote_server[:-1]
35+
self._url = '{}/v2/'.format(remote_server)
3936
self._update_remote_server_config(self._url)
4037
elif not self._server_is_alive():
4138
self._start_server_on_free_port()
@@ -151,6 +148,9 @@ def _get_attrib(cls):
151148

152149
@classmethod
153150
def _start_server_if_needed(cls):
151+
# Download language tool if needed.
152+
download_lt()
153+
# Start server.
154154
if not cls._server_is_alive() and cls._remote is False:
155155
cls._start_server_on_free_port()
156156

@@ -294,6 +294,11 @@ def _terminate_server(cls):
294294
return LanguageToolError_message
295295

296296

297+
class LanguageToolPublicAPI(LanguageTool):
298+
""" Language tool client of the official API. """
299+
def __init__(self, *args, **kwargs):
300+
super().__init__(*args, remote_server='https://api.languagetool.org', **kwargs)
301+
297302
@atexit.register
298303
def terminate_server():
299304
"""Terminate the server."""
@@ -310,4 +315,4 @@ def _consume(stdout):
310315
311316
"""
312317
while stdout.readline():
313-
pass
318+
pass

0 commit comments

Comments
 (0)