From: Urban Wallasch Date: Tue, 15 Jun 2021 15:58:28 +0000 (+0200) Subject: Merge branch 'configdlg' X-Git-Tag: v0.1.0~136 X-Git-Url: https://git.packet-gain.de/?a=commitdiff_plain;h=e4631b052736fcd490894633f200187e1ec1174b;p=jiten-pai.git Merge branch 'configdlg' --- e4631b052736fcd490894633f200187e1ec1174b diff --cc jiten-pai.py index a501e2a,0c7a3f6..753ba7a --- a/jiten-pai.py +++ b/jiten-pai.py @@@ -436,17 -841,20 +841,26 @@@ class jpMainWindow(QMainWindow) elif self.engopt_word.isChecked(): term = '\W' + term + '\W' # result limiting - max_res = self.genopt_limit.value() if self.genopt_limit.isEnabled() else 0 + limit = self.genopt_limit.value() if self.genopt_limit.isEnabled() else 0 # perform lookup QApplication.processEvents() - result = dict_lookup(cfg['dict'], term, mode, max_res) - # + result = [] + if self.genopt_dict.isChecked(): + dic = self.genopt_dictsel.itemData(self.genopt_dictsel.currentIndex()) + result = dict_lookup(dic, term, mode, limit) + else: + for d in cfg['dicts']: + r = dict_lookup(d[1], term, mode, limit) + result.extend(r) + limit -= len(r) + if limit == 0: + limit = -1 + self.result_group.setTitle('Search results: %d' % len(result)) - self.result_pane.setHtml('') - self.result_pane.setEnabled(True); + # bail early on empty result + if 0 == len(result): ++ self.result_pane.setHtml('') ++ self.result_pane.setEnabled(True); + return # format result term = self.search_box.lineEdit().text() re_term = re.compile(kata2hira(term), re.IGNORECASE) @@@ -471,16 -879,9 +885,8 @@@ html.append(' %s
\n' % res[2]) html.append('') self.result_pane.setHtml(''.join(html)) -- self.result_group.setTitle('Search results: %d' % len(result)) self.result_pane.setEnabled(True) - def kbd_copy(self): - self.clipboard.setText(self.result_pane.textCursor().selectedText()) - - def kbd_paste(self): - self.search_box.lineEdit().setText(self.clipboard.text()) - self.search_box.setFocus() - ############################################################ # dictionary lookup @@@ -489,34 -890,34 +895,37 @@@ # 〆日 [しめび] /(n) time limit/closing day/settlement day (payment)/deadline/ # ハート /(n) heart/(P)/ - def dict_lookup(dict_fname, pattern, mode, max_res=0): + def dict_lookup(dict_fname, pattern, mode, limit=0): result = [] cnt = 0 - with open(dict_fname) as dict_file: - try: - re_pattern = re.compile(pattern, re.IGNORECASE) - except: - return result - for line in dict_file: - if max_res and cnt >= max_res: - break + try: + with open(dict_fname) as dict_file: - re_pattern = re.compile(pattern, re.IGNORECASE) + try: - # manually splitting the line is actually faster than regex - p1 = line.split('[', 1) - if len(p1) < 2: - p1 = line.split('/', 1) - p2 = ['', p1[1]] - else: - p2 = p1[1].split(']', 1) - term = p1[0].strip() - hira = p2[0].strip() - trans = ' ' + p2[1].lstrip('/ ').rstrip(' \t\r\n').replace('/', '; ') ++ re_pattern = re.compile(pattern, re.IGNORECASE) + except: - continue - if (mode == ScanMode.JAP and (re_pattern.search(kata2hira(term)) or re_pattern.search(hira))) \ - or (mode == ScanMode.ENG and re_pattern.search(trans)): - result.append([term, hira, trans]) - cnt += 1 ++ return result + for line in dict_file: + if limit and cnt >= limit: + break + try: + # manually splitting the line is actually faster than regex + p1 = line.split('[', 1) + if len(p1) < 2: + p1 = line.split('/', 1) + p2 = ['', p1[1]] + else: + p2 = p1[1].split(']', 1) + term = p1[0].strip() + hira = p2[0].strip() + trans = ' ' + p2[1].lstrip('/ ').rstrip(' \t\r\n').replace('/', '; ') + except: + continue + if (mode == ScanMode.JAP and (re_pattern.search(kata2hira(term)) or re_pattern.search(hira))) \ + or (mode == ScanMode.ENG and re_pattern.search(trans)): + result.append([term, hira, trans]) + cnt += 1 + except Exception as e: + eprint(dict_fname, str(e)) return result