@@ -14,6 +14,7 @@ class LCAUI:
1414 def __init__ (self ) -> None :
1515 self .args_parser : argparse .ArgumentParser = self .create_args_parser ()
1616 self .options : argparse .Namespace = argparse .Namespace ()
17+ self .scaio = SCAIO ()
1718
1819 self .is_spacy_initialized : bool = False
1920
@@ -106,7 +107,7 @@ def parse_args(self, argv: List[str]) -> SCAProcedureResult:
106107 return False , "Unexpected argument(s):\n \n {}" .format ("\n " .join (ifile_list ))
107108 self .verified_ifiles = None
108109 else :
109- self .verified_ifiles = SCAIO .get_verified_ifile_list (ifile_list )
110+ self .verified_ifiles = self . scaio .get_verified_ifile_list (ifile_list )
110111
111112 self .init_kwargs = {
112113 "wordlist" : options .wordlist ,
@@ -122,24 +123,50 @@ def install_spacy(self) -> SCAProcedureResult:
122123 from subprocess import CalledProcessError
123124
124125 command = [sys .executable , "-m" , "pip" , "install" , "-U" , "spacy" ]
126+ if get_yes_or_no (
127+ "Do you want to download spaCy from a Chinese mirror site? If you"
128+ " are inside of China, you may want to use this for a faster network"
129+ " connection."
130+ ):
131+ command .extend (["-i" , "https://pypi.tuna.tsinghua.edu.cn/simple" ])
132+
125133 try :
126134 subprocess .run (command , check = True , capture_output = False )
127135 except CalledProcessError as e :
128136 return False , f"Failed to install spaCy: { e } "
129137
130- command = [sys .executable , "-m" , "spacy" , "download" , "en_core_web_sm" ]
138+ return True , None
139+
140+ def install_model (self ) -> SCAProcedureResult :
141+ import subprocess
142+ from subprocess import CalledProcessError
143+
144+ if get_yes_or_no (
145+ "Do you want to download en_core_web_sm from sourceforge.net? If you"
146+ " are inside of China, you may want to use this for a faster network"
147+ " connection."
148+ ):
149+ command = [
150+ sys .executable ,
151+ "-m" ,
152+ "pip" ,
153+ "install" ,
154+ "https://master.dl.sourceforge.net/project/en-core-web-sm/en_core_web_sm-3.6.0-py3-none-any.whl?viasf=1" ,
155+ ]
156+ else :
157+ command = [sys .executable , "-m" , "spacy" , "download" , "en_core_web_sm" ]
158+
131159 try :
132160 subprocess .run (command , check = True , capture_output = False )
133161 except CalledProcessError as e :
134162 return False , f"Failed to download en_core_web_sm: { e } "
135163
136164 return True , None
137165
138- def check_spacy (self ) -> SCAProcedureResult :
166+ def check_spacy_and_model (self ) -> SCAProcedureResult :
139167 try :
140168 logging .info ("Trying to load spaCy..." )
141169 import spacy # type: ignore # noqa: F401 'spacy' imported but unused
142- import en_core_web_sm # type: ignore # noqa: F401 'en_core_web_sm' imported but unused
143170 except ModuleNotFoundError :
144171 is_install = get_yes_or_no (
145172 "Running LCA requires spaCy. Do you want me to install it for you?"
@@ -148,26 +175,56 @@ def check_spacy(self) -> SCAProcedureResult:
148175 return (
149176 False ,
150177 (
151- "\n spaCy installation refused. You need to manually install it using:"
178+ "\n spaCy installation is refused. You need to manually install it using:"
152179 "\n pip install spacy"
153- "\n python -m spacy download en_core_web_sm"
154180 ),
155181 )
156182 return self .install_spacy ()
157183 else :
158184 color_print ("OKGREEN" , "ok" , prefix = "spaCy has already been installed. " )
185+
186+ try :
187+ logging .info ("Trying to load en_core_web_sm..." )
188+ import en_core_web_sm # type: ignore # noqa: F401 'en_core_web_sm' imported but unused
189+ except ModuleNotFoundError :
190+ is_install = get_yes_or_no (
191+ "Running LCA requires spaCy's en_core_web_sm model. Do you want me to install it for you?"
192+ )
193+ if not is_install :
194+ return (
195+ False ,
196+ (
197+ "\n en_core_web_sm installation is refused. You need to manually install it using:"
198+ "\n python -m spacy download en_core_web_sm"
199+ ),
200+ )
201+ return self .install_model ()
202+ else :
203+ color_print ("OKGREEN" , "ok" , prefix = "en_core_web_sm has already been installed. " )
159204 return True , None
160205
206+ def exit_routine (self ) -> None :
207+ if self .options .is_quiet or self .options .is_stdout :
208+ return
209+
210+ color_print (
211+ "OKGREEN" ,
212+ f"{ os_path .abspath (self .options .ofile )} " ,
213+ prefix = "Output has been saved to " ,
214+ postfix = ". Done." ,
215+ )
216+
161217 def run_tmpl (func : Callable ): # type:ignore
162218 def wrapper (self , * args , ** kwargs ):
163- sucess , err_msg = self .check_spacy ()
219+ sucess , err_msg = self .check_spacy_and_model ()
164220 if not sucess :
165221 return sucess , err_msg
166222 if not self .options .is_stdout :
167223 sucess , err_msg = SCAIO .is_writable (self .options .ofile )
168224 if not sucess :
169225 return sucess , err_msg
170226 func (self , * args , ** kwargs )
227+ self .exit_routine ()
171228 return True , None
172229
173230 return wrapper
0 commit comments