22additional dictionaries if provided.
33"""
44
5+ import pathlib
56import logging
67import importlib .resources
78import spellchecker
9+ import requests
810
911
1012def create_checker (dict_list : list [str ] = None ) -> spellchecker .SpellChecker :
@@ -22,13 +24,44 @@ def create_checker(dict_list: list[str] = None) -> spellchecker.SpellChecker:
2224 english_dict = str (lib_path ) + "/resources/en.json.gz"
2325 logger .info ("Loading English dictionary from: %s" , english_dict )
2426 checker .word_frequency .load_dictionary (english_dict )
27+ logger .info ("# of words: %d" , checker .word_frequency .unique_words )
2528
2629 # load the additional dictionaries
2730 if not isinstance (dict_list , list ):
2831 return checker
2932 if len (dict_list ) > 0 :
3033 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 )
3366
3467 return checker
0 commit comments