55import logging
66import importlib .resources
77import spellchecker
8+ import requests
89
910
1011def create_checker (dict_list : list [str ] = None ) -> spellchecker .SpellChecker :
@@ -20,15 +21,35 @@ def create_checker(dict_list: list[str] = None) -> spellchecker.SpellChecker:
2021 # load the English dictionary
2122 lib_path = importlib .resources .files (spellchecker )
2223 english_dict = str (lib_path ) + "/resources/en.json.gz"
23- logger .info ("Loading English dictionary from: %s" , english_dict )
2424 checker .word_frequency .load_dictionary (english_dict )
25+ logger .info ("Loaded %s" , english_dict )
26+ logger .info ("%d words" , checker .word_frequency .unique_words )
2527
2628 # load the additional dictionaries
27- if not isinstance (dict_list , list ):
29+ if not isinstance (dict_list , list ) or not dict_list :
2830 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 )
3354
3455 return checker
0 commit comments