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

如何修改人物族谱TXT生成器以输出正确的亲属关系格式?

问题分析与解决方案

问题根源

  1. 关系存储与导出逻辑不匹配:你存储亲属关系的格式是"${selectedRelationship} of ${selectedPerson.name}"(比如Brother of Elliot),但导出代码里却试图通过查找括号(来提取关系,完全不符合存储格式,导致关系提取失败。
  2. 葡萄牙语复合关系适配缺失:葡萄牙语亲属关系用de替代英语的of,现有逻辑未适配该语言特性,无法正确拆分关系字符串。

修复步骤

1. 修复导出函数的关系提取逻辑

修改generateFileBtn的点击事件处理代码,针对存储的关系格式([关系] de/of [人物])提取关系,同时兼容英语和葡萄牙语:

generateFileBtn.addEventListener('click', function () {
    const selectCharacter = document.getElementById('selectCharacter');
    const selectedCharacter = selectCharacter.value;

    let data;
    // 支持英语of和葡萄牙语de两种分割符
    const separators = [' of ', ' de '];
    
    if (selectedCharacter) {
        data = people
            .filter(p => p.relationship.includes(selectedCharacter))
            .map(p => {
                let relationship = '';
                // 遍历分割符,找到匹配的拆分方式
                for (const sep of separators) {
                    if (p.relationship.includes(sep)) {
                        relationship = p.relationship.split(sep)[0].trim();
                        break;
                    }
                }
                return `${p.name} (${relationship})`;
            })
            .join('\n');
    } else {
        data = people
            .map(p => {
                let relationship = '';
                for (const sep of separators) {
                    if (p.relationship.includes(sep)) {
                        relationship = p.relationship.split(sep)[0].trim();
                        break;
                    }
                }
                return `${p.name} (${relationship})`;
            })
            .join('\n');
    }

    const blob = new Blob([data], { type: 'text/plain' });
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = 'genealogical_tree.txt';
    document.body.appendChild(a);
    a.click();
    URL.revokeObjectURL(url);
    document.body.removeChild(a);
});

2. 优化关系存储(可选但推荐)

当前关系是单向存储(比如仅记录Jonh是Brother of Elliot,但Elliot的关系字段为空),建议改为双向关系存储,让族谱逻辑更完整:

修改人物数据结构:

function addPerson(name) {
    const person = { 
        name, 
        relatives: [] // 存储格式:[{ relativeName: 'xxx', relationship: 'Brother' }]
    };
    people.push(person);
    updateTable();
    updateSelectRelative();
    saveLocally();
}

修改保存关系的逻辑,同时添加双向关系:

document.getElementById('saveRelationship').addEventListener('click', function () {
    const relativeSelect = document.getElementById('relativeSelect');
    const relationshipSelect = document.getElementById('relationshipSelect');
    const selectedRelative = relativeSelect.value;
    const selectedRelationship = relationshipSelect.value;

    const relativePerson = people.find(p => p.name === selectedRelative);
    if (relativePerson && selectedPerson) {
        // 给选中人物添加正向关系
        selectedPerson.relatives.push({
            relativeName: selectedRelative,
            relationship: selectedRelationship
        });
        // 给亲属添加反向关系,可根据葡萄牙语亲属关系扩展映射
        const reverseRelationship = getReverseRelationship(selectedRelationship);
        relativePerson.relatives.push({
            relativeName: selectedPerson.name,
            relationship: reverseRelationship
        });
        updateTable();
        saveLocally();
    }

    relationshipModal.hide();
});

// 关系反向映射示例,可补充葡萄牙语关系
function getReverseRelationship(rel) {
    const reverseMap = {
        'Father': 'Son/Daughter',
        'Mother': 'Son/Daughter',
        'Brother': 'Brother',
        'Grandfather': 'Grandson/Granddaughter',
        // 葡萄牙语示例
        'Pai': 'Filho/Filha',
        'Mãe': 'Filho/Filha',
        'Irmão': 'Irmão',
        'Avô': 'Neto/Neta'
    };
    return reverseMap[rel] || rel;
}

3. 适配葡萄牙语关系选项

确保relationshipSelect下拉框包含葡萄牙语亲属词汇,比如Pai(父亲)、Mãe(母亲)、Irmão(兄弟)等,让关系存储更准确。

效果验证

修复后,选中Elliot导出时会正确输出预期格式:

Jonh (Brother)
Kyle (Grandfather)
Joe (Father)

内容的提问来源于stack exchange,提问作者Tori Calasans

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 00:36:01