5
5
import logging
6
6
import importlib .resources
7
7
import spellchecker
8
+ import requests
8
9
9
10
10
11
def create_checker (dict_list : list [str ] = None ) -> spellchecker .SpellChecker :
@@ -20,15 +21,35 @@ def create_checker(dict_list: list[str] = None) -> spellchecker.SpellChecker:
20
21
# load the English dictionary
21
22
lib_path = importlib .resources .files (spellchecker )
22
23
english_dict = str (lib_path ) + "/resources/en.json.gz"
23
- logger .info ("Loading English dictionary from: %s" , english_dict )
24
24
checker .word_frequency .load_dictionary (english_dict )
25
+ logger .info ("Loaded %s" , english_dict )
26
+ logger .info ("%d words" , checker .word_frequency .unique_words )
25
27
26
28
# load the additional dictionaries
27
- if not isinstance (dict_list , list ):
29
+ if not isinstance (dict_list , list ) or not dict_list :
28
30
return checker
29
- if len (dict_list ) > 0 :
30
- for d in dict_list :
31
- logger .info ("Loading additional dictionary from: %s" , d )
32
- checker .word_frequency .load_text_file (d )
31
+
32
+ for d in dict_list :
33
+
34
+ # load dictionary from URL
35
+ try :
36
+ response = requests .get (d )
37
+ response .raise_for_status ()
38
+ checker .word_frequency .load_text (response .text )
39
+
40
+ except requests .exceptions .MissingSchema :
41
+ # URL didn't work so assume it's a local file path
42
+ try :
43
+ checker .word_frequency .load_text_file (d )
44
+ except IOError :
45
+ logger .error ("Error loading %s" , d )
46
+ continue
47
+
48
+ except requests .exceptions .RequestException as e :
49
+ logger .error ("Error loading dictionary from URL %s: %s" , d , e )
50
+ continue
51
+
52
+ logger .info ("Loaded %s" , d )
53
+ logger .info ("%d words" , checker .word_frequency .unique_words )
33
54
34
55
return checker
0 commit comments