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:
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(
self, text, is_auto_run=True, embedcallback=None, onlytrans=False
):
@ -1250,7 +1259,7 @@ class MAINUI:
if bool(param):
self.translation_ui.settop()
else:
if not globalconfig['keepontop']:
if not globalconfig["keepontop"]:
self.translation_ui.canceltop()
elif msg == 2:
self.translation_ui.closesignal.emit()

View File

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

View File

@ -366,21 +366,6 @@ class QUnFrameWindow(resizableframeless):
newlines.append(line)
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):
clear = kwargs.get("clear", True)
origin = kwargs.get("origin", True)
@ -413,7 +398,7 @@ class QUnFrameWindow(resizableframeless):
)
needhira = isshow_fenci or isshowhira or isfenciclick
if needhira:
hira = self.parsehira(text)
hira = gobject.baseobject.parsehira(text)
self.translate_text.append(
origin,

View File

@ -3,10 +3,6 @@ from traceback import print_exc
from myutils.proxy import getproxy
class KnownException(Exception):
pass
class basehira:
def init(self):
pass
@ -45,53 +41,66 @@ class basehira:
word = word[:-1]
return start, word, end
def parseparse(self, text):
hira = []
def safeparse(self, text):
try:
if self.needinit:
self.init()
self.needinit = False
try:
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
return self.parse_multilines(text)
except:
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

View File

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