Merge branch 'master' of github.com:mumuy/relationship
This commit is contained in:
81
src/locale/guangdong.js
Normal file
81
src/locale/guangdong.js
Normal file
@@ -0,0 +1,81 @@
|
||||
export default {
|
||||
'name':'广东',
|
||||
'data':{
|
||||
'f':['老窦'],
|
||||
'f,f':['阿爷'],
|
||||
'f,f,f':['太公'],
|
||||
'f,f,m':['太婆'],
|
||||
'f,m':['阿嫲'],
|
||||
'f,m,f':['太外公'],
|
||||
'f,m,m':['太外婆'],
|
||||
'f,ob':['伯伯'],
|
||||
'f,ob,w':['伯娘'],
|
||||
'f,lb':['阿叔'],
|
||||
'f,lb,w':['阿婶'],
|
||||
'0,f,xb,d,s':['堂姨甥'],
|
||||
'1,f,xb,d,s':['堂外甥'],
|
||||
'0,f,xb,d,d':['堂姨甥女'],
|
||||
'1,f,xb,d,d':['堂外甥女'],
|
||||
'f,os':['姑妈'],
|
||||
'f,ls':['姑姐'],
|
||||
'f,xs,h':['姑丈'],
|
||||
'0,f,xs,d,s':['姑表姨甥'],
|
||||
'1,f,xs,d,s':['姑表外甥'],
|
||||
'0,f,xs,d,d':['姑表姨甥女'],
|
||||
'1,f,xs,d,d':['姑表外甥女'],
|
||||
'm':['老母'],
|
||||
'm,f':['阿公'],
|
||||
'm,f,f':['外太公'],
|
||||
'm,f,m':['外太婆'],
|
||||
'm,m':['阿婆'],
|
||||
'm,m,f':['外太外公'],
|
||||
'm,m,m':['外太外婆'],
|
||||
'm,ob':['舅父'],
|
||||
'm,lb':['舅仔'],
|
||||
'm,xb,w':['舅母'],
|
||||
'0,m,xb,d,s':['舅表姨甥'],
|
||||
'1,m,xb,d,s':['舅表外甥'],
|
||||
'0,m,xb,d,d':['舅表姨甥女'],
|
||||
'1,m,xb,d,d':['舅表外甥女'],
|
||||
'm,os':['姨妈'],
|
||||
'm,ls':['姨仔'],
|
||||
'm,xs,h':['姨丈'],
|
||||
'0,m,xb,d,s':['姨姨甥'],
|
||||
'1,m,xb,d,s':['姨外甥'],
|
||||
'0,m,xb,d,d':['姨姨甥女'],
|
||||
'1,m,xb,d,d':['姨外甥女'],
|
||||
'ob':['大佬'],
|
||||
'ob,w':['阿嫂'],
|
||||
'lb':['细佬'],
|
||||
'lb,w':['弟妇'],
|
||||
'xb,s':['侄'],
|
||||
'xb,d':['侄女'],
|
||||
'os':['家姐'],
|
||||
'os,h':['姐夫'],
|
||||
'ls':['阿妹'],
|
||||
'ls,h':['妹夫'],
|
||||
'0,xs,s':['姨甥'],
|
||||
'1,xs,s':['外甥'],
|
||||
'0,xs,d':['姨甥女'],
|
||||
'1,xs,d':['外甥女'],
|
||||
's':['仔'],
|
||||
's,w':['新妇'],
|
||||
's,s':['孙'],
|
||||
's,s,w':['孙新妇'],
|
||||
's,s,s':['息仔'],
|
||||
's,s,d':['息女'],
|
||||
's,d':['孙女'],
|
||||
's,d,h':['孙女婿'],
|
||||
's,d,s':['外息仔'],
|
||||
's,d,d':['外息女'],
|
||||
'd':['女'],
|
||||
'd,s':['外孙'],
|
||||
'd,s,w':['外孙新妇'],
|
||||
'd,s,s':['外息仔'],
|
||||
'd,s,d':['外息女'],
|
||||
'd,d':['外孙女'],
|
||||
'd,d,h':['外孙女婿'],
|
||||
'd,d,s':['外息仔'],
|
||||
'd,d,d':['外息女'],
|
||||
}
|
||||
}
|
||||
19
src/locale/north.js
Normal file
19
src/locale/north.js
Normal file
@@ -0,0 +1,19 @@
|
||||
export default {
|
||||
'name':'北方',
|
||||
'data':{
|
||||
'm,m':['姥姥'],
|
||||
'm,m,xs':['姨姥姥'],
|
||||
'm,m,xs,h':['姨姥爷'],
|
||||
'm,m,xb':['舅姥爷'],
|
||||
'm,m,xb,w':['舅姥姥'],
|
||||
'm,f':['姥爷'],
|
||||
'm,f,xs':['姑姥姥'],
|
||||
'm,f,xs,h':['姑姥爷'],
|
||||
'm,f,ob':['大姥爷'],
|
||||
'm,f,ob,w':['大姥姥'],
|
||||
'm,f,lb':['小姥爷'],
|
||||
'm,f,lb,w':['小姥姥'],
|
||||
'f,ob':['大爷'],
|
||||
'f,ob,w':['大娘'],
|
||||
}
|
||||
}
|
||||
82
src/module/expression.js
Normal file
82
src/module/expression.js
Normal file
@@ -0,0 +1,82 @@
|
||||
// 自然语言表达
|
||||
export default [
|
||||
{
|
||||
exp:/^(\S+?)(应该|得)?(称呼|叫|喊)(什么|啥)??$/,
|
||||
opt:match=>({
|
||||
text:match[1]
|
||||
})
|
||||
},
|
||||
{
|
||||
exp:/^(\S+?)(应该|得)?(如何|怎么)(称呼|叫|喊)??$/,
|
||||
opt:match=>({
|
||||
text:match[1]
|
||||
})
|
||||
},
|
||||
{
|
||||
exp:/^(\S+?)是(谁|什么亲戚|啥亲戚|什么辈分|啥辈分)??$/,
|
||||
opt:match=>{
|
||||
return {
|
||||
text:match[1],
|
||||
type:match[0].indexOf('的')>-1?'default':'chain'
|
||||
};
|
||||
}
|
||||
},
|
||||
{
|
||||
exp:/^(\S+?)(应该|得)?(称呼|叫|喊)(\S+?)(什么|啥)??$/,
|
||||
opt:match=>({
|
||||
text:match[4],
|
||||
target:match[1]
|
||||
})
|
||||
},
|
||||
{
|
||||
exp:/^(\S+?)(应该|得)?(如何|怎么)(称呼|叫|喊)(\S+?)??$/,
|
||||
opt:match=>({
|
||||
text:match[5],
|
||||
target:match[1]
|
||||
})
|
||||
},
|
||||
{
|
||||
exp:/^(\S+?)是(\S+?)的(谁|什么|什么亲戚|啥亲戚|什么人)??$/,
|
||||
opt:match=>({
|
||||
text:match[1],
|
||||
target:match[2]
|
||||
})
|
||||
},
|
||||
{
|
||||
exp:/^(\S+?)(应该|得)?管(\S+?)叫(什么|啥)??$/,
|
||||
opt:match=>({
|
||||
text:match[2],
|
||||
target:match[1]
|
||||
})
|
||||
},
|
||||
{
|
||||
exp:/^(\S+?)(和|与)(\S+?)(之间)?是(什么|啥)关系??$/,
|
||||
opt:match=>({
|
||||
text:match[1],
|
||||
target:match[3],
|
||||
type:'pair'
|
||||
})
|
||||
},
|
||||
{
|
||||
exp:/^(\S+?)(指的)?是(什么|啥)(意思|关系|辈分|人)??$/,
|
||||
opt:match=>({
|
||||
text:match[1],
|
||||
type:'chain'
|
||||
})
|
||||
},
|
||||
{
|
||||
exp:/^(\S+?)(指的)?是(什么|啥)亲戚关系??$/,
|
||||
opt:match=>({
|
||||
text:match[1],
|
||||
type:'chain'
|
||||
})
|
||||
},
|
||||
{
|
||||
exp:/^(\S+?)对于(\S+?)是(什么|啥)关系??$/,
|
||||
opt:match=>({
|
||||
text:match[1],
|
||||
target:match[2],
|
||||
type:'chain'
|
||||
})
|
||||
},
|
||||
];
|
||||
@@ -2,6 +2,7 @@
|
||||
import _filter from './filter';
|
||||
import _map from './map';
|
||||
import _pair from './pair';
|
||||
import _expression from './expression';
|
||||
|
||||
var _mode = {}; // 模式数据
|
||||
var _data = Object.assign({},_map); // 最终数据
|
||||
@@ -14,17 +15,8 @@ var zh2number = function(text){
|
||||
if(map[text]){
|
||||
num = map[text];
|
||||
}else{
|
||||
if(text.includes('十')){
|
||||
var numAttr = text.split('十');
|
||||
if(!numAttr[0]){
|
||||
num = 10;
|
||||
}else{
|
||||
num = textAttr.indexOf(numAttr[0])*10;
|
||||
}
|
||||
num += textAttr.indexOf(numAttr[1]);
|
||||
}else{
|
||||
num += textAttr.includes(text)?textAttr.indexOf(text):0;
|
||||
}
|
||||
var [unit,dec=0] = text.replace(/^十/,'一十').split('十').map(word=> textAttr.indexOf(word)).reverse();
|
||||
num = dec*10+unit;
|
||||
}
|
||||
return num;
|
||||
};
|
||||
@@ -39,15 +31,7 @@ var number2zh = function(num){
|
||||
}else{
|
||||
var dec = ~~(num/10);
|
||||
var unit = num%10;
|
||||
if(dec){
|
||||
if(dec>1){
|
||||
text = dec<textAttr.length?textAttr[dec]:'';
|
||||
}
|
||||
text += '十';
|
||||
text += unit<textAttr.length?textAttr[unit]:'';
|
||||
}else{
|
||||
text += num<textAttr.length?textAttr[num]:'';
|
||||
}
|
||||
text = (dec?(textAttr[dec]+'十').replace('一十','十'):'')+textAttr[unit];
|
||||
}
|
||||
return text;
|
||||
};
|
||||
@@ -66,9 +50,7 @@ var getGen = function(id){
|
||||
|
||||
// 获得最简
|
||||
var getOptimal = function(options){
|
||||
var from = options['from'];
|
||||
var to = options['to']
|
||||
var sex = options['sex'];
|
||||
var {from,to,sex} = options;
|
||||
var from_chain = options['from'].split(',');
|
||||
var to_chain = options['to'].split(',');
|
||||
for(var i=0;i<from_chain.length&&i<to_chain.length;i++){
|
||||
@@ -217,10 +199,10 @@ export function getSelectors(str){
|
||||
getList(name);
|
||||
// 通过关键词找关系
|
||||
keywords.forEach(function(name){
|
||||
var x_name = name.replace(/^[大|小]|^[一|二|三|四|五|六|七|八|九|十]+/,'几');
|
||||
var r_name = name.replace(/^[大|小]|^[一|二|三|四|五|六|七|八|九|十]+/,'');
|
||||
var match = name.match(/^[大|小]|^[一|二|三|四|五|六|七|八|九|十]+/);
|
||||
if(match){
|
||||
var x_name = name.replace(match[0],'几');
|
||||
var r_name = name.replace(match[0],'');
|
||||
var num = zh2number(match[0]);
|
||||
for(var i in _data){
|
||||
var r_i = i.replace(/(,[hw])$/,'&'+num+'$1').replace(/([^hw]+)$/,'$1&'+num);
|
||||
@@ -272,9 +254,7 @@ export function getSelectors(str){
|
||||
|
||||
// 合并选择器,查找两个对象之间的关系
|
||||
export function mergeSelector(param){
|
||||
var from_selector = param['from'];
|
||||
var to_selector = param['to'];
|
||||
var my_sex = param['sex'];
|
||||
var {from:from_selector,to:to_selector,sex:my_sex} = param;
|
||||
if(my_sex<0){
|
||||
var to_sex = -1;
|
||||
var from_sex = -1;
|
||||
@@ -379,8 +359,7 @@ export function selector2id(selector,sex){
|
||||
hash[selector] = true;
|
||||
do{
|
||||
s = selector;
|
||||
for(var i in _filter){
|
||||
var item = _filter[i];
|
||||
for(var item of _filter){
|
||||
// console.log('[filter]',item['exp'],selector);
|
||||
selector = selector.replace(item['exp'],item['str']);
|
||||
if(selector.includes('#')){
|
||||
@@ -457,19 +436,18 @@ export function reverseId(id,sex){
|
||||
// 通过ID获取关系称呼
|
||||
export function getItemsById(id){
|
||||
var items = [];
|
||||
var getData = function(d){
|
||||
var getData = function(key){
|
||||
var res = [];
|
||||
if(_data[d]){
|
||||
res.push(_data[d][0]);
|
||||
if(_data[key]){
|
||||
res.push(_data[key][0]);
|
||||
}else{
|
||||
for(var i in _data){
|
||||
if(i.replace(/&[ol]/g,'')==d){
|
||||
if(i.replace(/&[ol]/g,'')==key){
|
||||
res.push(_data[i][0]);
|
||||
}else{
|
||||
var expr = d;
|
||||
while (expr.match(/[ol](b|s)/)){
|
||||
expr = expr.replace(/[ol](b|s)/,'x$1');
|
||||
if(expr==i){
|
||||
while (key.match(/[ol](b|s)/)){
|
||||
key = key.replace(/[ol](b|s)/,'x$1');
|
||||
if(key==i){
|
||||
res.push(_data[i][0]);
|
||||
break;
|
||||
}
|
||||
@@ -589,11 +567,22 @@ export function setMode(sign,data){
|
||||
|
||||
// 获取模式数据
|
||||
export function getDataByMode(sign){
|
||||
var data = Object.assign({},_map);
|
||||
_data = Object.assign({},_map);
|
||||
if(sign&&_mode[sign]){
|
||||
for(var key in _mode[sign]){
|
||||
data[key] = [].concat(_mode[sign][key],_map[key]||[]);
|
||||
_data[key] = [].concat(_mode[sign][key],_map[key]||[]);
|
||||
}
|
||||
}
|
||||
return data;
|
||||
return _data;
|
||||
};
|
||||
|
||||
// 获取配置
|
||||
export function getOptions(text){
|
||||
for(var item of _expression){
|
||||
var match = text.match(item['exp']);
|
||||
if(match){
|
||||
return item['opt'](match);
|
||||
}
|
||||
}
|
||||
return {};
|
||||
};
|
||||
@@ -209,9 +209,7 @@ var prefix = {
|
||||
for(var key in prefixMap){
|
||||
for(var selector in prefixMap[key]){
|
||||
var ids = selector2id(selector);
|
||||
if(typeof prefix[key]=='undefined'){
|
||||
prefix[key] = {};
|
||||
}
|
||||
prefix[key] = prefix[key]||{};
|
||||
ids.forEach(function(id){
|
||||
prefix[key][id] = prefixMap[key][selector];
|
||||
});
|
||||
7
src/relationship-mode.js
Normal file
7
src/relationship-mode.js
Normal file
@@ -0,0 +1,7 @@
|
||||
import guangdong from './locale/guangdong';
|
||||
import north from './locale/north';
|
||||
|
||||
export default {
|
||||
guangdong,
|
||||
north
|
||||
};
|
||||
@@ -1,5 +1,6 @@
|
||||
import {
|
||||
unique,
|
||||
getOptions,
|
||||
getSelectors,
|
||||
mergeSelector,
|
||||
selector2id,
|
||||
@@ -9,11 +10,14 @@ import {
|
||||
getPairsByIds,
|
||||
setMode,
|
||||
getDataByMode
|
||||
} from './method';
|
||||
} from './module/method';
|
||||
var _data = getDataByMode(); // 最终数据
|
||||
|
||||
// 对外方法
|
||||
var relationship = function (parameter){
|
||||
if(typeof parameter =='string'){
|
||||
parameter = getOptions(parameter);
|
||||
}
|
||||
var options = Object.assign({
|
||||
text:'', // 目标对象:目标对象的称谓汉字表达,称谓间用‘的’字分隔
|
||||
target:'', // 相对对象:相对对象的称谓汉字表达,称谓间用‘的’字分隔,空表示自己
|
||||
@@ -33,13 +37,12 @@ var relationship = function (parameter){
|
||||
// console.log('[selectors]',from_selectors,to_selectors);
|
||||
from_selectors.forEach(function(from_selector){
|
||||
to_selectors.forEach(function(to_selector){
|
||||
var list = mergeSelector({
|
||||
mergeSelector({
|
||||
from:from_selector,
|
||||
to:to_selector,
|
||||
sex:options.sex,
|
||||
optimal:options.optimal
|
||||
});
|
||||
list.forEach(function(data){
|
||||
}).forEach(function(data){
|
||||
// console.log('[data]',from_selector,to_selector,data);
|
||||
var ids = data?selector2id(data['selector'],data['sex']):null;
|
||||
// console.log('[ids]',data['selector'],data['sex'],ids);
|
||||
@@ -73,9 +76,7 @@ var relationship = function (parameter){
|
||||
temps = reverseId(id,data['sex']);
|
||||
temps.forEach(function(r_id){
|
||||
var pairs = getPairsByIds(id,r_id);
|
||||
if(pairs.length){
|
||||
result = result.concat(pairs);
|
||||
}
|
||||
result = result.concat(pairs);
|
||||
});
|
||||
}else{
|
||||
temps.forEach(function(id){
|
||||
@@ -83,9 +84,7 @@ var relationship = function (parameter){
|
||||
if(!items.length){
|
||||
items = getItemsById(sex+','+id);
|
||||
}
|
||||
if(items.length){
|
||||
result = result.concat(items);
|
||||
}
|
||||
result = result.concat(items);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user