-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapi.py
More file actions
225 lines (188 loc) · 8.03 KB
/
api.py
File metadata and controls
225 lines (188 loc) · 8.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
r"""
Python API wrapper for the languagetool REST API.
Simple usage::
>>> from pylanguagetool import api
>>> api.check(
... 'This is a example',
... api_url='https://languagetool.org/api/v2/',
... lang='en-US',
... )
"""
from typing import Optional
import requests
def get_languages(api_url: str) -> list[dict[str, str]]:
"""
Return supported languages as a list of dictionaries.
Args:
api_url (str): API base url.
Returns:
list[dict]:
Supported languages as a list of dictionaries.
Each dictionary contains three keys, ``name``, ``code`` and
``longCode``::
{
"name":"English (GB)",
"code":"en",
"longCode":"en-GB"
}
"""
r = requests.get(api_url + "languages")
return r.json()
def _is_in_pwl(match, pwl):
start = match['context']['offset']
end = start + match['context']['length']
word = match['context']['text'][start:end]
return word in pwl
def check(
input_text: str, api_url: str,
lang: str, pwl: list[str],
mother_tongue: Optional[str] = None,
preferred_variants: Optional[str] = None,
enabled_rules: Optional[str] = None, disabled_rules: Optional[str] = None,
enabled_categories: Optional[str] = None, disabled_categories: Optional[str] = None,
enabled_only: bool = False, picky: bool = False, verbose: bool = False,
username: Optional[str] = None, api_key: Optional[str] = None):
"""
Check given text and return API response as a dictionary.
Args:
input_text (str):
Plain text that will be checked for spelling mistakes.
api_url (str):
API base url, e.g. ``https://languagetool.org/api/v2/``
lang: Language of the given text as `RFC 3066`__ language code.
For example ``en-GB`` or ``de-AT``. ``auto`` is a valid value too
and will cause the language to be detected automatically.
__ https://www.ietf.org/rfc/rfc3066.txt
mother_tongue: Native language of the author as `RFC 3066`__ language
code.
__ https://www.ietf.org/rfc/rfc3066.txt
preferred_variants (str):
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. Therefore, this parameter can be used to specify the
preferred variants like ``en-GB`` and ``de-AT``. Only available
with ``language=auto``.
enabled_rules (str):
Comma-separated list of IDs of rules to be enabled
disabled_rules (str):
Comma-separated list of IDs of rules to be disabled.
enabled_categories (str):
Comma-separated list of IDs of categories to be enabled.
disabled_categories (str):
Comma-separated list of IDs of categories to be disabled.
enabled_only (bool):
If ``True``, only the rules and categories whose IDs are specified
with ``enabledRules`` or ``enabledCategories`` are enabled.
Defaults to ``False``.
picky (bool):
If enabled, addition rules are activated.
verbose (bool):
If ``True``, a more verbose output will be printed. Defaults to
``False``.
pwl (list[str]):
Personal world list. A custom dictionary of words that should be
excluded from spell checking errors.
username (str):
For Premium API
api_key (str):
For Premium API
Returns:
dict:
A dictionary representation of the JSON API response.
The most notable key is ``matches``, which contains a list of all
spelling mistakes that have been found.
E.g.::
{
"language": {
"name": "English (GB)",
"code": "en-GB",
"detectedLanguage": {
"name": "English (GB)",
"code": "en-GB",
"confidence": 0.561,
"source": "fasttext",
},
},
"sentenceRanges": [[0, 17]],
"extendedSentenceRanges": [
{"from": 0, "to": 17, "detectedLanguages": [{"language": "en", "rate": 1.0}]}
],
"matches": [
{
"context": {"text": "This is a example", "offset": 8, "length": 1},
"contextForSureMatch": 1,
"ignoreForIncompleteSentence": False,
"length": 1,
"message": "Use “an” instead of ‘a’ if the following word starts with a vowel sound, e.g.\xa0‘an article’, ‘an hour’.",
"offset": 8,
"replacements": [{"value": "an"}],
"rule": {
"category": {"id": "MISC", "name": "Miscellaneous"},
"description": "Use of 'a' vs. 'an'",
"id": "EN_A_VS_AN",
"issueType": "misspelling",
"urls": [
{
"value": "https://languagetool.org/insights/post/indefinite-articles/"
}
],
},
"sentence": "This is a example",
"shortMessage": "Wrong article",
"type": {"typeName": "Other"},
}
],
"software": {
"apiVersion": 1,
"buildDate": "2024-09-27 11:27:57 +0200",
"version": "6.5",
"name": "LanguageTool",
"premium": False,
"premiumHint": "You might be missing errors only the Premium version can find. Contact us at support<at>languagetoolplus.com.",
"status": "",
},
"warnings": {"incompleteResults": False},
}
"""
post_parameters = {
"text": input_text,
"language": lang,
}
if mother_tongue:
post_parameters["motherTongue"] = mother_tongue
if preferred_variants:
post_parameters["preferredVariants"] = preferred_variants
if enabled_rules:
post_parameters["enabledRules"] = enabled_rules
if disabled_rules:
post_parameters["disabledRules"] = disabled_rules
if enabled_categories:
post_parameters["enabledCategories"] = enabled_categories
if disabled_categories:
post_parameters["disabledCategories"] = disabled_categories
if enabled_only:
post_parameters["enabledOnly"] = 'true'
if picky:
post_parameters["level"] = 'picky'
if username or api_key:
if not username or not api_key:
raise ValueError("When specifying username or apikey, you must specify both")
if api_url != "https://languagetool.org/api/v2/":
raise ValueError("Premium API only works when using languagetool.org")
post_parameters["username"] = username
post_parameters["apiKey"] = api_key
r = requests.post(api_url + "check", data=post_parameters)
if r.status_code != 200:
raise ValueError(r.text)
if verbose:
print(post_parameters)
print(r.json())
data = r.json()
if pwl:
matches = data.pop('matches', [])
data['matches'] = [
match for match in matches
if not _is_in_pwl(match, pwl)
]
return data