updated 删除搜索

This commit is contained in:
PASSER-BY 2022-12-09 20:26:14 +08:00
parent fff5ea0571
commit a90e5abb2f
4 changed files with 16 additions and 53 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

@ -1,44 +0,0 @@
/*
* 数据快速搜索
*/
export function quickSearch(map){
var _ = this;
var _cacheKey = {};
var _cacheValue = {};
var groupLength = ~~Math.sqrt(Object.entries(map).length);
// 索引分组
var getGroupIndex = function(key){
var arr = key.replace(/,/g,'').split('').map(value=>value.codePointAt(0));
if(arr.length>1){
return arr.reduce((a,b)=>(a+b))%groupLength;
}
return 0;
};
// 建立缓存表
for(var key in map){
var keyIndex = getGroupIndex(key);
if(typeof _cacheKey[keyIndex]=='undefined'){
_cacheKey[keyIndex] = {};
}
_cacheKey[keyIndex][key] = map[key];
map[key].forEach(function(name){
var valueIndex = getGroupIndex(name);
if(typeof _cacheValue[valueIndex]=='undefined'){
_cacheValue[valueIndex] = {};
}
if(typeof _cacheValue[valueIndex][name]=='undefined'){
_cacheValue[valueIndex][name] = [];
}
_cacheValue[valueIndex][name].push(key);
});
}
// 查询方法
_.getNameByKey = function(key){
var keyIndex = getGroupIndex(key);
return _cacheKey[keyIndex][key]||[];
};
_.getKeyByName = function(name){
var valueIndex = getGroupIndex(name);
return _cacheValue[valueIndex][name]||[];
};
};

View File

@ -8,7 +8,16 @@ import _similar from './rule/similar';
import {zh2number} from './unit'; import {zh2number} from './unit';
import {reverseId,filterId,getGenById} from './id'; import {reverseId,filterId,getGenById} from './id';
import {modeData as _data} from './mode'; import {modeData as _data} from './mode';
import {quickSearch} from './search';
var _cache = {};
for(var key in _data){
_data[key].forEach(function(name){
if(typeof _cache[name]=='undefined'){
_cache[name] = [];
}
_cache[name].push(key);
});
}
// 获得最简 // 获得最简
var getOptimal = function(options){ var getOptimal = function(options){
@ -71,8 +80,6 @@ var getOptimal = function(options){
}; };
}; };
var search = new quickSearch(_data);
// 中文获取选择器 // 中文获取选择器
export function getSelectors(str){ export function getSelectors(str){
str = str.replace(/之/g,'的').replace(/吾之?(.+)/,'$1').replace(/我的?(.+)/,'$1'); str = str.replace(/之/g,'的').replace(/吾之?(.+)/,'$1').replace(/我的?(.+)/,'$1');
@ -126,9 +133,9 @@ export function getSelectors(str){
var x_name = name.replace(match[0],'几'); var x_name = name.replace(match[0],'几');
var r_name = name.replace(match[0],''); var r_name = name.replace(match[0],'');
var num = zh2number(match[0]); var num = zh2number(match[0]);
var x_ids = search.getKeyByName(x_name); var x_ids = _cache[x_name]||[];
var r_ids = search.getKeyByName(r_name); var r_ids = _cache[r_name]||[];
var i_ids = search.getKeyByName(name); var i_ids = _cache[name]||[];
if(x_ids.length){ if(x_ids.length){
x_ids.forEach(function(i){ x_ids.forEach(function(i){
var r_i = i.replace(/(,[hw])$/,'&'+num+'$1').replace(/([^hw]+)$/,'$1&'+num); var r_i = i.replace(/(,[hw])$/,'&'+num+'$1').replace(/([^hw]+)$/,'$1&'+num);
@ -148,7 +155,7 @@ export function getSelectors(str){
}); });
} }
} }
items = items.concat(search.getKeyByName(name)); items = items.concat(_cache[name]||[]);
}); });
// console.log('[keywords]',keywords); // console.log('[keywords]',keywords);
// 如找不到结果,再是否存在称呼的排行问题(不直接判断,因存在"大舅""三从父兄""三世祖"这样特俗含义的情况) // 如找不到结果,再是否存在称呼的排行问题(不直接判断,因存在"大舅""三从父兄""三世祖"这样特俗含义的情况)