-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcreate_checker.py
More file actions
55 lines (42 loc) · 1.81 KB
/
create_checker.py
File metadata and controls
55 lines (42 loc) · 1.81 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
"""Create a case sensitive spell checker with the English dictionary and
additional dictionaries if provided.
"""
import logging
import importlib.resources
import spellchecker
import requests
def create_checker(dict_list: list[str] = None) -> spellchecker.SpellChecker:
"""Create a case sensitive spell checker with the English dictionary and
additional dictionaries if provided."""
logger = logging.getLogger("comment_spell_check.create_checker")
# create an empty SpellChecker object, because we want a case
# sensitive checker
checker = spellchecker.SpellChecker(language=None, case_sensitive=True)
# load the English dictionary
lib_path = importlib.resources.files(spellchecker)
english_dict = str(lib_path) + "/resources/en.json.gz"
checker.word_frequency.load_dictionary(english_dict)
logger.info("Loaded %s", english_dict)
logger.info("%d words", checker.word_frequency.unique_words)
# load the additional dictionaries
if not isinstance(dict_list, list) or not dict_list:
return checker
for d in dict_list:
# load dictionary from URL
try:
response = requests.get(d)
response.raise_for_status()
checker.word_frequency.load_text(response.text)
except requests.exceptions.MissingSchema:
# URL didn't work so assume it's a local file path
try:
checker.word_frequency.load_text_file(d)
except IOError:
logger.error("Error loading %s", d)
continue
except requests.exceptions.RequestException as e:
logger.error("Error loading dictionary from URL %s: %s", d, e)
continue
logger.info("Loaded %s", d)
logger.info("%d words", checker.word_frequency.unique_words)
return checker