Files
EnglishNotes/英语词汇/情景单词记忆/generate_full_vocab.py
T
2026-10-04 11:13:54 +08:00

79 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""
批量生成《词汇全表》分组文件:从 kao_yan_3.db 导出全部词汇,
每 100 词一组,输出到 英语词汇/词汇全表/ 目录下,文件名为 第01组.md / 第02组.md ...
(阿拉伯数字零填充命名,保证 Obsidian 等软件按数值排序)
每组格式参照第一组词汇总表,仅保留:序号 | 单词 | 词性 | 例句。
用法:python generate_full_vocab.py [--db 路径] [--out-dir 输出目录]
"""
import argparse
import os
import sqlite3
def main():
base = os.path.dirname(os.path.abspath(__file__))
ap = argparse.ArgumentParser()
ap.add_argument("--db", default=os.path.join(base, "kao_yan_3.db"))
ap.add_argument("--out-dir", default=os.path.normpath(os.path.join(base, "..", "词汇全表")))
args = ap.parse_args()
conn = sqlite3.connect(args.db)
cur = conn.cursor()
# 词性:一词多词性去重,按 sort 顺序合并,如 "n./v."
pos_map = {}
for wid, pos in cur.execute(
"SELECT word_id, pos FROM word_trans WHERE pos IS NOT NULL AND pos != '' ORDER BY sort, id"
):
if pos not in pos_map.setdefault(wid, []):
pos_map[wid].append(pos)
# 例句:取每词第一条(sort 最小)
sent_map = {}
for wid, s in cur.execute(
"SELECT word_id, s_content FROM word_sentences "
"WHERE s_content IS NOT NULL AND s_content != '' ORDER BY sort, id"
):
if wid not in sent_map:
sent_map[wid] = s
rows = cur.execute("SELECT word_id, word_rank, head_word FROM words ORDER BY word_rank").fetchall()
conn.close()
def esc(s):
return (s or "").replace("|", "\\|").replace("\r", " ").replace("\n", " ").strip()
os.makedirs(args.out_dir, exist_ok=True)
group_size = 100
groups = [rows[i:i + group_size] for i in range(0, len(rows), group_size)]
no_sent = sum(1 for wid, _, _ in rows if wid not in sent_map)
for gi, group in enumerate(groups, 1):
start, end = group[0][1], group[-1][1]
title = f"# 第{gi:02d}组词汇(序号 {start}–{end},共 {len(group)} 词)"
lines = [
title,
"",
"> 由脚本 generate_full_vocab.py 从 kao_yan_3.db 批量生成,仅含 序号 / 单词 / 词性 / 例句 四列。",
"> 例句为该词在词库中的首条例句;词库无例句的词标注(无例句)。",
"",
"| 序号 | 单词 | 词性 | 例句 |",
"| --- | --- | --- | --- |",
]
for wid, rank, head in group:
pos = "/".join(pos_map.get(wid, [])) or "—"
sent = esc(sent_map.get(wid)) or "(无例句)"
lines.append(f"| {rank} | {esc(head)} | {pos} | {sent} |")
out = os.path.join(args.out_dir, f"第{gi:02d}组.md")
with open(out, "w", encoding="utf-8") as f:
f.write("\n".join(lines) + "\n")
print(f"已生成 {out}({len(group)} 词)")
print(f"\n共 {len(groups)} 组,总词数 {len(rows)};有例句 {len(rows) - no_sent},无例句 {no_sent}")
if __name__ == "__main__":
main()