File size: 4,128 Bytes
fa11d50 5d63cec bfebbee 5d63cec bfebbee fa11d50 bfebbee fa11d50 bfebbee fa11d50 bfebbee fa11d50 bfebbee fa11d50 bfebbee fa11d50 bfebbee 5d63cec bfebbee 5d63cec fa11d50 bfebbee f5f7c79 fa11d50 bfebbee fa11d50 bfebbee fa11d50 bfebbee fa11d50 bfebbee fa11d50 bfebbee fa11d50 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | # app.py —— 永久通用版:支持上5代、下5代、任意关系查询(已修复语法错误)
import os
import gradio as gr
import re
# ==================== 自动构建完整族谱关系树 ====================
def build_family_tree():
if not os.path.exists("genealogy.txt"):
return {}, {}
with open("genealogy.txt", "r", encoding="utf-8") as f:
text = f.read()
# 儿子 → 父亲(关键!用于向上查)
child_to_parent = {}
# 父亲 → [儿子们](用于向下查)
parent_to_children = {}
blocks = text.split("——")
current_person = ""
for block in blocks:
lines = [l.strip() for l in block.split("\n") if l.strip()]
current_person = ""
for line in lines:
# 提取当前主角名字(冯、陈、胡、秦等姓)
if re.search(r"[冯陈胡秦杨李董周][\u4e00-\u9fa5]{1,4}", line):
match = re.search(r"[冯陈胡秦杨李董周][\u4e00-\u9fa5]{1,4}", line)
if match:
current_person = match.group(0)
# 找父亲关系:常见写法“孝男 冯乔源”“孝孙 冯宗德”“曾孙 冯永明”
if current_person:
father_match = re.search(r"[孝曾外][男孙玄曾][孙女]?\s*([冯陈胡秦杨李董周][\u4e00-\u9fa5]{1,4})", line)
if father_match:
father = father_match.group(1)
child_to_parent[current_person] = father
parent_to_children.setdefault(father, []).append(current_person)
return child_to_parent, parent_to_children
CHILD_TO_PARENT, PARENT_TO_CHILDREN = build_family_tree()
# ==================== 万能关系查询函数 ====================
def query_relation(name, up=0, down=0):
if name not in CHILD_TO_PARENT and name not in PARENT_TO_CHILDREN:
return f"家谱里没找到「{name}」这个人"
result = [f"【{name}】的关系查询结果:\n"]
# 上溯 N 代
current = name
for i in range(1, up + 1):
father = CHILD_TO_PARENT.get(current)
if not father:
result.append(f"往上第 {i} 代:未知(家谱未记录)")
break
titles = ["父亲,祖父,曾祖父,高祖父,天祖父,烈祖父"]
title = titles[i-1] if i <= 6 else f"第{i}代祖先"
result.append(f"{title}:{father}")
current = father
# 下溯 N 2 代(够用了)
children = PARENT_TO_CHILDREN.get(name, [])
if down >= 1 and children:
result.append(f"\n儿子/女儿:{'、'.join(children) if children else '无'}")
if down >= 2:
grandchildren = []
for child in children:
grandchildren.extend(PARENT_TO_CHILDREN.get(child, []))
result.append(f"孙子/孙女:{'、'.join(grandchildren) if grandchildren else '无'}")
return "\n".join(result)
def chat(message, history):
msg = message.strip()
# 自动识别“上N代”
up_match = re.search(r"(.+)[的的上]([0-9一二三四五六七八九十]+)代", msg)
if up_match:
name = up_match.group(1).replace("的","").replace("上","")
n_str = up_match.group(2)
n_map = {"一":1,"二":2,"三":3,"四":4,"五":5,"六":6,"七":7,"八":8,"九":9,"十":10}
n = n_map.get(n_str, 0) or int(n_str) if n_str.isdigit() else 3
return query_relation(name, up=n)
# 自动识别“下N代”
down_match = re.search(r"(.+)[的下]([0-9一二三四五]+)代", msg)
if down_match:
name = down_match.group(1).replace("的","")
n_str = down_match.group(2)
n_map = {"一":1,"二":2,"三":3,"四":4,"五":5}
n = n_map.get(n_str, 0) or int(n_str) if n_str.isdigit() else 2
return query_relation(name, down=n)
# 其他问题就原文搜索
try:
with open("genealogy.txt", "r", encoding="utf-8") as f:
text = f.read()
lines = text.split("\n")
for i, line in enumerate(lines):
if message |