* Micro-optimizations in dict_lookup().
authorUrban Wallasch <urban.wallasch@freenet.de>
Fri, 18 Jun 2021 11:37:17 +0000 (13:37 +0200)
committerUrban Wallasch <urban.wallasch@freenet.de>
Fri, 18 Jun 2021 11:37:17 +0000 (13:37 +0200)
* Report malformed dictionary lines (just in case, not that we expect to hit any).

jiten-pai.py

index 86ccd8a5694439d11dbd4aa99b563e21a46c0cc4..a6579cc3f05502e307fc5ac0e92ca435cdcb6387 100755 (executable)
@@ -1279,8 +1279,6 @@ def dict_lookup(dict_fname, pattern, mode, limit=0):
         with open(dict_fname) as dict_file:
             re_pattern = re.compile(pattern, re.IGNORECASE)
             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)
@@ -1293,11 +1291,14 @@ def dict_lookup(dict_fname, pattern, mode, limit=0):
                     hira = p2[0].strip()
                     trans = ' ' + p2[1].lstrip('/ ').rstrip(' \t\r\n').replace('/', '; ')
                 except:
+                    eprint('lookup:', line, ':', str(e))
                     continue
-                if (mode == ScanMode.JAP and (re_pattern.search(kata2hira(term)) or re_pattern.search(hira))) \
+                if (mode == ScanMode.JAP and (re_pattern.search(hira) or re_pattern.search(kata2hira(term)))) \
                 or (mode == ScanMode.ENG and re_pattern.search(trans)):
                     result.append([term, hira, trans])
                     cnt += 1
+                    if limit and cnt >= limit:
+                        break
     except Exception as e:
         eprint('lookup:', dict_fname, str(e))
     return result