wordlists: 5-digit ids w-<no-0>, higher=harder; keep 0s free for gap insertion
This commit is contained in:
+32
-28
@@ -1,14 +1,15 @@
|
||||
#!/usr/bin/env python3
|
||||
"""把 mahavivo/english-wordlists 的词汇导入 me/lang.db 的 items 表。
|
||||
|
||||
- 每个词取**最低等级认证**作为 level(如同时在 CET4 和 TOEFL → CET4)。
|
||||
- 全部认证写入 tags。
|
||||
- status = "new"(未学),next_review = NULL(不进入 SRS 到期)。
|
||||
- id 用词本身生成(w-<slug> / p-<slug>),可重复运行(INSERT OR REPLACE)。
|
||||
编号规则:
|
||||
- id = w-<5位数字> / p-<5位数字>;**号越大词越难**(按最低认证的等级排序)。
|
||||
- 当前只使用**不含数字 0** 的 5 位数;含 0 的号留给以后往中间插词。
|
||||
- level = 最低等级认证(如同时在 CET4 和 TOEFL → CET4);tags = 全部认证。
|
||||
- status = "new"(未学),next_review = NULL(不进 SRS 到期)。
|
||||
- 重复运行会先清掉旧的 `wordlist:` 条目再导入(幂等)。
|
||||
|
||||
用法:
|
||||
python actions/import_wordlists.py
|
||||
python actions/import_wordlists.py --dir <english-wordlists-master>
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -24,7 +25,7 @@ import langdb # noqa: E402
|
||||
|
||||
DEFAULT_DIR = ROOT / "actions" / "raw" / "english-wordlists-master"
|
||||
|
||||
# 从低到高;level 取第一个命中的
|
||||
# 从低到高;level 取第一个命中的,编号也按此顺序递增
|
||||
LISTS = [
|
||||
("小学", "小学英语大纲词汇.txt"),
|
||||
("中考", "中考英语词汇表.txt"),
|
||||
@@ -45,9 +46,7 @@ POS_SET = {"n", "v", "vt", "vi", "adj", "adv", "art", "prep", "conj",
|
||||
def parse_line(line: str):
|
||||
"""返回 (word, meaning) 或 None。取行首第一个词为词条。"""
|
||||
s = line.strip().lstrip("\ufeff").strip()
|
||||
if not s:
|
||||
return None
|
||||
if re.fullmatch(r"[A-Z]", s): # 章节标题 A/B/C…
|
||||
if not s or re.fullmatch(r"[A-Z]", s): # 空行 / 章节标题 A/B/C
|
||||
return None
|
||||
m = re.match(r"^([A-Za-z][A-Za-z\-'’\.]*)", s)
|
||||
if not m:
|
||||
@@ -59,13 +58,17 @@ def parse_line(line: str):
|
||||
rest = re.sub(r"^\s*\([^)]*\)", "", rest) # (an)
|
||||
rest = re.sub(r"^\s*\[[^\]]*\]", "", rest) # [phonetic]
|
||||
rest = re.sub(r"^(?:\s*" + POS + r"\.?)+", "", rest) # 前导 pos
|
||||
rest = re.sub(r"\s*(?:abbr|adj|adv|art|aux|conj|prep|pron|int|num|vt|vi|ad|n|v)\.\s*", " ", rest) # 句中 pos
|
||||
rest = re.sub(r"\s*(?:abbr|adj|adv|art|aux|conj|prep|pron|int|num|vt|vi|ad|n|v)\.\s*", " ", rest)
|
||||
rest = re.sub(r"\s{2,}", " ", rest).strip(" .·-—;;::")
|
||||
return word.lower(), rest[:120]
|
||||
|
||||
|
||||
def slug(word: str) -> str:
|
||||
return re.sub(r"[^a-z0-9]+", "-", word).strip("-") or "x"
|
||||
def no0_numbers():
|
||||
"""不含 0 的 5 位数,升序。"""
|
||||
for n in range(11111, 100000):
|
||||
s = str(n)
|
||||
if "0" not in s:
|
||||
yield s
|
||||
|
||||
|
||||
def build_payload(d: Path) -> tuple[list[dict], dict[str, int]]:
|
||||
@@ -73,9 +76,8 @@ def build_payload(d: Path) -> tuple[list[dict], dict[str, int]]:
|
||||
meaning: dict[str, str] = {}
|
||||
per = {}
|
||||
for name, fname in LISTS:
|
||||
p = d / fname
|
||||
ws = set()
|
||||
for ln in p.read_text(encoding="utf-8", errors="replace").splitlines():
|
||||
for ln in (d / fname).read_text(encoding="utf-8", errors="replace").splitlines():
|
||||
r = parse_line(ln)
|
||||
if not r:
|
||||
continue
|
||||
@@ -88,21 +90,21 @@ def build_payload(d: Path) -> tuple[list[dict], dict[str, int]]:
|
||||
meaning[w] = mg
|
||||
per[name] = len(ws)
|
||||
|
||||
# 按难度排序:先等级(低→高),同级按字母
|
||||
def rank(w):
|
||||
return ORDER[min(members[w], key=lambda c: ORDER[c])]
|
||||
ordered = sorted(members, key=lambda w: (rank(w), w))
|
||||
|
||||
today = _dt.date.today().isoformat()
|
||||
items, seen = [], set()
|
||||
for w, certs in members.items():
|
||||
certs = sorted(certs, key=lambda c: ORDER[c])
|
||||
level = certs[0]
|
||||
nums = no0_numbers()
|
||||
items = []
|
||||
for w in ordered:
|
||||
num = next(nums)
|
||||
certs = sorted(members[w], key=lambda c: ORDER[c])
|
||||
kind = "phrase" if " " in w else "word"
|
||||
base = ("p-" if kind == "phrase" else "w-") + slug(w)
|
||||
iid = base
|
||||
n = 2
|
||||
while iid in seen:
|
||||
iid = f"{base}-{n}"
|
||||
n += 1
|
||||
seen.add(iid)
|
||||
items.append({
|
||||
"id": iid, "kind": kind, "item": w, "level": level,
|
||||
"id": ("p-" if kind == "phrase" else "w-") + num,
|
||||
"kind": kind, "item": w, "level": certs[0],
|
||||
"meaning": meaning.get(w, ""), "context": "", "status": "new",
|
||||
"first": "", "fix": "", "function": "",
|
||||
"tags": ",".join(certs), "source": "wordlist:mahavivo",
|
||||
@@ -126,10 +128,12 @@ def main() -> int:
|
||||
sys.exit(f"目录不存在: {d}")
|
||||
items, per = build_payload(d)
|
||||
conn = langdb.connect()
|
||||
n = langdb.ingest(conn, {"items": items})
|
||||
removed = conn.execute("DELETE FROM items WHERE source LIKE 'wordlist:%'").rowcount
|
||||
n = langdb.ingest(conn, {"items": items}) # ingest 内部 commit
|
||||
conn.close()
|
||||
print("各表:", per)
|
||||
print("导入:", n, "(并集条目)")
|
||||
print(f"清除旧词表条目 {removed},导入 {n['items']}")
|
||||
print("id 范围:", items[0]["id"], "→", items[-1]["id"])
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
BIN
Binary file not shown.
Reference in New Issue
Block a user