From 37894cc386bc923ac53760f8abd6f318ec445436 Mon Sep 17 00:00:00 2001 From: Urban Wallasch Date: Mon, 14 Jun 2021 19:06:40 +0200 Subject: [PATCH] * Improved responsiveness by only trying to match relevant parts during dictionary lookup and (more important) in the highlighter. --- jiten-pai.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/jiten-pai.py b/jiten-pai.py index 504669e..09ea29a 100755 --- a/jiten-pai.py +++ b/jiten-pai.py @@ -30,6 +30,7 @@ import os import re import argparse import unicodedata +import enum from configparser import RawConfigParser as ConfigParser from PyQt5.QtCore import * from PyQt5.QtWidgets import * @@ -37,7 +38,7 @@ from PyQt5.QtGui import * ############################################################ -# utility functions +# utility functions and classes def die(rc=0): sys.exit(rc) @@ -48,6 +49,10 @@ def contains_cjk(s): return True return False +class ScanMode(enum.Enum): + JAP = 1 + ENG = 2 + ############################################################ # configuration @@ -217,7 +222,8 @@ class jpMainWindow(QMainWindow): self.result_pane.setEnabled(False); QApplication.processEvents() # apply search options - if contains_cjk(term): + mode = ScanMode.JAP if contains_cjk(term) else ScanMode.ENG + if mode == ScanMode.JAP: if self.japopt_exact.isChecked(): if term[0] != '^': term = '^' + term @@ -246,17 +252,18 @@ class jpMainWindow(QMainWindow): # result limiting max_res = self.genopt_limit.value() if self.genopt_limit.isEnabled() else 0 # perform lookup - result = dict_lookup(cfg['dict'], term, max_res) + result = dict_lookup(cfg['dict'], term, mode, max_res) # format result re_term = re.compile(self.search_box.lineEdit().text(), re.IGNORECASE) nfmt = '
' % (cfg['font'], cfg['font_sz']) lfmt = '' % (cfg['lfont'], cfg['lfont_sz']) html = [nfmt] + mrange = [0, 1] if mode == ScanMode.JAP else [2] def hl_repl(match): return '%s' % (cfg['hl_col'], match.group(0)) for res in result: # highlight matches - for i in range(len(res)): + for i in mrange: res[i] = re_term.sub(hl_repl, res[i]) # construct display line html.append('%s%s' % (lfmt, res[0])) @@ -283,7 +290,7 @@ class jpMainWindow(QMainWindow): # 〆日 [しめび] /(n) time limit/closing day/settlement day (payment)/deadline/ # ハート /(n) heart/(P)/ -def dict_lookup(dict_fname, term, max_res = 0): +def dict_lookup(dict_fname, term, mode, max_res=0): result = [] cnt = 0; with open(dict_fname) as dict_file: @@ -304,10 +311,8 @@ def dict_lookup(dict_fname, term, max_res = 0): trans = ' ' + p2[1].lstrip('/ ').rstrip(' \t\r\n').replace('/', '; ') except: continue - # for now promiscuously try to match anything anywhere - if re_term.search(kanji) is not None \ - or re_term.search(kana) is not None \ - or re_term.search(trans) is not None: + if (mode == ScanMode.JAP and (re_term.search(kanji) or re_term.search(kana))) \ + or (mode == ScanMode.ENG and re_term.search(trans)): result.append([kanji, kana, trans]) cnt += 1 return result -- 2.30.2