|
5 | 5 | import socket |
6 | 6 | import threading |
7 | 7 | import urllib.parse |
| 8 | +from typing import List |
8 | 9 | from weakref import WeakValueDictionary |
9 | 10 | from .download_lt import download_lt |
10 | 11 | from .language_tag import LanguageTag |
@@ -33,7 +34,9 @@ class LanguageTool: |
33 | 34 | _instances = WeakValueDictionary() |
34 | 35 | _PORT_RE = re.compile(r"(?:https?://.*:|port\s+)(\d+)", re.I) |
35 | 36 |
|
36 | | - def __init__(self, language=None, motherTongue=None, remote_server=None): |
| 37 | + def __init__(self, language=None, motherTongue=None, remote_server=None, newSpellings: List[str] = None): |
| 38 | + if newSpellings: |
| 39 | + self._register_spellings(newSpellings) |
37 | 40 | if remote_server is not None: |
38 | 41 | self._remote = True |
39 | 42 | self._url = parse_url(remote_server) |
@@ -124,6 +127,16 @@ def disable_spellchecking(self): |
124 | 127 | """Disable spell-checking rules.""" |
125 | 128 | self.disabled_categories.update(self._spell_checking_categories) |
126 | 129 |
|
| 130 | + def _register_spellings(self, spellings: List[str]): |
| 131 | + library_path = get_language_tool_directory() |
| 132 | + spelling_file_path = os.path.join(library_path, "org/languagetool/resource/en/hunspell/spelling.txt") |
| 133 | + if not os.path.exists(spelling_file_path): |
| 134 | + raise FileNotFoundError(f"Failed to find the spellings file at {spelling_file_path}\n" |
| 135 | + f"Please file an issue at https://github.com/jxmorris12/language_tool_python") |
| 136 | + with open(spelling_file_path, "a+") as spellings_file: |
| 137 | + spellings_file.write("\n" + "\n".join([word for word in spellings])) |
| 138 | + print(f"Updated the spellings at {spelling_file_path}") |
| 139 | + |
127 | 140 | def _get_languages(self) -> set: |
128 | 141 | """Get supported languages (by querying the server).""" |
129 | 142 | self._start_server_if_needed() |
|
0 commit comments