Skip to content

Commit 0786acd

Browse files
committed
fix: from camel case to snake case for some params and attribs (breaking)
1 parent 19f0d2d commit 0786acd

File tree

7 files changed

+68
-70
lines changed

7 files changed

+68
-70
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ prune docs/
1919
prune tests/
2020

2121
exclude .gitignore
22+
exclude .readthedocs.yaml
2223
exclude build_and_publish.sh
2324
exclude CONTRIBUTING.md
2425
exclude make.bat

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ If you want to apply a particular suggestion from a `Match`, use `Match.select_r
7171
>>> tool = language_tool_python.LanguageTool('en-US')
7272
>>> matches = tool.check(s)
7373
>>> matches
74-
[Match({'ruleId': 'MORFOLOGIK_RULE_EN_US', 'message': 'Possible spelling mistake found.', 'replacements': ['BOK', 'OK', 'book', 'box'], 'offsetInContext': 11, 'context': 'There is a bok on the table.', 'offset': 11, 'errorLength': 3, 'category': 'TYPOS', 'ruleIssueType': 'misspelling', 'sentence': 'There is a bok on the table.'})]
74+
[Match({'rule_id': 'MORFOLOGIK_RULE_EN_US', 'message': 'Possible spelling mistake found.', 'replacements': ['BOK', 'OK', 'book', 'box'], 'offset_in_context': 11, 'context': 'There is a bok on the table.', 'offset': 11, 'error_length': 3, 'category': 'TYPOS', 'rule_issue_type': 'misspelling', 'sentence': 'There is a bok on the table.'})]
7575
>>> matches[0].select_replacement(2)
7676
>>> patched_text = language_tool_python.utils.correct(s, matches)
7777
>>> patched_text
@@ -115,9 +115,9 @@ From the interpreter:
115115
Check out some ``Match`` object attributes:
116116

117117
```python
118-
>>> matches[0].ruleId, matches[0].replacements # ('EN_A_VS_AN', ['an'])
118+
>>> matches[0].rule_id, matches[0].replacements # ('EN_A_VS_AN', ['an'])
119119
('EN_A_VS_AN', ['an'])
120-
>>> matches[1].ruleId, matches[1].replacements
120+
>>> matches[1].rule_id, matches[1].replacements
121121
('TOT_HE', ['to the'])
122122
```
123123

@@ -188,7 +188,7 @@ You can run LanguageTool on one host and connect to it from another. This is us
188188
>>>
189189
>>>
190190
>>> lang_tool.check('helo darknes my old frend')
191-
[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'})]
191+
[Match({'rule_id': 'UPPERCASE_SENTENCE_START', 'message': 'This sentence does not start with an uppercase letter.', 'replacements': ['Helo'], 'offset_in_Context': 0, 'context': 'helo darknes my old frend', 'offset': 0, 'error_length': 4, 'category': 'CASING', 'rule_issue_type': 'typographical', 'sentence': 'helo darknes my old frend'}), Match({'rule_id': 'MORFOLOGIK_RULE_EN_US', 'message': 'Possible spelling mistake found.', 'replacements': ['darkness', 'darkens', 'darkies'], 'offset_in_context': 5, 'context': 'helo darknes my old frend', 'offset': 5, 'error_length': 7, 'category': 'TYPOS', 'rule_issue_type': 'misspelling', 'sentence': 'helo darknes my old frend'}), Match({'rule_id': 'MORFOLOGIK_RULE_EN_US', 'message': 'Possible spelling mistake found.', 'replacements': ['friend', 'trend', 'Fred', 'freed', 'Freud', 'Friend', 'fend', 'fiend', 'frond', 'rend', 'fr end'], 'offset_in_context': 20, 'context': 'helo darknes my old frend', 'offset': 20, 'error_length': 5, 'category': 'TYPOS', 'rule_issue_type': 'misspelling', 'sentence': 'helo darknes my old frend'})]
192192
>>>
193193
```
194194

language_tool_python/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def main() -> int:
254254
remote_server += f":{args.remote_port}"
255255
with LanguageTool(
256256
language=args.language,
257-
motherTongue=args.mother_tongue,
257+
mother_tongue=args.mother_tongue,
258258
remote_server=remote_server,
259259
) as lang_tool:
260260
try:
@@ -278,7 +278,7 @@ def main() -> int:
278278
print(lang_tool.correct(text))
279279
else:
280280
for match in lang_tool.check(text):
281-
rule_id = match.ruleId
281+
rule_id = match.rule_id
282282

283283
replacement_text = ", ".join(
284284
f"'{word}'" for word in match.replacements

language_tool_python/match.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,28 @@ def get_match_ordered_dict() -> OrderedDictType[str, type]:
2929
3030
The keys and their corresponding types are:
3131
32-
- 'ruleId': str
32+
- 'rule_id': str
3333
- 'message': str
3434
- 'replacements': list
35-
- 'offsetInContext': int
35+
- 'offset_in_context': int
3636
- 'context': str
3737
- 'offset': int
38-
- 'errorLength': int
38+
- 'error_length': int
3939
- 'category': str
40-
- 'ruleIssueType': str
40+
- 'rule_issue_type': str
4141
- 'sentence': str
4242
"""
4343
return OrderedDict(
4444
[
45-
("ruleId", str),
45+
("rule_id", str),
4646
("message", str),
4747
("replacements", list),
48-
("offsetInContext", int),
48+
("offset_in_context", int),
4949
("context", str),
5050
("offset", int),
51-
("errorLength", int),
51+
("error_length", int),
5252
("category", str),
53-
("ruleIssueType", str),
53+
("rule_issue_type", str),
5454
("sentence", str),
5555
],
5656
)
@@ -147,7 +147,7 @@ class Match:
147147
"""The positions of 4-byte encoded characters in the text, registered by the previous match object
148148
(kept for optimization purposes if the text is the same)."""
149149

150-
ruleId: str
150+
rule_id: str
151151
"""The ID of the rule that was violated."""
152152

153153
message: str
@@ -156,7 +156,7 @@ class Match:
156156
replacements: List[str]
157157
"""A list of suggested replacements for the error."""
158158

159-
offsetInContext: int
159+
offset_in_context: int
160160
"""The offset of the error in the context."""
161161

162162
context: str
@@ -165,13 +165,13 @@ class Match:
165165
offset: int
166166
"""The offset of the error."""
167167

168-
errorLength: int
168+
error_length: int
169169
"""The length of the error."""
170170

171171
category: str
172172
"""The category of the rule that was violated."""
173173

174-
ruleIssueType: str
174+
rule_issue_type: str
175175
"""The issue type of the rule that was violated."""
176176

177177
def __init__(self, attrib: Dict[str, Any], text: str) -> None:
@@ -184,16 +184,16 @@ def __init__(self, attrib: Dict[str, Any], text: str) -> None:
184184

185185
# Process rule.
186186
attrib["category"] = attrib["rule"]["category"]["id"]
187-
attrib["ruleId"] = attrib["rule"]["id"]
188-
attrib["ruleIssueType"] = attrib["rule"]["issueType"]
187+
attrib["rule_id"] = attrib["rule"]["id"]
188+
attrib["rule_issue_type"] = attrib["rule"]["issueType"]
189189
del attrib["rule"]
190190
# Process context.
191-
attrib["offsetInContext"] = attrib["context"]["offset"]
191+
attrib["offset_in_context"] = attrib["context"]["offset"]
192192
attrib["context"] = attrib["context"]["text"]
193193
# Process replacements.
194194
attrib["replacements"] = [r["value"] for r in attrib["replacements"]]
195195
# Rename error length.
196-
attrib["errorLength"] = attrib["length"]
196+
attrib["error_length"] = attrib["length"]
197197
# Normalize unicode
198198
attrib["message"] = unicodedata.normalize("NFKC", attrib["message"])
199199
# Store objects on self.
@@ -253,25 +253,25 @@ def __str__(self) -> str:
253253
:return: A formatted string describing the match object.
254254
:rtype: str
255255
"""
256-
ruleId = self.ruleId
257-
s = f"Offset {self.offset}, length {self.errorLength}, Rule ID: {ruleId}"
256+
rule_id = self.rule_id
257+
s = f"Offset {self.offset}, length {self.error_length}, Rule ID: {rule_id}"
258258
if self.message:
259259
s += f"\nMessage: {self.message}"
260260
if self.replacements:
261261
s += f"\nSuggestion: {'; '.join(self.replacements)}"
262-
s += f"\n{self.context}\n{' ' * self.offsetInContext + '^' * self.errorLength}"
262+
s += f"\n{self.context}\n{' ' * self.offset_in_context + '^' * self.error_length}"
263263
return s
264264

265265
@property
266-
def matchedText(self) -> str:
266+
def matched_text(self) -> str:
267267
"""
268268
Returns the substring from the context that corresponds to the matched text.
269269
270270
:return: The matched text from the context.
271271
:rtype: str
272272
"""
273273
return self.context[
274-
self.offsetInContext : self.offsetInContext + self.errorLength
274+
self.offset_in_context : self.offset_in_context + self.error_length
275275
]
276276

277277
def get_line_and_column(self, original_text: str) -> Tuple[int, int]:

language_tool_python/server.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ class LanguageTool:
6565
6666
:param language: The language to be used by the LanguageTool server. If None, it will try to detect the system language.
6767
:type language: Optional[str]
68-
:param motherTongue: The mother tongue of the user.
69-
:type motherTongue: Optional[str]
68+
:param mother_tongue: The mother tongue of the user.
69+
:type mother_tongue: Optional[str]
7070
:param remote_server: URL of a remote LanguageTool server. If provided, the local server will not be started.
7171
:type remote_server: Optional[str]
72-
:param newSpellings: Custom spellings to be added to the LanguageTool server.
73-
:type newSpellings: Optional[List[str]]
72+
:param new_spellings: Custom spellings to be added to the LanguageTool server.
73+
:type new_spellings: Optional[List[str]]
7474
:param new_spellings_persist: Whether the new spellings should persist across sessions.
7575
:type new_spellings_persist: Optional[bool]
7676
:param host: The host address for the LanguageTool server. Defaults to 'localhost'.
@@ -144,9 +144,9 @@ class LanguageTool:
144144
def __init__(
145145
self,
146146
language: Optional[str] = None,
147-
motherTongue: Optional[str] = None,
147+
mother_tongue: Optional[str] = None,
148148
remote_server: Optional[str] = None,
149-
newSpellings: Optional[List[str]] = None,
149+
new_spellings: Optional[List[str]] = None,
150150
new_spellings_persist: bool = True,
151151
host: Optional[str] = None,
152152
config: Optional[Dict[str, Any]] = None,
@@ -160,8 +160,7 @@ def __init__(
160160
self._new_spellings = None
161161
self._new_spellings_persist = new_spellings_persist
162162
self._host = host or socket.gethostbyname("localhost")
163-
self._available_ports = list(range(8081, 8999))
164-
random.shuffle(self._available_ports)
163+
self._available_ports = random.sample(range(8081, 8999), (8999 - 8081))
165164
self._port = self._available_ports.pop()
166165
self._server = None
167166

@@ -183,11 +182,11 @@ def __init__(
183182
language = get_locale_language()
184183
except ValueError:
185184
language = FAILSAFE_LANGUAGE
186-
if newSpellings:
187-
self._new_spellings = newSpellings
185+
if new_spellings:
186+
self._new_spellings = new_spellings
188187
self._register_spellings()
189188
self._language = LanguageTag(language, self._get_languages())
190-
self._mother_tongue = motherTongue
189+
self._mother_tongue = mother_tongue
191190
self.disabled_rules = set()
192191
self.enabled_rules = set()
193192
self.disabled_categories = set()
@@ -255,7 +254,7 @@ def __repr__(self) -> str:
255254
:return: A string that includes the class name, language, and mother tongue.
256255
:rtype: str
257256
"""
258-
return f"{self.__class__.__name__}(language={self.language!r}, motherTongue={self.motherTongue!r})"
257+
return f"{self.__class__.__name__}(language={self.language!r}, motherTongue={self.mother_tongue!r})"
259258

260259
def close(self) -> None:
261260
"""
@@ -297,7 +296,7 @@ def language(self, language: str) -> None:
297296
self.enabled_rules.clear()
298297

299298
@property
300-
def motherTongue(self) -> Optional[LanguageTag]:
299+
def mother_tongue(self) -> Optional[LanguageTag]:
301300
"""
302301
Retrieve the mother tongue language tag.
303302
@@ -308,16 +307,16 @@ def motherTongue(self) -> Optional[LanguageTag]:
308307
return LanguageTag(self._mother_tongue, self._get_languages())
309308
return None
310309

311-
@motherTongue.setter
312-
def motherTongue(self, motherTongue: Optional[str]) -> None:
310+
@mother_tongue.setter
311+
def mother_tongue(self, mother_tongue: Optional[str]) -> None:
313312
"""
314313
Sets the mother tongue for the language tool.
315314
316-
:param motherTongue: The mother tongue language tag as a string. If None, the mother tongue is set to None.
317-
:type motherTongue: Optional[str]
315+
:param mother_tongue: The mother tongue language tag as a string. If None, the mother tongue is set to None.
316+
:type mother_tongue: Optional[str]
318317
"""
319318

320-
self._mother_tongue = motherTongue
319+
self._mother_tongue = mother_tongue
321320

322321
@property
323322
def _spell_checking_categories(self) -> Set[str]:
@@ -367,8 +366,8 @@ def _create_params(self, text: str) -> Dict[str, str]:
367366
- 'level': 'picky' if picky mode is enabled.
368367
"""
369368
params = {"language": str(self.language), "text": text}
370-
if self.motherTongue is not None:
371-
params["motherTongue"] = self.motherTongue.tag
369+
if self.mother_tongue is not None:
370+
params["motherTongue"] = self.mother_tongue.tag
372371
if self.disabled_rules:
373372
params["disabledRules"] = ",".join(self.disabled_rules)
374373
if self.enabled_rules:
@@ -452,9 +451,7 @@ def _register_spellings(self) -> None:
452451
spelling_file_path = self._get_valid_spelling_file_path()
453452
logger.debug("Registering new spellings at %s", spelling_file_path)
454453
with open(spelling_file_path, "r+", encoding="utf-8") as spellings_file:
455-
existing_spellings = set(
456-
line.strip() for line in spellings_file.readlines()
457-
)
454+
existing_spellings = {line.strip() for line in spellings_file.readlines()}
458455
new_spellings = [
459456
word for word in self._new_spellings if word not in existing_spellings
460457
]

language_tool_python/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ def correct(text: str, matches: List[Match]) -> str:
104104
ltext = list(text)
105105
matches = [match for match in matches if match.replacements]
106106
errors = [
107-
ltext[match.offset : match.offset + match.errorLength] for match in matches
107+
ltext[match.offset : match.offset + match.error_length] for match in matches
108108
]
109109
correct_offset = 0
110110
for n, match in enumerate(matches):
111111
frompos, topos = (
112112
correct_offset + match.offset,
113-
correct_offset + match.offset + match.errorLength,
113+
correct_offset + match.offset + match.error_length,
114114
)
115115
if ltext[frompos:topos] != errors[n]:
116116
continue

0 commit comments

Comments
 (0)