diff --git a/actions/build_dashboard.py b/actions/build_dashboard.py index 5fb8903..0f590ae 100644 --- a/actions/build_dashboard.py +++ b/actions/build_dashboard.py @@ -417,16 +417,16 @@ function panel(cfg) { panel({ bar: "exprbar", tbl: "exprtbl", pg: "exprpg", rows: DATA.items.filter(i => i.kind === "expression"), - groups: [{ key: "function", label: "功能" }, { key: "level", label: "等级" }, { key: "status", label: "状态" }], - fields: ["item", "context", "function"], - head: `id条目等级功能上下文状态`, - row: i => `${esc(i.id)}${esc(i.item)}${esc(i.level)}${esc(i.function)}${esc(i.context)}${badge(i.status)}`, + groups: [{ key: "level", label: "等级" }, { key: "status", label: "状态" }], + fields: ["item", "meaning", "context"], + head: `id条目等级翻译 / 用法例句状态`, + row: i => `${esc(i.id)}${esc(i.item)}${esc(i.level)}${esc(i.meaning)}${esc(i.context)}${badge(i.status)}`, }); panel({ bar: "grambar", tbl: "gramtbl", pg: "grampg", rows: DATA.grammar, - groups: [{ key: "category", label: "类别" }, { key: "status", label: "状态" }], + groups: [{ key: "status", label: "状态" }], fields: ["issue", "rule", "examples"], head: `id类别问题正确规则例子状态次数`, row: g => `${esc(g.id)}${esc(g.category)}${esc(g.issue)}${esc(g.rule)}${esc(g.examples)}${badge(g.status)}${g.count}`, diff --git a/actions/fill_context.py b/actions/fill_context.py new file mode 100644 index 0000000..463710c --- /dev/null +++ b/actions/fill_context.py @@ -0,0 +1,102 @@ +#!/usr/bin/env python3 +"""用 Friends 全季剧本给 items 回填上下文例句(context)。 + +- 单词:按词元索引直接命中。 +- 词组/表达:按最稀有的词定位候选句,再整串匹配。 +- 只填空 context 的项(可重复运行)。 + +用法: + python actions/fill_context.py +""" +from __future__ import annotations + +import glob +import html +import re +import sqlite3 +import sys +from collections import defaultdict +from pathlib import Path + +ROOT = Path(__file__).resolve().parent.parent +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import langdb # noqa: E402 + +CORPUS = ROOT / "actions" / "raw" / "friends" +SPK = re.compile(r"^\s*([A-Za-z][A-Za-z .'\-]{0,20}):\s*(.+)$") + + +def strip_tags(s: str) -> str: + s = re.sub(r"(?s)<[^>]+>", " ", s) + return html.unescape(s).replace("\u2019", "'").replace("\u2018", "'") + + +def load_sentences() -> list[str]: + out = [] + for f in glob.glob(str(CORPUS / "season" / "*.html")): + text = strip_tags(Path(f).read_text(encoding="latin-1")) # 先去标签再切行 + for line in re.split(r"[\n\r]+", text): + m = SPK.match(line.strip()) + if not m: + continue + d = m.group(2) + d = re.sub(r"\[[^\]]*\]", " ", d) + d = re.sub(r"\([^)]*\)", " ", d) + d = re.sub(r"\s+", " ", d).strip() + if 4 <= len(d) <= 240: + out.append(d) + return out + + +def main() -> int: + for st in (sys.stdout, sys.stderr): + try: + st.reconfigure(encoding="utf-8") + except (AttributeError, ValueError): + pass + + sents = load_sentences() + low = [s.lower() for s in sents] + post: dict[str, list[int]] = defaultdict(list) + for i, s in enumerate(low): + for w in set(re.findall(r"[a-z]+(?:'[a-z]+)?", s)): + post[w].append(i) + print(f"例句库: {len(sents)} 句;词索引: {len(post)}") + + conn = langdb.connect() + rows = conn.execute( + "SELECT id, kind, item FROM items WHERE context IS NULL OR context='' " + "OR context LIKE 'Friends 全季%'").fetchall() + n = 0 + for r in rows: + item = (r["item"] or "").strip().lower() + if not item: + continue + toks = re.findall(r"[a-z]+(?:'[a-z]+)?", item) + if not toks: + continue + hit = None + if len(toks) == 1: + lst = post.get(toks[0]) + if lst: + hit = lst[0] + else: + # 选最稀有的词定位候选 + pivot = min(toks, key=lambda w: len(post.get(w, ()))) + pat = re.compile(r"(?''").fetchone()[0] + conn.close() + print(f"回填 {n} 条;有例句的 items 共 {filled}") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/actions/import_friends.py b/actions/import_friends.py index c585e5c..e2aa7da 100644 --- a/actions/import_friends.py +++ b/actions/import_friends.py @@ -512,10 +512,8 @@ def mine_phrasal(utts: list[str], names: set[str], pmi_min: float = 7.0): for i in range(len(t) - 2): if t[i] in COREV and t[i+1] in PART and t[i+2] in PREP: c3[f"{t[i]} {t[i+1]} {t[i+2]}"] += 1 - if t[i] in COREV and t[i+1] in PRO and t[i+2] in PART: - cp[f"{t[i]} {t[i+1]} {t[i+2]}"] += 1 out = {} - for g, n in list(c2.items()) + list(c3.items()) + list(cp.items()): + for g, n in list(c2.items()) + list(c3.items()): th = 4 if g.count(" ") == 1 else 3 if n >= th and not any(w in names for w in g.split()): out[g] = max(n, out.get(g, 0)) @@ -558,6 +556,15 @@ def main() -> int: rows.append({"phrase": g, "fn": "短语动词", "count": n}) rows.sort(key=lambda r: (-r["count"], r["phrase"])) + # 中文译文(人工维护) + zh = {} + zhf = ROOT / "actions" / "raw" / "friends_expr_zh.tsv" + if zhf.exists(): + for ln in zhf.read_text(encoding="utf-8").splitlines(): + if "\t" in ln: + a, b = ln.split("\t", 1) + zh[a.strip().lower()] = b.strip() + # 用 word 表推 level(取短语里最难的词) conn = langdb.connect() wl = {r["item"]: r["level"] for r in conn.execute( @@ -574,7 +581,7 @@ def main() -> int: lv = wl[w] items.append({ "id": "e-" + next(nums), "kind": "expression", "item": r["phrase"], - "level": lv, "meaning": "", "context": f"Friends 全季 ×{r['count']}", + "level": lv, "meaning": zh.get(r["phrase"], ""), "context": "", "status": "new", "first": "", "fix": "", "function": r["fn"], "tags": "口语," + r["fn"], "source": "friends:fangj", "added": today, "hits": 0, "streak": 0, "next_review": None, diff --git a/me/lang.db b/me/lang.db index 591d6ea..4563502 100644 Binary files a/me/lang.db and b/me/lang.db differ