Skip to content

Commit 726b8b6

Browse files
authored
Merge pull request jxmorris12#19 from misrasaurabh1/master
Add function to register new spellings.
2 parents 77b6d83 + fe7cb2d commit 726b8b6

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

‎language_tool_python/server.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import socket
66
import threading
77
import urllib.parse
8+
from typing import List
89
from weakref import WeakValueDictionary
910
from .download_lt import download_lt
1011
from .language_tag import LanguageTag
@@ -33,7 +34,9 @@ class LanguageTool:
3334
_instances = WeakValueDictionary()
3435
_PORT_RE = re.compile(r"(?:https?://.*:|port\s+)(\d+)", re.I)
3536

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)
3740
if remote_server is not None:
3841
self._remote = True
3942
self._url = parse_url(remote_server)
@@ -124,6 +127,16 @@ def disable_spellchecking(self):
124127
"""Disable spell-checking rules."""
125128
self.disabled_categories.update(self._spell_checking_categories)
126129

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+
127140
def _get_languages(self) -> set:
128141
"""Get supported languages (by querying the server)."""
129142
self._start_server_if_needed()

0 commit comments

Comments
 (0)