2
2
additional dictionaries if provided.
3
3
"""
4
4
5
+ import pathlib
5
6
import logging
6
7
import importlib .resources
7
8
import spellchecker
9
+ import requests
8
10
9
11
10
12
def create_checker (dict_list : list [str ] = None ) -> spellchecker .SpellChecker :
@@ -22,13 +24,44 @@ def create_checker(dict_list: list[str] = None) -> spellchecker.SpellChecker:
22
24
english_dict = str (lib_path ) + "/resources/en.json.gz"
23
25
logger .info ("Loading English dictionary from: %s" , english_dict )
24
26
checker .word_frequency .load_dictionary (english_dict )
27
+ logger .info ("# of words: %d" , checker .word_frequency .unique_words )
25
28
26
29
# load the additional dictionaries
27
30
if not isinstance (dict_list , list ):
28
31
return checker
29
32
if len (dict_list ) > 0 :
30
33
for d in dict_list :
31
- logger .info ("Loading additional dictionary from: %s" , d )
32
- checker .word_frequency .load_text_file (d )
34
+ if isinstance (d , pathlib .PosixPath ):
35
+ # local file path
36
+ try :
37
+ checker .word_frequency .load_text_file (d )
38
+ logger .info ("Loading dictionary: %s" , d )
39
+ except IsADirectoryError :
40
+ # if a directory is provided, load all text files in it
41
+ for file in d .glob ("*.txt" ):
42
+ try :
43
+ checker .word_frequency .load_text_file (file )
44
+ logger .info ("Loading dictionary: %s" , file )
45
+ except FileNotFoundError :
46
+ logger .error ("File not found: %s" , file )
47
+ continue
48
+ else :
49
+ # load dictionary from URL
50
+ try :
51
+ response = requests .get (d )
52
+ response .raise_for_status ()
53
+ checker .word_frequency .load_text (response .text )
54
+ logger .info ("Loading dictionary URL: %s" , d )
55
+ except requests .exceptions .MissingSchema :
56
+ # URL didn't work so assume it's a local file path
57
+ try :
58
+ checker .word_frequency .load_text_file (d )
59
+ logger .info ("Loading dictionary: %s" , d )
60
+ except FileNotFoundError :
61
+ logger .error ("File not found: %s" , d )
62
+ continue
63
+ except requests .exceptions .RequestException as e :
64
+ logger .error ("Error loading dictionary from URL %s: %s" , d , e )
65
+ logger .info ("# of words: %d" , checker .word_frequency .unique_words )
33
66
34
67
return checker
0 commit comments