11import configparser
22import concurrent .futures
3- from typing import List
3+ from typing import List , Callable
44from . import ecdict , tencent , argos
5+ from threading import Thread
56
7+ class ResultThread (Thread ):
8+ def __init__ (self , func , args ):
9+ super ().__init__ ()
10+ self .func = func
11+ self .args = args
12+ self .result = None
13+
14+ def run (self ):
15+ # 在线程启动时执行传入的函数,并保存结果
16+ self .result = self .func (* self .args )
17+
18+ def join (self ):
19+ # 等待线程执行完毕,并返回结果
20+ super ().join ()
21+ return self .result
22+
623# 太长或太短,或英文占比没有达到60%,都不翻译
724def is_translation_needed (text , target = "zh" ) -> bool :
825 total_chars = len (text )
@@ -21,10 +38,24 @@ def is_translation_needed(text, target = "zh") -> bool:
2138 return percentage <= 0.4
2239
2340def data_cleaning (text : str ) -> List [str ]:
24- texts = text .split ("\n \n " )
25- for (idt , text ) in enumerate (texts ):
26- texts [idt ] = text .replace ("\n " , " " ).strip ("' \" [](){}" )
27- return texts
41+ fragments = text .split ("\n \n " )
42+ for (idt , text ) in enumerate (fragments ):
43+ fragments [idt ] = text .replace ("\n " , " " ).strip ("' \" [](){}" )
44+ return fragments
45+
46+ def super_translater (translate : Callable [[str ], str ], fragments : List [str ],
47+ callback : Callable [[str , int ], None ], priority_level : int ):
48+ threads : List [ResultThread ] = []
49+ for fragment in fragments :
50+ threads .append (ResultThread (func = translate , args = (fragment ,)))
51+ for thread in threads :
52+ thread .start ()
53+ results = []
54+ for thread in threads :
55+ results .append (thread .join ())
56+ result = "\n \n " .join (results ).strip ()
57+ if result and not result .isascii ():
58+ callback (result , priority_level )
2859
2960class Translator ():
3061 def __init__ (self ):
@@ -37,8 +68,8 @@ def __init__(self):
3768 def set_ui (self , ui ):
3869 self .ui = ui
3970
40- def notify (self , text : str ):
41- self .ui .update_result (text )
71+ def notify (self , text : str , priority_level : int ):
72+ self .ui .update_result (text , priority_level )
4273
4374 # 翻译英文段落、单词为中文,结果为空表明不满足翻译要求
4475 def translate (self , text ) -> bool :
@@ -47,30 +78,16 @@ def translate(self, text) -> bool:
4778 text_list = data_cleaning (text )
4879 if len (text_list ) == 1 :
4980 if trans_result := self .ecdict_trans .translate (text_list [0 ]):
50- self .notify (trans_result )
81+ self .notify (trans_result , 1 )
5182 return True
52- with concurrent .futures .ThreadPoolExecutor () as executor :
53- futures_online = []
54- futures_local = []
55- for t in text_list :
56- future = executor .submit (self .tencent_trans .translate , t )
57- futures_online .append (future )
58- for t in text_list :
59- future = executor .submit (self .argos_trans .translate , t )
60- futures_local .append (future )
61- online_result = ""
62- local_result = ""
63- for future in futures_local :
64- r = future .result ()
65- local_result += r + "\n \n "
66- local_result = local_result .strip ()
67- self .notify (local_result )
68- for future in futures_online :
69- r = future .result ()
70- online_result += r + "\n \n "
71- online_result = online_result .strip ()
72- if not online_result .isascii ():
73- self .notify (online_result )
74- return True
7583
84+ local_thread = ResultThread (func = super_translater ,
85+ args = (self .argos_trans .translate , text_list , self .notify , 3 ))
86+ tencent_thread = ResultThread (func = super_translater ,
87+ args = (self .tencent_trans .translate , text_list , self .notify , 2 ))
88+ local_thread .start ()
89+ tencent_thread .start ()
90+ local_thread .join ()
91+ tencent_thread .join ()
92+ return True
7693
0 commit comments