From fa0e70ac98d5957235b1da1ddfe73676c7ecaa60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Fri, 8 Mar 2024 02:03:45 +0800 Subject: [PATCH] display error --- .../LunaTranslator/LunaTranslator.py | 32 ++----------- .../LunaTranslator/gui/textbrowser.py | 2 +- .../LunaTranslator/gui/translatorUI.py | 48 ++++++++++++++++--- .../translator/basetranslator.py | 26 ++++------ 4 files changed, 55 insertions(+), 53 deletions(-) diff --git a/LunaTranslator/LunaTranslator/LunaTranslator.py b/LunaTranslator/LunaTranslator/LunaTranslator.py index 136e5170..ae036041 100644 --- a/LunaTranslator/LunaTranslator/LunaTranslator.py +++ b/LunaTranslator/LunaTranslator/LunaTranslator.py @@ -52,8 +52,6 @@ class MAINUI() : self.currentsignature=None self.isrunning=True self.solvegottextlock=threading.Lock() - self.castkata2hira=str.maketrans(static_data['allkata'],static_data['allhira']) - self.casthira2kata=str.maketrans(static_data['allhira'],static_data['allkata']) @property def textsource(self):return self.textsource_p @textsource.setter @@ -183,9 +181,6 @@ class MAINUI() : msg=str(type(e))[8:-2]+' '+str(e).replace('\n','').replace('\r','') self.translation_ui.displaystatus.emit(msg,'red',True,True) return - - while len(text) and text[-1] in '\r\n \t': #在后处理之后在去除换行,这样换行符可以当作行结束符给后处理用 - text=text[:-1] if text=='' or (is_auto_run and (text==self.currenttext or len(text)>(max(globalconfig['maxoriginlength'],globalconfig['maxlength'])))): if embedcallback: @@ -205,33 +200,12 @@ class MAINUI() : self.currentread=text self.autoreadcheckname() - try: - if self.hira_: - hira=self.hira_.fy(text) - for _1 in range(len(hira)): - _=len(hira)-1-_1 - if globalconfig['hira_vis_type']==0: - hira[_]['hira']=hira[_]['hira'].translate(self.castkata2hira) - elif globalconfig['hira_vis_type']==1: - hira[_]['hira']=hira[_]['hira'].translate(self.casthira2kata) - elif globalconfig['hira_vis_type']==2: - __kanas=[static_data['hira']+['っ'],static_data['kata']+['ッ']] - target=static_data['roma']+['-'] - for _ka in __kanas: - for __idx in range(len(_ka)): - _reverse_idx=len(_ka)-1-__idx - hira[_]['hira']=hira[_]['hira'].replace(_ka[_reverse_idx],target[_reverse_idx]) - else: - hira=[] - except: - print_exc() - hira=[] if globalconfig['refresh_on_get_trans']==False: - self.translation_ui.displayraw1.emit(dict(hira=hira,text=text,color=globalconfig['rawtextcolor'],onlytrans=onlytrans)) + self.translation_ui.displayraw1.emit(dict(text=text,color=globalconfig['rawtextcolor'],onlytrans=onlytrans)) _showrawfunction=None _showrawfunction_sig=0 else: - _showrawfunction=functools.partial(self.translation_ui.displayraw1.emit,dict(hira=hira,text=text,color=globalconfig['rawtextcolor'],onlytrans=onlytrans)) + _showrawfunction=functools.partial(self.translation_ui.displayraw1.emit,dict(text=text,color=globalconfig['rawtextcolor'],onlytrans=onlytrans)) _showrawfunction_sig=time.time() text_solved,optimization_params= self.solvebeforetrans(text) @@ -266,7 +240,7 @@ class MAINUI() : #print(keys,usenum,self.lasttranslatorindex) for engine in keys: if engine not in self.premtalready: - self.translators[engine].gettask((partial(self.GetTranslationCallback,onlytrans,engine,self.currentsignature, optimization_params,_showrawfunction,_showrawfunction_sig,text),text,text_solved,skip,embedcallback,is_auto_run,hira)) + self.translators[engine].gettask((partial(self.GetTranslationCallback,onlytrans,engine,self.currentsignature, optimization_params,_showrawfunction,_showrawfunction_sig,text),text,text_solved,skip,embedcallback,is_auto_run)) thistimeusednum+=1 self.lasttranslatorindex+=1 if(thistimeusednum>=usenum): diff --git a/LunaTranslator/LunaTranslator/gui/textbrowser.py b/LunaTranslator/LunaTranslator/gui/textbrowser.py index 68a3d503..44ab8aaf 100644 --- a/LunaTranslator/LunaTranslator/gui/textbrowser.py +++ b/LunaTranslator/LunaTranslator/gui/textbrowser.py @@ -363,7 +363,7 @@ class Textbrowser( ): tl3=self.textbrowser.cursorRect(self.textcursor).topLeft() color=self.randomcolor(word) if color: - if word['orig'] not in ['\n','\r',' ',''] : + if word['orig'] not in ['\n',' ',''] : if labeli >=len(self.searchmasklabels)-1: ql=QLabel(self.atback2) ql.setMouseTracking(True) diff --git a/LunaTranslator/LunaTranslator/gui/translatorUI.py b/LunaTranslator/LunaTranslator/gui/translatorUI.py index 59935210..c9f1ee46 100644 --- a/LunaTranslator/LunaTranslator/gui/translatorUI.py +++ b/LunaTranslator/LunaTranslator/gui/translatorUI.py @@ -7,8 +7,7 @@ import winsharedutils from PyQt5.QtCore import QT_VERSION_STR import windows from traceback import print_exc -from PyQt5.QtWidgets import QWidget, QLabel, QPushButton, QVBoxLayout,QApplication -from PyQt5.QtCore import Qt, pyqtSignal ,QThread +from PyQt5.QtCore import Qt, pyqtSignal import qtawesome from PyQt5.QtCore import pyqtSignal,Qt,QRect,QSize from PyQt5.QtGui import QFont ,QIcon,QPixmap ,QMouseEvent,QCursor @@ -16,7 +15,7 @@ from PyQt5.QtWidgets import QLabel ,QPushButton ,QSystemTrayIcon ,QAction,QMenu import gobject from myutils.wrapper import threader import winsharedutils,queue -from myutils.config import globalconfig,saveallconfig,_TR +from myutils.config import globalconfig,saveallconfig,_TR,static_data from myutils.subproc import endsubprocs from myutils.ocrutil import ocr_run,imageCut from myutils.hwnd import mouseselectwindow ,showintab,grabwindow,getExeIcon @@ -104,7 +103,6 @@ class QUnFrameWindow(resizableframeless): except: print_exc() def showraw(self,kwargs):#hira,res,color,onlytrans): - hira=kwargs.get('hira') text=kwargs.get('text') color=kwargs.get('color') onlytrans=kwargs.get('onlytrans') @@ -118,7 +116,7 @@ class QUnFrameWindow(resizableframeless): else: _res=text if globalconfig['isshowhira'] and globalconfig['isshowrawtext']: - self.showline(clear=clear,text=_res,hira=hira,color=color) + self.showline(clear=clear,text=_res,hira=True,color=color) elif globalconfig['isshowrawtext']: self.showline(clear=clear,text=_res,color=color) else: @@ -127,19 +125,53 @@ class QUnFrameWindow(resizableframeless): gobject.baseobject.edittextui.getnewsentencesignal.emit(text) def showstatus(self,res,color,clear,origin): self.showline(clear=clear,text=res,color=color,origin=origin) + def cleartext(self,text): + text=text.replace('\t',' ') + text=text.replace('\r','\n') + lines=text.split('\n') + newlines=[] + for line in lines: + if len(line.strip()): + newlines.append(line) + return '\n'.join(newlines) + def parsehira(self,text): + hira=[] + try: + if gobject.baseobject.hira_: + hira=gobject.baseobject.hira_.fy(text) + for _1 in range(len(hira)): + _=len(hira)-1-_1 + if globalconfig['hira_vis_type']==0: + hira[_]['hira']=hira[_]['hira'].translate(self.castkata2hira) + elif globalconfig['hira_vis_type']==1: + hira[_]['hira']=hira[_]['hira'].translate(self.casthira2kata) + elif globalconfig['hira_vis_type']==2: + __kanas=[static_data['hira']+['っ'],static_data['kata']+['ッ']] + target=static_data['roma']+['-'] + for _ka in __kanas: + for __idx in range(len(_ka)): + _reverse_idx=len(_ka)-1-__idx + hira[_]['hira']=hira[_]['hira'].replace(_ka[_reverse_idx],target[_reverse_idx]) + except: + print_exc() + return hira def showline (self,**kwargs):# clear,res,color ,type_=1,origin=True): clear=kwargs.get('clear',True) origin=kwargs.get('origin',True) text=kwargs.get('text',None) color=kwargs.get('color',None) - hira=kwargs.get('hira',[]) + hira=kwargs.get('hira',False) iter_context=kwargs.get('iter_context',None) if clear: self.translate_text.clear() if text is None: return - + text=self.cleartext(text) + if hira: + hira=self.parsehira(text) + else: + hira=[] self.translate_text.setnextfont(origin) if globalconfig['showatcenter']: @@ -444,6 +476,8 @@ class QUnFrameWindow(resizableframeless): self.quitf_signal.connect(self.close) self.fullsgame_signal.connect(self._fullsgame) + self.castkata2hira=str.maketrans(static_data['allkata'],static_data['allhira']) + self.casthira2kata=str.maketrans(static_data['allhira'],static_data['allkata']) self.isletgamefullscreened=False self.fullscreenmanager=fullscreen(self._externalfsend) self._isTracking=False diff --git a/LunaTranslator/LunaTranslator/translator/basetranslator.py b/LunaTranslator/LunaTranslator/translator/basetranslator.py index 8e627085..2f4a2a89 100644 --- a/LunaTranslator/LunaTranslator/translator/basetranslator.py +++ b/LunaTranslator/LunaTranslator/translator/basetranslator.py @@ -167,7 +167,7 @@ class basetrans(commonbase): if langkey not in self._cache: self._cache[langkey]={} self._cache[langkey][src] = tgt - def cached_translate(self,contentsolved,hira,is_auto_run): + def cached_translate(self,contentsolved,is_auto_run): is_using_gpt_and_retrans= is_auto_run==False and self.is_gpt_like if is_using_gpt_and_retrans==False: res=self.shorttermcacheget(contentsolved) @@ -179,21 +179,21 @@ class basetrans(commonbase): return res if self.transtype=='offline': - res=self.dispatch_translate(contentsolved,hira) + res=self.translate(contentsolved) else: - res=self.intervaledtranslate(contentsolved,hira) + res=self.intervaledtranslate(contentsolved) return res def cachesetatend(self,contentsolved,res): if globalconfig['uselongtermcache']: self.longtermcacheset(contentsolved,res) self.shorttermcacheset(contentsolved,res) - def maybecachetranslate(self,contentraw,contentsolved,hira,is_auto_run): + def maybecachetranslate(self,contentraw,contentsolved,is_auto_run): if self.transtype=='pre': res=self.translate(contentraw) else: - res=self.cached_translate(contentsolved,hira,is_auto_run) + res=self.cached_translate(contentsolved,is_auto_run) return res - def intervaledtranslate(self,content,hira): + def intervaledtranslate(self,content): interval=globalconfig['requestinterval'] current=time.time() self.current=current @@ -207,15 +207,9 @@ class basetrans(commonbase): self.multiapikeycurrentidx+=1 - res=self.dispatch_translate(content,hira) + res=self.translate(content) return res - def dispatch_translate(self,content,hira): - if 'translate_with_extra' in dir(self): - res=self.translate_with_extra(content,{'hira':hira}) - else: - res=self.translate(content) - return res @property def onlymanual(self): if 'manual' not in globalconfig['fanyi'][self.typename] : @@ -229,7 +223,7 @@ class basetrans(commonbase): while True: _=self.queue.get() if _ is None:break - callback,contentraw,contentsolved,skip,embedcallback,is_auto_run,hira=_ + callback,contentraw,contentsolved,skip,embedcallback,is_auto_run=_ if embedcallback is not None: savelast.clear() @@ -238,7 +232,7 @@ class basetrans(commonbase): break if self.using==False:break if savelast[0][4] is not None: - callback,contentraw,contentsolved,skip,embedcallback,is_auto_run,hira=savelast.pop(0) + callback,contentraw,contentsolved,skip,embedcallback,is_auto_run=savelast.pop(0) for _ in savelast: self.gettask(_) if embedcallback is None: @@ -259,7 +253,7 @@ class basetrans(commonbase): self._private_init() except Exception as e: raise Exception('inittranslator failed : '+str(stringfyerror(e))) - return self.maybecachetranslate(contentraw,contentsolved,hira,is_auto_run) + return self.maybecachetranslate(contentraw,contentsolved,is_auto_run) res=timeoutfunction(reinitandtrans,checktutukufunction=checktutukufunction ) collectiterres=[]