5
5
import logging
6
6
import importlib .resources
7
7
import spellchecker
8
+ import requests
9
+ import pathlib
8
10
9
11
10
12
def create_checker (dict_list : list [str ] = None ) -> spellchecker .SpellChecker :
@@ -22,13 +24,26 @@ 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 ("number of words: %s" , 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
34
logger .info ("Loading additional dictionary from: %s" , d )
32
- checker .word_frequency .load_text_file (d )
35
+ if isinstance (d , pathlib .PosixPath ):
36
+ # assume it's a local file path
37
+ checker .word_frequency .load_text_file (d )
38
+ else :
39
+ # load dictionary from URL
40
+ if d .startswith ("http://" ) or d .startswith ("https://" ):
41
+ response = requests .get (d )
42
+ response .raise_for_status ()
43
+ checker .word_frequency .load_text (response .text )
44
+ else :
45
+ # assume it's a local file path
46
+ checker .word_frequency .load_text_file (d )
47
+ logger .info ("# of words: %s" , checker .word_frequency .unique_words )
33
48
34
49
return checker
0 commit comments