|
| 1 | +"""Lightweight pinyin + fuzzy matching (no heavy dependencies). |
| 2 | +
|
| 3 | +A small built-in CJK->pinyin map covers common characters; unknown characters |
| 4 | +fall back to themselves. This powers pinyin / pinyin-initial recall and a |
| 5 | +bounded edit-distance fuzzy fallback for typo tolerance. |
| 6 | +""" |
| 7 | +from typing import List |
| 8 | + |
| 9 | +# Compact map of common Chinese characters to pinyin (extend as needed). |
| 10 | +_PINYIN = { |
| 11 | + "北": "bei", "京": "jing", "上": "shang", "海": "hai", "广": "guang", |
| 12 | + "州": "zhou", "深": "shen", "圳": "zhen", "中": "zhong", "国": "guo", |
| 13 | + "学": "xue", "习": "xi", "笔": "bi", "记": "ji", "工": "gong", |
| 14 | + "作": "zuo", "生": "sheng", "活": "huo", "项": "xiang", "目": "mu", |
| 15 | + "想": "xiang", "法": "fa", "今": "jin", "天": "tian", "明": "ming", |
| 16 | + "编": "bian", "程": "cheng", "教": "jiao", "搜": "sou", "索": "suo", |
| 17 | + "任": "ren", "务": "wu", "文": "wen", "件": "jian", "数": "shu", |
| 18 | + "据": "ju", "时": "shi", "间": "jian", "标": "biao", "题": "ti", |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +def to_pinyin(text: str) -> str: |
| 23 | + """Full pinyin of text (known chars mapped, others kept).""" |
| 24 | + out = [_PINYIN.get(ch, ch if ch.isascii() else "") for ch in text] |
| 25 | + return "".join(out).lower() |
| 26 | + |
| 27 | + |
| 28 | +def to_initials(text: str) -> str: |
| 29 | + """First letters of each character's pinyin (e.g. 北京 -> bj).""" |
| 30 | + out = [] |
| 31 | + for ch in text: |
| 32 | + py = _PINYIN.get(ch) |
| 33 | + if py: |
| 34 | + out.append(py[0]) |
| 35 | + elif ch.isascii() and ch.isalnum(): |
| 36 | + out.append(ch.lower()) |
| 37 | + return "".join(out) |
| 38 | + |
| 39 | + |
| 40 | +def matches_pinyin(query: str, text: str) -> bool: |
| 41 | + """True if query matches text's full pinyin or pinyin initials.""" |
| 42 | + q = query.lower() |
| 43 | + return bool(q) and (q in to_pinyin(text) or q in to_initials(text)) |
| 44 | + |
| 45 | + |
| 46 | +def edit_distance(a: str, b: str) -> int: |
| 47 | + """Levenshtein distance (iterative DP).""" |
| 48 | + a, b = a.lower(), b.lower() |
| 49 | + if a == b: |
| 50 | + return 0 |
| 51 | + prev = list(range(len(b) + 1)) |
| 52 | + for i, ca in enumerate(a, 1): |
| 53 | + cur = [i] |
| 54 | + for j, cb in enumerate(b, 1): |
| 55 | + cur.append(min(prev[j] + 1, cur[j - 1] + 1, prev[j - 1] + (ca != cb))) |
| 56 | + prev = cur |
| 57 | + return prev[-1] |
| 58 | + |
| 59 | + |
| 60 | +def fuzzy_match(query: str, term: str, max_distance: int = 1) -> bool: |
| 61 | + """True if query is within max_distance edits of term (or a prefix region).""" |
| 62 | + if not query: |
| 63 | + return False |
| 64 | + if query in term: |
| 65 | + return True |
| 66 | + return edit_distance(query, term) <= max_distance |
| 67 | + |
| 68 | + |
| 69 | +def suggest(prefix: str, terms: List[str], limit: int = 5) -> List[str]: |
| 70 | + """Prefix suggestions across literal terms and their pinyin/initials.""" |
| 71 | + p = prefix.lower() |
| 72 | + hits = [t for t in terms |
| 73 | + if t.lower().startswith(p) or to_pinyin(t).startswith(p) or to_initials(t).startswith(p)] |
| 74 | + return hits[:limit] |
0 commit comments