
下面是一个 Python 版“亲戚称呼计算器”。核心思路先把输入拆成关系链例如“妈妈的姐姐的儿子”解析为 mother - elder_sister - son再查表得到称呼。python# -*- coding: utf-8 -*-# 口语/常见称呼 - 规范关系路径ALIASES {爸爸: (father,), 父亲: (father,), 爸: (father,), 爹: (father,),妈妈: (mother,), 母亲: (mother,), 妈: (mother,), 娘: (mother,),哥哥: (elder_brother,), 兄: (elder_brother,),弟弟: (younger_brother,), 弟: (younger_brother,),姐姐: (elder_sister,), 姐: (elder_sister,),妹妹: (younger_sister,), 妹: (younger_sister,),儿子: (son,), 女儿: (daughter,),丈夫: (husband,), 老公: (husband,), 先生: (husband,),妻子: (wife,), 老婆: (wife,), 太太: (wife,),# 可直接输入已有称呼爷爷: (father, father), 祖父: (father, father),奶奶: (father, mother), 祖母: (father, mother),外公: (mother, father), 外祖父: (mother, father),外婆: (mother, mother), 外祖母: (mother, mother),伯父: (father, elder_brother), 伯伯: (father, elder_brother),叔叔: (father, younger_brother), 叔父: (father, younger_brother),姑妈: (father, elder_sister), 姑姑: (father, younger_sister),姑母: (father, elder_sister),舅舅: (mother, elder_brother), 舅父: (mother, elder_brother),姨妈: (mother, elder_sister), 姨母: (mother, elder_sister),小姨: (mother, younger_sister),岳父: (wife, father), 岳母: (wife, mother),公公: (husband, father), 婆婆: (husband, mother),孙子: (son, son), 孙女: (son, daughter),外孙: (daughter, son), 外孙女: (daughter, daughter),侄子: (elder_brother, son), 侄女: (elder_brother, daughter),外甥: (elder_sister, son), 外甥女: (elder_sister, daughter),}# 规范关系路径 - 称呼PATH_TO_NAMES {(): (自己,),(father,): (爸爸, 父亲),(mother,): (妈妈, 母亲),(husband,): (丈夫, 老公),(wife,): (妻子, 老婆),(elder_brother,): (哥哥,),(younger_brother,): (弟弟,),(elder_sister,): (姐姐,),(younger_sister,): (妹妹,),(son,): (儿子,),(daughter,): (女儿,),# 祖辈(father, father): (爷爷, 祖父),(father, mother): (奶奶, 祖母),(mother, father): (外公, 外祖父),(mother, mother): (外婆, 外祖母),(father, father, father): (曾祖父, 老爷爷),(father, father, mother): (曾祖母, 老奶奶),(mother, father, father): (外曾祖父,),(mother, mother, mother): (外曾祖母,),(father, father, father, father): (高祖父,),(father, father, father, mother): (高祖母,),# 子孙辈(son, son): (孙子,),(son, daughter): (孙女,),(daughter, son): (外孙,),(daughter, daughter): (外孙女,),(son, son, son): (曾孙,),(son, son, daughter): (曾孙女,),(son, son, son, son): (玄孙,),(son, son, son, daughter): (玄孙女,),# 配偶父母(wife, father): (岳父,),(wife, mother): (岳母,),(husband, father): (公公,),(husband, mother): (婆婆,),# 父系/母系旁系长辈(father, elder_brother): (伯父, 伯伯),(father, younger_brother): (叔叔, 叔父),(father, elder_sister): (姑妈, 姑母),(father, younger_sister): (姑姑, 姑母),(mother, elder_brother): (舅舅, 舅父),(mother, younger_brother): (舅舅, 舅父),(mother, elder_sister): (姨妈, 姨母),(mother, younger_sister): (小姨, 姨母),# 祖辈旁系(father, father, elder_brother): (伯祖父, 大爷爷),(father, father, younger_brother): (叔祖父, 叔爷爷),(father, father, elder_sister): (姑祖母, 姑奶奶),(father, father, younger_sister): (姑祖母, 姑奶奶),(mother, father, elder_brother): (舅祖父, 舅爷爷),(mother, father, younger_brother): (舅祖父, 舅爷爷),(mother, mother, elder_brother): (姨祖父, 姨爷爷),(mother, mother, elder_sister): (姨祖母, 姨奶奶),# 堂表兄弟姐妹(father, elder_brother, son): (堂兄/堂弟, 堂兄弟),(father, younger_brother, son): (堂兄/堂弟, 堂兄弟),(father, elder_brother, daughter): (堂姐/堂妹, 堂姐妹),(father, younger_brother, daughter): (堂姐/堂妹, 堂姐妹),(father, elder_sister, son): (姑表兄弟, 表兄弟),(father, younger_sister, son): (姑表兄弟, 表兄弟),(father, elder_sister, daughter): (姑表姐妹, 表姐妹),(father, younger_sister, daughter): (姑表姐妹, 表姐妹),(mother, elder_brother, son): (舅表兄弟, 表兄弟),(mother, younger_brother, son): (舅表兄弟, 表兄弟),(mother, elder_brother, daughter): (舅表姐妹, 表姐妹),(mother, younger_brother, daughter): (舅表姐妹, 表姐妹),(mother, elder_sister, son): (姨表兄弟, 表兄弟),(mother, younger_sister, son): (姨表兄弟, 表兄弟),(mother, elder_sister, daughter): (姨表姐妹, 表姐妹),(mother, younger_sister, daughter): (姨表姐妹, 表姐妹),# 侄甥辈(elder_brother, son): (侄子,),(younger_brother, son): (侄子,),(elder_brother, daughter): (侄女,),(younger_brother, daughter): (侄女,),(elder_sister, son): (外甥,),(younger_sister, son): (外甥,),(elder_sister, daughter): (外甥女,),(younger_sister, daughter): (外甥女,),# 侄孙辈(elder_brother, son, son): (侄孙,),(younger_brother, son, son): (侄孙,),(elder_brother, son, daughter): (侄孙女,),(younger_brother, son, daughter): (侄孙女,),(elder_sister, son, son): (外甥孙,),(younger_sister, son, son): (外甥孙,),(elder_sister, son, daughter): (外甥孙女,),(younger_sister, son, daughter): (外甥孙女,),# 有歧义的关系按可能称呼列出(father, son): (自己/哥哥/弟弟,),(father, daughter): (自己/姐姐/妹妹,),(mother, son): (自己/哥哥/弟弟,),(mother, daughter): (自己/姐姐/妹妹,),(father, father, son): (爸爸/伯父/叔叔,),(father, father, daughter): (姑妈/姑姑,),(father, mother, son): (爸爸/伯父/叔叔,),(father, mother, daughter): (姑妈/姑姑,),(mother, father, son): (妈妈/舅舅/姨妈,),(mother, father, daughter): (妈妈/舅舅/姨妈,),(mother, mother, son): (妈妈/舅舅/姨妈,),(mother, mother, daughter): (妈妈/舅舅/姨妈,),}TOKEN_CN {father: 父亲,mother: 母亲,elder_brother: 哥哥,younger_brother: 弟弟,elder_sister: 姐姐,younger_sister: 妹妹,son: 儿子,daughter: 女儿,husband: 丈夫,wife: 妻子,}def normalize(text: str) - str:清理输入去掉‘我的’、‘我’、空格等。text text.strip().replace( , ).replace(, ).replace(,, )for w in (我的, 我, 自己, 本人):text text.replace(w, )return textdef parse_relation(text: str):把输入文字解析成规范关系路径例如妈妈的姐姐的儿子 - (mother,elder_sister,son)text normalize(text)if not text:return ()path ()for part in text.split(的):if not part:continueif part not in ALIASES:raise ValueError(f无法识别{part})path ALIASES[part]return pathdef calc_relation(text: str) - str:根据输入文字计算亲戚称呼。path parse_relation(text)names PATH_TO_NAMES.get(path)if names:return / .join(names)# 未收录时给出关系路径描述desc 的.join(TOKEN_CN.get(token, token) for token in path)return f未收录关系路径{desc}if __name__ __main__:examples [我的爸爸的爸爸,妈妈的姐姐的儿子,爸爸的哥哥的女儿,妻子的爸爸,丈夫的妈妈,哥哥的女儿,儿子的儿子,爸爸的爸爸的爸爸,]print(示例)for e in examples:print(f{e} - {calc_relation(e)})print(\n输入 q 退出)while True:s input(请输入关系)if s.lower() q:breaktry:print(calc_relation(s))except Exception as ex:print(错误, ex)运行示例text我的爸爸的爸爸 - 爷爷 / 祖父妈妈的姐姐的儿子 - 姨表兄弟 / 表兄弟爸爸的哥哥的女儿 - 堂姐/堂妹 / 堂姐妹妻子的爸爸 - 岳父丈夫的妈妈 - 婆婆哥哥的女儿 - 侄女儿子的儿子 - 孙子爸爸的爸爸的爸爸 - 曾祖父 / 老爷爷这个版本覆盖了常见九族、直系、旁系、堂表、侄甥、岳父母、公婆等关系。不同地区叫法差异较大如果需要特定地区称呼只需要继续往 ALIASES 和 PATH_TO_NAMES 里补充即可。文章仅供参考用