You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

家庭亲属关系判定算法扩展需求:支持四代五代堂亲

扩展亲属关系判定算法以支持四代、五代亲属关系

现有一款TypeScript基础版亲属关系判定算法,仅能识别父母、兄弟姐妹、子女、堂亲、同父异母/同母异父兄弟姐妹、叔伯姑舅等基础亲属关系,现需扩展该算法功能,使其支持判定四代、五代亲属关系(即四代堂亲、五代堂亲)。

现有算法实现代码

export interface Member {
  name: string
  father: string | null
  mother: string | null
}
const determinateDeepRelationship = (p1: string, p2: string, fam: Member[]) => {
  const getParents = (
    p?: Member['name'] | null,
    fam?: Member[]
  ): Member | null => {
    const found = fam?.find((member) => member.name === p)
    return found || null
  }
  const p1Parents = getParents(p1, fam)
  const p2Parents = getParents(p2, fam)

  // 判定是否为亲兄弟姐妹
  if (
    p1Parents?.father === p2Parents?.father &&
    p1Parents?.mother === p2Parents?.mother
  ) {
    return 'hermano/hermana'
  }
  // 判定是否为父母
  if (
    p2Parents?.mother === p1Parents?.name ||
    p2Parents?.father === p1Parents?.name
  ) {
    return 'padre/madre'
  }

  // 判定是否为子女
  if (p1Parents?.mother === p2 || p1Parents?.father === p2) {
    return 'hija/hijo'
  }

  // 判定是否为同父异母/同母异父兄弟姐妹
  if (
    p1Parents?.father === p2Parents?.father ||
    p1Parents?.mother === p2Parents?.mother
  ) {
    return 'stepBrother'
  }

  // 获取祖父母信息
  const getGrandparents = (p: string): (string | null)[] => {
    const parents = getParents(p, fam)

    const grandparents = [
      getParents(parents?.father, fam)?.father || null,
      getParents(parents?.father, fam)?.mother || null,
      getParents(parents?.mother, fam)?.father || null,
      getParents(parents?.mother, fam)?.mother || null
    ]
    return grandparents
  }

  const p2Grandparents = getGrandparents(p2)
  const p1Grandparents = getGrandparents(p1)

  // 判定是否为堂亲
  for (let i = 0; i < p1Grandparents.length; i++) {
    for (let j = 0; j < p2Grandparents.length; j++) {
      if (
        (p2Grandparents[i] && p1Grandparents.includes(p2Grandparents[i])) ||
        (p1Grandparents[j] && p2Grandparents.includes(p1Grandparents[j]))
      )
        return 'primos/primas'
    }
  }

  // 判定是否为叔伯姑舅
  for (let j = 0; j < [p1Parents?.father, p1Parents?.mother].length; j++) {
    for (let i = 0; i < p2Grandparents.length; i++) {
      if (
        p1Parents?.father === p2Grandparents[i] ||
        p1Parents?.mother === p2Grandparents[i] ||
        p1Parents?.father === p2Grandparents[j] ||
        p1Parents?.mother === p2Grandparents[j]
      )
        return 'tia/tio'
    }
  }

  // 判定是否为侄甥
  for (let i = 0; i < p1Grandparents.length; i++) {
    for (let j = 0; j < [p2Parents?.father, p2Parents?.mother].length; j++) {
      if (
        p2Parents?.father === p1Grandparents[i] ||
        p2Parents?.mother === p1Grandparents[i] ||
        p2Parents?.father === p1Grandparents[j] ||
        p2Parents?.mother === p1Grandparents[j]
      )
        return 'sobrino/sobrina'
    }
  }

  return 'any'
}

export default determinateDeepRelationship

测试用家庭数据

const family = [
  { name: 'mat', father: 'mike', mother: null },
  { name: 'luz', father: null, mother: 'deb' },
  { name: 'sebas', father: 'mat', mother: 'luz' },
  { name: 'pilar', father: 'mat', mother: 'luz' },
  { name: 'juan', father: 'mat', mother: 'luz' },
  { name: 'lupe2', father: null, mother: null },
  { name: 'ana', father: 'sebas', mother: 'lupe' },
  { name: 'jose', father: 'sebas', mother: 'lupe' },
  { name: 'carlos', father: null, mother: null },
  { name: 'luis', father: 'carlos', mother: 'pilar' },
  { name: 'emma', father: 'carlos', mother: 'pilar' },
  { name: 'lulu', father: 'ed', mother: 'clau' },
  { name: 'lupe', father: 'juan', mother: 'lulu' },
  { name: 'pepe', father: 'juan', mother: 'lulu' },
  { name: 'tam', father: 'juan', mother: 'lulu' },
  { name: 'dan', father: 'carlos', mother: 'lulu' },
  { name: 'cam', father: 'carlos', mother: 'lulu' },
  { name: 'emi', father: 'jose', mother: 'lulu' },
  { name: 'max', father: 'ed', mother: 'ana' }
]

内容的提问来源于stack exchange,提问作者raul zarza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 07:15:35