relationship()
计算中国亲戚关系的称谓。
语法
ts
relationship(parameter: string | RelationshipOptions): string[]参数
parameter
可以是字符串或 RelationshipOptions 对象。
字符串模式
自然语言表达式:
ts
relationship('爸爸的妈妈是谁?');
// => ['奶奶', '祖母']对象模式
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
text | string | '' | 目标对象的称谓汉字表达,称谓间用"的"字分隔 |
target | string | '' | 相对对象的称谓,空表示自己 |
sex | -1 | 0 | 1 | -1 | 本人性别:-1=未知,0=女性,1=男性 |
type | 'default' | 'chain' | 'pair' | 'default' | 转换类型 |
reverse | boolean | false | 称呼方式:true=对方称呼我,false=我称呼对方 |
mode | string | 'default' | 使用的方言模式名称 |
optimal | boolean | false | 是否计算两者之间的最短关系 |
返回值
返回一个字符串数组,包含所有匹配的称谓。
示例
基础查询
ts
relationship({ text: '爸爸的妈妈' });
// => ['奶奶', '祖母']
relationship({ text: '妈妈的妈妈的哥哥' });
// => ['舅外公']指定性别
ts
relationship({ text: '哥哥的老婆', sex: 1 });
// => ['嫂子', '嫂嫂', '阿嫂']反向查询
ts
relationship({ text: '外婆', reverse: true, sex: 1 });
// => ['外孙']
relationship({ text: '外婆', reverse: true, sex: 0 });
// => ['外孙女']关系链查询
ts
relationship({ text: '舅公', type: 'chain' });
// => ['爸爸的妈妈的兄弟', '妈妈的妈妈的兄弟', '老公的妈妈的兄弟']
relationship({ text: '表姐', type: 'chain' });
// => ['爸爸的姐妹的女儿&o', '妈妈的姐妹的女儿&o', ...]关系合称
ts
relationship({ text: '外婆', target: '奶奶', type: 'pair' });
// => ['儿女亲家']
relationship({ text: '哥哥', target: '弟弟', type: 'pair' });
// => ['兄弟']相对关系
ts
relationship({ text: '外婆', target: '舅妈', sex: 1 });
// => ['婆婆']
relationship({ text: '奶奶', target: '姑姑', sex: 0 });
// => ['儿媳']自然语言模式
ts
relationship('舅妈如何称呼外婆?');
// => ['婆婆']
relationship('外婆和奶奶之间是什么关系?');
// => ['儿女亲家']
relationship('爸爸的妈妈是谁?');
// => ['奶奶', '祖母']类型定义
ts
interface RelationshipOptions {
text: string;
target?: string;
sex?: Sex;
type?: OutputType;
reverse?: boolean;
mode?: string;
optimal?: boolean;
}
type Sex = -1 | 0 | 1;
type OutputType = 'default' | 'chain' | 'pair';