updated id去重后取名称

This commit is contained in:
PASSER-BY 2022-12-07 21:48:53 +08:00
parent f61adeeb28
commit 470ee24c45
5 changed files with 26 additions and 18 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -61,6 +61,15 @@ export function reverseId(id,sex){
return [''];
};
// ID列表去重
export function filterId(arr){
var sameList = arr.filter(item=>item==item.replace(/[ol](?=[s|b])/g,'x').replace(/&[ol]/,''));
return arr.filter(item=>{
var temp = item.replace(/[ol](?=[s|b])/g,'x').replace(/&[ol]/,'');
return sameList.includes(item)||item!=temp&&!sameList.includes(temp);
}).filter((item,idx,arr) => arr.indexOf(item) === idx);
};
// 通过ID获取世代数
export function getGenById(id){
var gMap = {'f':1,'m':1,'s':-1,'d':-1};
@ -81,19 +90,26 @@ export function getItemsById(id){
if(_data[key]){
res.push(_data[key][0]);
}else{
var ids = [];
for(var i in _data){
if(i.replace(/&[ol]/g,'')==key){
res.push(_data[i][0]);
// res.push(_data[i][0]);
ids.push(i);
}else{
while (key.match(/[ol](b|s)/)){
key = key.replace(/[ol](b|s)/,'x$1');
if(key==i){
res.push(_data[i][0]);
// res.push(_data[i][0]);
ids.push(i);
break;
}
}
}
}
res = filterId(ids).map(function(id){
return _data[id][0];
});
}
return res;
};

View File

@ -6,7 +6,7 @@ import _replace from './rule/replace';
import _similar from './rule/similar';
import {zh2number} from './unit';
import {reverseId,getGenById} from './id';
import {reverseId,filterId,getGenById} from './id';
import {modeData as _data} from './mode';
// 获得最简
@ -70,20 +70,11 @@ var getOptimal = function(options){
};
};
// 去重
var unique = function(arr){
var sameList = arr.filter(item=>item==item.replace(/[ol](?=s|b)/,'x').replace(/&[ol]/,''));
return arr.filter(item=>{
var temp = item.replace(/[ol](?=s|b)/,'x').replace(/&[ol]/,'');
return sameList.includes(item)||item!=temp&&!sameList.includes(temp);
}).filter((item,idx,arr) => arr.indexOf(item) === idx);
};
// 中文获取选择器
export function getSelectors(str){
str = str.replace(/之/g,'的').replace(/吾之?(.+)/,'$1').replace(/我的?(.+)/,'$1');
// 惯用口语标准化
// str = str.replace(/(?<![娘婆岳亲])家的?(?=(孩子|儿子|女儿))/,'的');
// str = str.replace(/(?<![娘婆岳亲])家的?(?=(孩子|儿子|女儿))/,'的');
if(str.match(/[^娘婆岳亲]家的?(孩子|儿子|女儿)/)){
str = str.replace(/家的?/,'的');
}
@ -307,5 +298,5 @@ export function selector2id(selector,sex){
var result = expandSelector(selector).map(function(selector){
return selector.replace(/,[01]/,'').substr(1); //去前面逗号和性别信息
});
return unique(result);
return filterId(result);
};

View File

@ -28,6 +28,7 @@ test('relationship.js show to be tested', function (t) {
t.deepEqual(relationship({text:'我',target:'爸爸'}),['儿子','女儿']);
t.deepEqual(relationship({text:'爱人',target:'娘家侄子'}),['姑丈']);
t.deepEqual(relationship({text:'岳母的配偶的孩子的爸爸'}),['岳父']);
t.deepEqual(relationship({text:'姐姐的老公的姐姐的老公'}),['姊妹姻姊妹壻']);
t.deepEqual(relationship({text:'舅妈',target:'外婆',type:'pair'}),['婆媳']);
t.deepEqual(relationship({text:'妈妈的二哥'}),['二舅']);
t.deepEqual(relationship({text:'爸爸的二哥'}),['二伯']);