friends: fill example sentences for words/phrases/expressions from corpus; expr column -> translation/usage; drop function/category chips
This commit is contained in:
@@ -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: `<thead><tr><th>id</th><th>条目</th><th>等级</th><th>功能</th><th>上下文</th><th>状态</th></tr></thead><tbody>`,
|
||||
row: i => `<tr><td class="mono idc">${esc(i.id)}</td><td><b>${esc(i.item)}</b></td><td>${esc(i.level)}</td><td>${esc(i.function)}</td><td class="muted">${esc(i.context)}</td><td>${badge(i.status)}</td></tr>`,
|
||||
groups: [{ key: "level", label: "等级" }, { key: "status", label: "状态" }],
|
||||
fields: ["item", "meaning", "context"],
|
||||
head: `<thead><tr><th>id</th><th>条目</th><th>等级</th><th>翻译 / 用法</th><th>例句</th><th>状态</th></tr></thead><tbody>`,
|
||||
row: i => `<tr><td class="mono idc">${esc(i.id)}</td><td><b>${esc(i.item)}</b></td><td>${esc(i.level)}</td><td>${esc(i.meaning)}</td><td class="muted">${esc(i.context)}</td><td>${badge(i.status)}</td></tr>`,
|
||||
});
|
||||
|
||||
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: `<thead><tr><th>id</th><th>类别</th><th>问题</th><th>正确规则</th><th>例子</th><th>状态</th><th>次数</th></tr></thead><tbody>`,
|
||||
row: g => `<tr><td class="mono idc">${esc(g.id)}</td><td>${esc(g.category)}</td><td>${esc(g.issue)}</td><td class="muted">${esc(g.rule)}</td><td class="fx mono">${esc(g.examples)}</td><td>${badge(g.status)}</td><td>${g.count}</td></tr>`,
|
||||
|
||||
@@ -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"(?<![a-z])" + re.escape(item).replace(r"\ ", r"\s+") + r"(?![a-z])")
|
||||
for i in post.get(pivot, [])[:2000]:
|
||||
if pat.search(low[i]):
|
||||
hit = i
|
||||
break
|
||||
if hit is not None:
|
||||
conn.execute("UPDATE items SET context=? WHERE id=?", (sents[hit], r["id"]))
|
||||
n += 1
|
||||
conn.commit()
|
||||
filled = conn.execute("SELECT COUNT(*) FROM items WHERE context IS NOT NULL AND context<>''").fetchone()[0]
|
||||
conn.close()
|
||||
print(f"回填 {n} 条;有例句的 items 共 {filled}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -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,
|
||||
|
||||
BIN
Binary file not shown.
Reference in New Issue
Block a user