first commit
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
从词汇全表分组 md 中提取 Obsidian 高亮(==...==)词汇,
|
||||
从 kao_yan_3.db 查询音标/词性释义/例句/短语/近义词,生成"高亮单词解析"
|
||||
并追加到该 md 文件末尾。支持一次处理多个文件,幂等(已生成则先移除旧章节)。
|
||||
|
||||
用法:
|
||||
python highlight_words_analysis.py [--db 路径] [--file 文件1 文件2 ...]
|
||||
不带 --file 时自动处理 词汇全表 目录下所有含 ==高亮== 的 md。
|
||||
"""
|
||||
import argparse
|
||||
import os
|
||||
import re
|
||||
import sqlite3
|
||||
|
||||
HEADER = "## 高亮单词解析"
|
||||
|
||||
|
||||
def esc(s):
|
||||
return (s or "").replace("|", "\\|").replace("\r", " ").replace("\n", " ").strip()
|
||||
|
||||
|
||||
def parse_md(db_path, md_path):
|
||||
txt = open(md_path, encoding="utf-8").read()
|
||||
words = []
|
||||
for m in re.finditer(r"==(.+?)==", txt):
|
||||
w = m.group(1).strip()
|
||||
if w and w not in words:
|
||||
words.append(w)
|
||||
return txt, words
|
||||
|
||||
|
||||
def lookup(cur, word):
|
||||
row = cur.execute("SELECT word_id, ukphone, usphone FROM words WHERE head_word=?", (word,)).fetchone()
|
||||
if not row:
|
||||
return None
|
||||
wid, uk, us = row
|
||||
trans = cur.execute(
|
||||
"SELECT pos, tran_cn FROM word_trans WHERE word_id=? ORDER BY sort, id", (wid,)
|
||||
).fetchall()
|
||||
sent = cur.execute(
|
||||
"SELECT s_content, s_cn FROM word_sentences WHERE word_id=? ORDER BY sort, id LIMIT 1", (wid,)
|
||||
).fetchone()
|
||||
phrases = cur.execute(
|
||||
"SELECT p_content, p_cn FROM word_phrases WHERE word_id=? ORDER BY sort, id LIMIT 3", (wid,)
|
||||
).fetchall()
|
||||
synos = cur.execute(
|
||||
"SELECT hwd FROM word_synos WHERE word_id=? ORDER BY sort, id LIMIT 3", (wid,)
|
||||
).fetchall()
|
||||
return {"phone": uk or us, "trans": trans, "sent": sent, "phrases": phrases, "synos": synos}
|
||||
|
||||
|
||||
def build_block(word, data):
|
||||
phone = f" /{esc(data['phone'])}/" if data["phone"] else ""
|
||||
lines = [f"### {esc(word)}{phone}", ""]
|
||||
pos_parts = [f"{p}. {esc(t)}" for p, t in data["trans"] if p or t]
|
||||
if pos_parts:
|
||||
lines.append(f"**词性**:{';'.join(pos_parts)}")
|
||||
else:
|
||||
lines.append("**词性**:—")
|
||||
if data["sent"]:
|
||||
s, cn = data["sent"]
|
||||
lines.append(f"**例句**:{esc(s)}({esc(cn)})")
|
||||
if data["phrases"]:
|
||||
lines.append("**短语**:" + ";".join(f"{esc(p)} {esc(c)}" for p, c in data["phrases"]))
|
||||
if data["synos"]:
|
||||
lines.append("**近义**:" + ",".join(esc(s[0]) for s in data["synos"]))
|
||||
lines.append("")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def process(db_path, md_path):
|
||||
txt = open(md_path, encoding="utf-8").read()
|
||||
# 先移除旧章节(幂等),再提取高亮,避免旧章节说明文字被误提取
|
||||
pat = re.compile(r"\n---\n\n" + re.escape(HEADER) + r".*$", re.S)
|
||||
clean = pat.sub("", txt).rstrip() + "\n"
|
||||
words = []
|
||||
for m in re.finditer(r"==(.+?)==", clean):
|
||||
w = m.group(1).strip()
|
||||
if w and w not in words:
|
||||
words.append(w)
|
||||
if not words:
|
||||
print(f"跳过(无高亮):{md_path}")
|
||||
return 0
|
||||
conn = sqlite3.connect(db_path)
|
||||
cur = conn.cursor()
|
||||
blocks = []
|
||||
missing = []
|
||||
for w in words:
|
||||
data = lookup(cur, w)
|
||||
if data is None:
|
||||
missing.append(w)
|
||||
continue
|
||||
blocks.append(build_block(w, data))
|
||||
conn.close()
|
||||
|
||||
new_sec = "\n---\n\n" + HEADER + f"({len(blocks)} 词)\n\n"
|
||||
new_sec += "> 由脚本 highlight_words_analysis.py 自动提取文中高亮标记(==…==)词汇生成,数据来自 kao_yan_3.db(音标/词性/释义/例句/短语/近义词)。\n\n"
|
||||
new_sec += "\n".join(blocks)
|
||||
with open(md_path, "w", encoding="utf-8") as f:
|
||||
f.write(clean + "\n" + new_sec + "\n")
|
||||
print(f"已处理:{md_path}(高亮 {len(words)} 词,生成解析 {len(blocks)} 词,未找到 {missing})")
|
||||
return len(blocks)
|
||||
|
||||
|
||||
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("--file", nargs="*", default=None)
|
||||
args = ap.parse_args()
|
||||
|
||||
if args.file:
|
||||
files = args.file
|
||||
else:
|
||||
d = os.path.normpath(os.path.join(base, "..", "词汇全表"))
|
||||
files = [os.path.join(d, f) for f in sorted(os.listdir(d)) if f.endswith(".md")]
|
||||
total = 0
|
||||
for f in files:
|
||||
total += process(args.db, f)
|
||||
print(f"\n共生成解析 {total} 词。")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user