This commit is contained in:
恍兮惚兮 2024-08-09 22:58:41 +08:00
parent 64fbc6cb1e
commit 8bea7ec7d9
5 changed files with 74 additions and 75 deletions

View File

@ -190,6 +190,15 @@ class MAINUI:
with self.solvegottextlock: with self.solvegottextlock:
self.textgetmethod_1(text, is_auto_run, embedcallback, onlytrans) self.textgetmethod_1(text, is_auto_run, embedcallback, onlytrans)
def parsehira(self, text):
try:
if self.hira_:
return self.hira_.safeparse(text)
else:
return []
except:
return []
def textgetmethod_1( def textgetmethod_1(
self, text, is_auto_run=True, embedcallback=None, onlytrans=False self, text, is_auto_run=True, embedcallback=None, onlytrans=False
): ):
@ -1250,7 +1259,7 @@ class MAINUI:
if bool(param): if bool(param):
self.translation_ui.settop() self.translation_ui.settop()
else: else:
if not globalconfig['keepontop']: if not globalconfig["keepontop"]:
self.translation_ui.canceltop() self.translation_ui.canceltop()
elif msg == 2: elif msg == 2:
self.translation_ui.closesignal.emit() self.translation_ui.closesignal.emit()

View File

@ -201,9 +201,7 @@ class AnkiWindow(QWidget):
example = self.example.toPlainText() example = self.example.toPlainText()
if globalconfig["ankiconnect"]["boldword"]: if globalconfig["ankiconnect"]["boldword"]:
if self.example.hiras is None: if self.example.hiras is None:
self.example.hiras = gobject.baseobject.translation_ui.parsehira( self.example.hiras = gobject.baseobject.parsehira(example)
example
)
collect = [] collect = []
for hira in self.example.hiras: for hira in self.example.hiras:
if hira["orig"] == word or hira.get("origorig", None) == word: if hira["orig"] == word or hira.get("origorig", None) == word:
@ -512,7 +510,7 @@ class AnkiWindow(QWidget):
if text and len(text): if text and len(text):
self.ruby = quote( self.ruby = quote(
json.dumps( json.dumps(
gobject.baseobject.translation_ui.parsehira(text), gobject.baseobject.parsehira(text),
ensure_ascii=False, ensure_ascii=False,
) )
) )
@ -651,7 +649,6 @@ class CustomTabBar(LTabBar):
return self.savesizehint return self.savesizehint
class searchwordW(closeashidewindow): class searchwordW(closeashidewindow):
search_word = pyqtSignal(str, bool) search_word = pyqtSignal(str, bool)
show_dict_result = pyqtSignal(float, str, str) show_dict_result = pyqtSignal(float, str, str)

View File

@ -366,21 +366,6 @@ class QUnFrameWindow(resizableframeless):
newlines.append(line) newlines.append(line)
return "\n".join(newlines) return "\n".join(newlines)
def parsehira(self, text):
hira = []
try:
if gobject.baseobject.hira_:
for i, _ in enumerate(text.split("\n")):
h = gobject.baseobject.hira_.parseparse(_)
if i:
hira += [{"orig": "\n", "hira": "\n"}]
hira += h
except:
print_exc()
return hira
def showline(self, **kwargs): # clear,res,color ,type_=1,origin=True): def showline(self, **kwargs): # clear,res,color ,type_=1,origin=True):
clear = kwargs.get("clear", True) clear = kwargs.get("clear", True)
origin = kwargs.get("origin", True) origin = kwargs.get("origin", True)
@ -413,7 +398,7 @@ class QUnFrameWindow(resizableframeless):
) )
needhira = isshow_fenci or isshowhira or isfenciclick needhira = isshow_fenci or isshowhira or isfenciclick
if needhira: if needhira:
hira = self.parsehira(text) hira = gobject.baseobject.parsehira(text)
self.translate_text.append( self.translate_text.append(
origin, origin,

View File

@ -3,10 +3,6 @@ from traceback import print_exc
from myutils.proxy import getproxy from myutils.proxy import getproxy
class KnownException(Exception):
pass
class basehira: class basehira:
def init(self): def init(self):
pass pass
@ -45,53 +41,66 @@ class basehira:
word = word[:-1] word = word[:-1]
return start, word, end return start, word, end
def parseparse(self, text): def safeparse(self, text):
hira = []
try: try:
if self.needinit: if self.needinit:
self.init() self.init()
self.needinit = False self.needinit = False
try: return self.parse_multilines(text)
hira = self.parse(text)
except Exception as e:
self.needinit = True
raise e
__parsekonge = []
for word in hira:
ori = word["orig"]
start, w, end = self.splitspace(ori)
if len(start) == 0 and len(end) == 0:
__parsekonge.append(word)
continue
word["orig"] = w
word["hira"] = self.splitspace(word["hira"])[1]
if len(start):
__parsekonge.append({"orig": start, "hira": start})
__parsekonge.append(word)
if len(end):
__parsekonge.append({"orig": end, "hira": end})
hira = __parsekonge
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 KnownException:
pass
except: except:
print_exc() print_exc()
self.needinit = True
return []
def parse_multilines(self, text):
hira = []
for i, _ in enumerate(text.split("\n")):
h = self.parse_singleline(_)
if "".join(__["orig"] for __ in h) != _:
raise Exception("not match")
if i:
hira += [{"orig": "\n", "hira": "\n"}]
hira += h
return hira
def parse_singleline(self, text):
hira = self.parse(text)
__parsekonge = []
for word in hira:
ori = word["orig"]
start, w, end = self.splitspace(ori)
if len(start) == 0 and len(end) == 0:
__parsekonge.append(word)
continue
word["orig"] = w
word["hira"] = self.splitspace(word["hira"])[1]
if len(start):
__parsekonge.append({"orig": start, "hira": start})
__parsekonge.append(word)
if len(end):
__parsekonge.append({"orig": end, "hira": end})
hira = __parsekonge
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]
)
return hira return hira

View File

@ -2,7 +2,7 @@ import winsharedutils
import os, functools, csv, gobject import os, functools, csv, gobject
from ctypes import CFUNCTYPE, c_char_p from ctypes import CFUNCTYPE, c_char_p
from hiraparse.basehira import basehira, KnownException from hiraparse.basehira import basehira
# # 2.1.2 src schema # # 2.1.2 src schema
# UnidicFeatures17 = namedtuple('UnidicFeatures17', # UnidicFeatures17 = namedtuple('UnidicFeatures17',
@ -64,7 +64,7 @@ class mecabwrap:
fp = CFUNCTYPE(None, c_char_p, c_char_p)(cb) fp = CFUNCTYPE(None, c_char_p, c_char_p)(cb)
succ = winsharedutils.mecab_parse(self.kks, text.encode(codec), fp) succ = winsharedutils.mecab_parse(self.kks, text.encode(codec), fp)
if not succ: if not succ:
raise KnownException # failed raise Exception("mecab parse failed")
return res return res
@ -72,10 +72,9 @@ class mecabwrap:
class mecab(basehira): class mecab(basehira):
def init(self) -> None: def init(self) -> None:
mecabpath = self.config["path"] mecabpath = self.config["path"]
if os.path.exists(mecabpath): if not os.path.exists(mecabpath):
self.kks = mecabwrap( raise Exception("no exits " + mecabpath)
mecabpath self.kks = mecabwrap(mecabpath)
) # fugashi.Tagger('-r nul -d "{}" -Owakati'.format(mecabpath))
def parse(self, text): def parse(self, text):
start = 0 start = 0