mirror of
https://github.com/nextapps-de/flexsearch.git
synced 2025-09-01 18:03:56 +02:00
FIX encoding issues
FIX intersect
This commit is contained in:
@@ -828,11 +828,11 @@ The required memory for the index depends on several options:
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="left">Contextual Index</td>
|
||||
<td align="left">Multiplied with:</td>
|
||||
<td align="left">Multiply the sum above with:</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td>* 2(depth + 1)</td>
|
||||
<td>* (depth * 2 + 1)</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
|
169
flexsearch.js
169
flexsearch.js
@@ -3,7 +3,7 @@
|
||||
* ----------------------------------------------------------
|
||||
* @author: Thomas Wilkerling
|
||||
* @preserve https://github.com/nextapps-de/flexsearch
|
||||
* @version: 0.2.11
|
||||
* @version: 0.2.2
|
||||
* @license: Apache 2.0 Licence
|
||||
*/
|
||||
|
||||
@@ -928,9 +928,9 @@
|
||||
if(this.depth){
|
||||
|
||||
var use_contextual = true;
|
||||
var key = words[0];
|
||||
var ctx_root = words[0];
|
||||
|
||||
check_words[key] = "1";
|
||||
check_words[ctx_root] = "1";
|
||||
}
|
||||
else{
|
||||
|
||||
@@ -942,7 +942,7 @@
|
||||
|
||||
var ctx_map;
|
||||
|
||||
if(!use_contextual || (ctx_map = this._map[10])[key]){
|
||||
if(!use_contextual || (ctx_map = this._map[10])[ctx_root]){
|
||||
|
||||
for(var a = use_contextual ? 1 : 0; a < length; a++){
|
||||
|
||||
@@ -961,10 +961,11 @@
|
||||
|
||||
use_contextual ?
|
||||
|
||||
ctx_map[key][z][value]
|
||||
ctx_map[ctx_root]
|
||||
:
|
||||
this._map[z][value]
|
||||
);
|
||||
this._map
|
||||
|
||||
)[z][value];
|
||||
|
||||
if(map){
|
||||
|
||||
@@ -995,7 +996,7 @@
|
||||
check_words[value] = "1";
|
||||
}
|
||||
|
||||
key = value;
|
||||
ctx_root = value;
|
||||
}
|
||||
}
|
||||
else{
|
||||
@@ -1149,7 +1150,8 @@
|
||||
|
||||
'simple': (function(){
|
||||
|
||||
var regex_strip = regex('[^a-z0-9 ]'),
|
||||
var regex_whitespace = regex('\\s\\s+'),
|
||||
regex_strip = regex('[^a-z0-9 ]'),
|
||||
regex_split = regex('[-\/]'),
|
||||
regex_a = regex('[àáâãäå]'),
|
||||
regex_e = regex('[èéêë]'),
|
||||
@@ -1174,14 +1176,17 @@
|
||||
regex_c, 'c',
|
||||
regex_s, 's',
|
||||
regex_split, ' ',
|
||||
regex_strip, ''
|
||||
regex_strip, '',
|
||||
regex_whitespace, ' '
|
||||
];
|
||||
|
||||
return function(str){
|
||||
|
||||
str = replace(str.toLowerCase(), regex_pairs);
|
||||
|
||||
return (
|
||||
|
||||
replace(str.toLowerCase(), regex_pairs)
|
||||
str !== ' ' ? str : ''
|
||||
);
|
||||
};
|
||||
}()),
|
||||
@@ -1490,7 +1495,7 @@
|
||||
|
||||
var count_vowels = 0,
|
||||
count_literal = 0,
|
||||
count_parts = -1;
|
||||
count_parts = 0;
|
||||
|
||||
var tmp = "";
|
||||
var length = value.length;
|
||||
@@ -1524,20 +1529,28 @@
|
||||
|
||||
// dynamic n-gram sequences
|
||||
|
||||
if((char === ' ') || ((count_vowels >= 2) && (count_literal >= 2)) || (i === length - 1)){
|
||||
if((char === ' ') || ((count_vowels > 1) && (count_literal > 1)) || (count_vowels > 2) || (count_literal > 2) || (i === length - 1)){
|
||||
|
||||
if(tmp){
|
||||
|
||||
var tmp_length = tmp.length;
|
||||
if(parts[count_parts] && (tmp.length > 2)){
|
||||
|
||||
if(tmp_length > 2){
|
||||
|
||||
parts[++count_parts] = tmp;
|
||||
count_parts++;
|
||||
}
|
||||
else if(parts[count_parts]){
|
||||
|
||||
if(parts[count_parts]){
|
||||
|
||||
parts[count_parts] += tmp;
|
||||
}
|
||||
else{
|
||||
|
||||
parts[count_parts] = tmp;
|
||||
}
|
||||
|
||||
if(char === ' '){
|
||||
|
||||
count_parts++;
|
||||
}
|
||||
|
||||
tmp = "";
|
||||
}
|
||||
@@ -1567,7 +1580,7 @@
|
||||
|
||||
if(char !== char_prev){
|
||||
|
||||
if((i > 0) && (char === 'h')){
|
||||
if(i && (char === 'h')){
|
||||
|
||||
var char_prev_is_vowel = (
|
||||
|
||||
@@ -1589,7 +1602,7 @@
|
||||
(char_next === 'y')
|
||||
);
|
||||
|
||||
if(char_prev_is_vowel && char_next_is_vowel){
|
||||
if((char_prev_is_vowel && char_next_is_vowel) || (char_prev === ' ')){
|
||||
|
||||
collapsed_string += char;
|
||||
}
|
||||
@@ -1666,38 +1679,55 @@
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {!Array<Array<number|string>>} arr
|
||||
* Fastest intersect method for a set of unsorted arrays so far
|
||||
* @param {!Array<Array<number|string>>} arrays
|
||||
* @param {number=} limit
|
||||
* @returns {Array}
|
||||
*/
|
||||
|
||||
function intersect(arr, limit) {
|
||||
function intersect(arrays, limit) {
|
||||
|
||||
var result = [];
|
||||
var length_z = arr.length;
|
||||
var length_z = arrays.length;
|
||||
|
||||
if(length_z > 1){
|
||||
|
||||
arr.sort(sort_by_length_up);
|
||||
// pre-sort arrays by length up
|
||||
|
||||
var map = {};
|
||||
var a = arr[0];
|
||||
arrays.sort(sort_by_length_up);
|
||||
|
||||
for(var i = 0, length = a.length; i < length; ++i) {
|
||||
// fill initial map
|
||||
|
||||
map[a[i]] = 1;
|
||||
var check = {};
|
||||
var arr = arrays[0];
|
||||
var length = arr.length;
|
||||
var i = 0;
|
||||
|
||||
while(i < length) {
|
||||
|
||||
check[arr[i++]] = 1;
|
||||
}
|
||||
|
||||
// loop through arrays
|
||||
|
||||
var tmp, count = 0;
|
||||
var z = 1;
|
||||
|
||||
for(var z = 1; z < length_z; ++z){
|
||||
while(z < length_z){
|
||||
|
||||
// get each array one by one
|
||||
|
||||
var b = arr[z];
|
||||
var found = false;
|
||||
|
||||
for(var i = 0, length = b.length; i < length; ++i){
|
||||
arr = arrays[z];
|
||||
length = arr.length;
|
||||
i = 0;
|
||||
|
||||
if((map[tmp = b[i]]) === z){
|
||||
while(i < length){
|
||||
|
||||
if((check[tmp = arr[i++]]) === z){
|
||||
|
||||
// fill in during last round
|
||||
|
||||
if(z === (length_z - 1)){
|
||||
|
||||
@@ -1705,34 +1735,89 @@
|
||||
|
||||
if(limit && (count === limit)){
|
||||
|
||||
return result;
|
||||
found = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// apply count status
|
||||
|
||||
found = true;
|
||||
map[tmp] = z + 1;
|
||||
|
||||
break;
|
||||
check[tmp] = z + 1;
|
||||
}
|
||||
}
|
||||
|
||||
if(!found){
|
||||
|
||||
return [];
|
||||
break;
|
||||
}
|
||||
|
||||
z++;
|
||||
}
|
||||
}
|
||||
else if(length_z){
|
||||
|
||||
result = arrays[0];
|
||||
|
||||
if(limit && result && (result.length > limit)){
|
||||
|
||||
// Note: do not touch original array!
|
||||
|
||||
result = result.slice(0, limit);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
else if(length_z){
|
||||
|
||||
result = arr[0];
|
||||
/**
|
||||
* Fastest intersect method for 2 sorted arrays so far
|
||||
* @param {!Array<number|string>} a
|
||||
* @param {!Array<number|string>} b
|
||||
* @param {number=} limit
|
||||
* @returns {Array}
|
||||
*/
|
||||
|
||||
if(limit && result && (result.length > limit)){
|
||||
function intersect_sorted(a, b, limit){
|
||||
|
||||
// Note: do not touch original array
|
||||
var result = [];
|
||||
|
||||
result = result.slice(0, limit);
|
||||
var length_a = a.length,
|
||||
length_b = b.length;
|
||||
|
||||
if(length_a && length_b){
|
||||
|
||||
var x = 0, y = 0, count = 0;
|
||||
|
||||
var current_a = 0,
|
||||
current_b = 0;
|
||||
|
||||
while(true){
|
||||
|
||||
if((current_a || (current_a = a[x])) ===
|
||||
(current_b || (current_b = b[y]))){
|
||||
|
||||
result[count++] = current_a;
|
||||
|
||||
current_a = current_b = 0;
|
||||
x++;
|
||||
y++;
|
||||
}
|
||||
else if(current_a < current_b){
|
||||
|
||||
current_a = 0;
|
||||
x++;
|
||||
}
|
||||
else{
|
||||
|
||||
current_b = 0;
|
||||
y++;
|
||||
}
|
||||
|
||||
if((x === length_a) || (y === length_b)){
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
42
flexsearch.min.js
vendored
42
flexsearch.min.js
vendored
@@ -1,25 +1,25 @@
|
||||
/*
|
||||
https://github.com/nextapps-de/flexsearch
|
||||
@version: 0.2.11
|
||||
@version: 0.2.2
|
||||
@license: Apache 2.0 Licence
|
||||
*/
|
||||
'use strict';(function(m,C,f){var d;(d=f.define)&&d.amd?d([],function(){return C}):(d=f.modules)?d[m.toLowerCase()]=C:"undefined"!==typeof module?module.exports=C:f[m]=C})("FlexSearch",function H(m){function f(a){a||(a=t);this.id=a.id||I++;this.init(a);Object.defineProperty(this,"index",{get:function(){return this.a}});Object.defineProperty(this,"length",{get:function(){return Object.keys(this.a).length}})}function d(a){return new RegExp(a,"g")}function u(a,b,c){if("undefined"===typeof c){for(c=0;c<
|
||||
b.length;c+=2)a=a.replace(b[c],b[c+1]);return a}return a.replace(b,c)}function p(a,b,c,e,l,n){if("undefined"===typeof b[c]){var g=l.indexOf(c);g=3/l.length*(l.length-g)+6/(g-l.lastIndexOf(" ",g))+.5|0;b[c]=g;g>n&&(a=a[g],a=a[c]||(a[c]=[]),a[a.length]=e)}return g||b[c]}function x(a){for(var b="",c="",e="",l=0;l<a.length;l++){var n=a[l];n!==c&&(0<l&&"h"===n?(e="a"===e||"e"===e||"i"===e||"o"===e||"u"===e||"y"===e,"a"!==c&&"e"!==c&&"i"!==c&&"o"!==c&&"u"!==c&&"y"!==c||!e||(b+=n)):b+=n);e=l===a.length-
|
||||
1?"":a[l+1];c=n}return b}function y(a,b){a=a.length-b.length;return 0>a?1:0<a?-1:0}function J(a,b){a=a.length-b.length;return 0>a?-1:0<a?1:0}function K(a,b){var c=[],e=a.length;if(1<e){a.sort(J);for(var l={},n=a[0],g=0,D=n.length;g<D;++g)l[n[g]]=1;for(var d=0,k=1;k<e;++k){var h=a[k],f=!1;g=0;for(D=h.length;g<D;++g)if(l[n=h[g]]===k){if(k===e-1&&(c[d++]=n,b&&d===b))return c;f=!0;l[n]=k+1;break}if(!f)return[]}}else e&&(c=a[0],b&&c&&c.length>b&&(c=c.slice(0,b)));return c}function z(a){a.w||(a.w=E(function(){a.w=
|
||||
null;var b=a.async;b&&(a.async=!1);if(a.c.length){for(var c=F(),e;(e=a.c.shift())||0===e;){var l=a.h[e];switch(l[0]){case A.add:a.add(l[1],l[2]);break;case A.update:a.update(l[1],l[2]);break;case A.remove:a.remove(l[1])}a.h[e]=null;delete a.h[e];if(100<F()-c)break}a.c.length&&z(a)}b&&(a.async=b)},1,"search-async-"+a.id))}function F(){return"undefined"!==typeof performance?performance.now():(new Date).getTime()}function L(a,b,c,e){a=m("flexsearch","id"+a,function(){var a,b;self.a=function(c){if(c=
|
||||
c.data)c.search?self.postMessage({result:b.search(c.content,c.threshold?{limit:c.limit,threshold:c.threshold}:c.limit),id:a,content:c.content,limit:c.limit}):c.add?b.add(c.id,c.content):c.update?b.update(c.id,c.content):c.remove?b.remove(c.id):c.reset?b.reset():c.info?(c=b.info(),c.worker=a,b.A&&console.log(c)):c.register&&(a=c.id,c.options.cache=!1,c.options.async=!0,c.options.worker=!1,b=(new Function(c.register.substring(c.register.indexOf("{")+1,c.register.lastIndexOf("}"))))(),b=new b(c.options))}},
|
||||
function(a){(a=a.data)&&a.result&&e(a.id,a.content,a.result,a.limit)},b);var l=H.toString();c.id=b;a.postMessage(b,{register:l,options:c,id:b});return a}var t={type:"integer",mode:"forward",cache:!1,async:!1,b:!1,threshold:0,depth:0,encode:"icase"},v=[],I=0,A={add:0,update:1,remove:2},G=d("[ -/]");f.new=function(a){return new this(a)};f.create=function(a){return f.new(a)};f.addMatcher=function(a){for(var b in a)a.hasOwnProperty(b)&&(v[v.length]=d(b),v[v.length]=a[b]);return this};f.register=function(a,
|
||||
b){B[a]=b;return this};f.encode=function(a,b){return B[a].call(B,b)};f.prototype.init=function(a){this.m=[];if(a){if(a.worker)if("undefined"===typeof Worker)a.worker=!1,a.async=!0,this.j=null;else{var b=this,c=parseInt(a.worker,10)||4;b.s=-1;b.o=0;b.i=[];b.v=null;b.j=Array(c);for(var e=0;e<c;e++)b.j[e]=L(b.id,e,a||t,function(a,c,e,d){b.o!==b.b&&(b.i=b.i.concat(e),b.o++,d&&b.i.length>=d&&(b.o=b.b),b.v&&b.o===b.b&&(b.i.length?b.f="":b.f||(b.f=c),b.cache&&b.l.set(c,b.i),b.v(b.i),b.i=[]))})}this.mode=
|
||||
a.mode||this.mode||t.mode;this.cache=a.cache||this.cache||t.cache;this.async=a.async||this.async||t.async;this.b=a.worker||this.b||t.b;this.threshold=a.threshold||this.threshold||t.threshold;this.depth=a.depth||this.depth||t.depth;this.encoder=a.encode&&B[a.encode]||("function"===typeof a.encode?a.encode:this.encoder||!1);this.A=a.debug||this.A;a.matcher&&this.addMatcher(a.matcher)}this.g=[{},{},{},{},{},{},{},{},{},{},{}];this.a={};this.h={};this.c=[];this.w=null;this.f="";this.u=!0;this.l=this.cache?
|
||||
new M(3E4,50,!0):!1;return this};f.prototype.encode=function(a){a&&v.length&&(a=u(a,v));a&&this.m.length&&(a=u(a,this.m));a&&this.encoder&&(a=this.encoder.call(B,a));return a};f.prototype.addMatcher=function(a){for(var b in a)a.hasOwnProperty(b)&&(this.m[this.m.length]=d(b),this.m[this.m.length]=a[b]);return this};f.prototype.add=function(a,b){if("string"===typeof b&&b&&(a||0===a))if(this.a[a])this.update(a,b);else{if(this.b)return++this.s>=this.j.length&&(this.s=0),this.j[this.s].postMessage(this.s,
|
||||
{add:!0,id:a,content:b}),this.a[a]=""+this.s,this;if(this.async)return this.h[a]||(this.c[this.c.length]=a),this.h[a]=[A.add,a,b],z(this),this;b=this.encode(b);if(!b.length)return this;if("ngram"===this.mode){var c=b;var e=[];if(c)for(var d=0,n=0,g=-1,f="",q=c.length,k=0;k<q;k++){var h=c[k];"a"===h||"e"===h||"i"===h||"o"===h||"u"===h||"y"===h?d++:n++;" "!==h&&(f+=h);if(" "===h||2<=d&&2<=n||k===q-1)f&&(2<f.length?e[++g]=f:e[g]&&(e[g]+=f),f=""),n=d=0}c=e}else c=b.split(G);e={_ctx:{}};d=this.threshold;
|
||||
n=this.depth;g=this.g;f=c.length;for(q=0;q<f;q++)if(k=c[q]){var m=k.length;switch(this.mode){case "reverse":case "both":var r="";for(h=m-1;1<=h;h--)r=k[h]+r,p(g,e,r,a,b,d);case "forward":r="";for(h=0;h<m;h++)r+=k[h],p(g,e,r,a,b,d);break;case "full":for(h=0;h<m;h++)for(var w=m;w>h;w--)r=k.substring(h,w),p(g,e,r,a,b,d);break;default:if(h=p(g,e,k,a,b,d),n&&1<f&&h>d)for(h=g[10],m=e._ctx[k]||(e._ctx[k]={}),k=h[k]||(h[k]=[{},{},{},{},{},{},{},{},{},{}]),h=q-n,w=q+n,0>h&&(h=0),w>f-1&&(w=f-1);h<=w;h++)h!==
|
||||
q&&p(k,m,c[h],a,b,d)}}this.a[a]="1";this.u=!1}return this};f.prototype.update=function(a,b){if("string"===typeof b&&(a||0===a)&&this.a[a]){if(this.b){var c=parseInt(this.a[a],10);this.j[c].postMessage(c,{update:!0,id:a,content:b});return this}if(this.async)return this.h[a]||(this.c[this.c.length]=a),this.h[a]=[A.update,a,b],z(this),this;this.remove(a);b&&this.add(a,b)}return this};f.prototype.remove=function(a){if(this.a[a]){if(this.b){var b=parseInt(this.a[a],10);this.j[b].postMessage(b,{remove:!0,
|
||||
id:a});delete this.a[a];return this}if(this.async)return this.h[a]||(this.c[this.c.length]=a),this.h[a]=[A.remove,a],z(this),this;for(b=0;10>b;b++)for(var c=Object.keys(this.g[b]),e=0;e<c.length;e++){var d=c[e],f=this.g[b];if((f=f&&f[d])&&f.length)for(var g=0;g<f.length;g++)if(f[g]===a){f.splice(g,1);break}f.length||delete this.g[b][d]}delete this.a[a];this.u=!1}return this};f.prototype.search=function(a,b,c){var e=[];if(a&&"object"===typeof a){c=a.callback||b;b=a.limit;var d=a.threshold;a=a.query}d||
|
||||
(d=0);"function"===typeof b?(c=b,b=1E3):b||(b=1E3);if(this.b){this.v=c;this.o=0;this.i=[];for(e=0;e<this.b;e++)this.j[e].postMessage(e,{search:!0,limit:b,threshold:d,content:a});return null}if(c){var f=this;E(function(){c(f.search(a,b));f=null},1,"search-"+this.id);return null}if(!a||"string"!==typeof a)return e;var g=a;if(!this.u)this.cache&&(this.f="",this.l.reset()),this.u=!0;else if(this.cache){var m=this.l.get(a);if(m)return m}else if(this.f&&0===a.indexOf(this.f))return e;g=this.encode(g);if(!g.length)return e;
|
||||
g=g.constructor===Array?g:g.split(G);m=g.length;var q=!0,k=[],h={};if(1<m)if(this.depth){var t=!0,r=g[0];h[r]="1"}else g.sort(y);var w;if(!t||(w=this.g[10])[r])for(var u=t?1:0;u<m;u++){var p=g[u];if(p&&!h[p]){for(var v,A=!1,x=[],B=0,z=9;z>=d;z--)if(v=t?w[r][z][p]:this.g[z][p])x[B++]=v,A=!0;if(A)k[k.length]=1<B?k.concat.apply([],x):x[0];else{q=!1;break}h[p]="1"}r=p}else q=!1;q&&(e=K(k,b));e.length?this.f="":this.f||(this.f=a);this.cache&&this.l.set(a,e);return e};f.prototype.info=function(){if(this.b)for(var a=
|
||||
0;a<this.b;a++)this.j[a].postMessage(a,{info:!0,id:this.id});else{for(var b,c,e=0,d=0,f=0,g=0;10>g;g++)for(b=Object.keys(this.g[g]),a=0;a<b.length;a++)c=this.g[g][b[a]].length,e+=c+2*b[a].length+4,d+=c,f+=2*b[a].length;b=Object.keys(this.a);c=b.length;for(a=0;a<c;a++)e+=2*b[a].length+2;return{id:this.id,memory:e,items:c,sequences:d,chars:f,status:this.u,cache:this.c.length,matcher:v.length,worker:this.b}}};f.prototype.reset=function(){this.destroy();return this.init()};f.prototype.destroy=function(){this.cache&&
|
||||
this.l.reset();this.g=this.a=this.l=null;return this};var B={icase:function(a){return a.toLowerCase()},simple:function(){var a=[d("[\u00e0\u00e1\u00e2\u00e3\u00e4\u00e5]"),"a",d("[\u00e8\u00e9\u00ea\u00eb]"),"e",d("[\u00ec\u00ed\u00ee\u00ef]"),"i",d("[\u00f2\u00f3\u00f4\u00f5\u00f6\u0151]"),"o",d("[\u00f9\u00fa\u00fb\u00fc\u0171]"),"u",d("[\u00fd\u0177\u00ff]"),"y",d("\u00f1"),"n",d("\u00e7"),"c",d("\u00df"),"s",d("[-/]")," ",d("[^a-z0-9 ]"),""];return function(b){return u(b.toLowerCase(),a)}}(),
|
||||
advanced:function(){var a=[d("ae"),"a",d("ai"),"ei",d("ay"),"ei",d("ey"),"ei",d("oe"),"o",d("ue"),"u",d("ie"),"i",d("sz"),"s",d("zs"),"s",d("sh"),"s",d("ck"),"k",d("cc"),"k",d("dt"),"t",d("ph"),"f",d("pf"),"f",d("ou"),"o",d("uo"),"u"];return function(b,c){if(!b)return b;b=this.simple(b);2<b.length&&(b=u(b,a));c||1<b.length&&(b=x(b));return b}}(),extra:function(){var a=[d("p"),"b",d("z"),"s",d("[cgq]"),"k",d("n"),"m",d("d"),"t",d("[vw]"),"f",d("[aeiouy]"),""];return function(b){if(!b)return b;b=this.advanced(b,
|
||||
!0);if(1<b.length){b=b.split(" ");for(var c=0;c<b.length;c++){var d=b[c];1<d.length&&(b[c]=d[0]+u(d.substring(1),a))}b=b.join("");b=x(b)}return b}}()},E=function(){var a={};return function(b,c,d){var e=a[d];e&&clearTimeout(e);return a[d]=setTimeout(b,c)}}(),M=function(){function a(){this.cache={}}a.prototype.reset=function(){this.cache={}};a.prototype.set=function(a,c){this.cache[a]=c};a.prototype.get=function(a){return this.cache[a]};return a}();return f}(function(){var m={},C=!("undefined"===typeof Blob||
|
||||
"undefined"===typeof URL||!URL.createObjectURL);return function(f,d,u,p,x){var y=f;f=C?URL.createObjectURL(new Blob(["("+u.toString()+")()"],{type:"text/javascript"})):"../"+y+".js";y+="-"+d;m[y]||(m[y]=[]);m[y][x]=new Worker(f);m[y][x].onmessage=p;return{postMessage:function(d,f){m[y][d].postMessage(f)}}}}()),this);
|
||||
'use strict';(function(l,C,e){var d;(d=e.define)&&d.amd?d([],function(){return C}):(d=e.modules)?d[l.toLowerCase()]=C:"undefined"!==typeof module?module.exports=C:e[l]=C})("FlexSearch",function H(l){function e(a){a||(a=u);this.id=a.id||I++;this.init(a);Object.defineProperty(this,"index",{get:function(){return this.a}});Object.defineProperty(this,"length",{get:function(){return Object.keys(this.a).length}})}function d(a){return new RegExp(a,"g")}function v(a,b,c){if("undefined"===typeof c){for(c=0;c<
|
||||
b.length;c+=2)a=a.replace(b[c],b[c+1]);return a}return a.replace(b,c)}function q(a,b,c,h,n,k){if("undefined"===typeof b[c]){var f=n.indexOf(c);f=3/n.length*(n.length-f)+6/(f-n.lastIndexOf(" ",f))+.5|0;b[c]=f;f>k&&(a=a[f],a=a[c]||(a[c]=[]),a[a.length]=h)}return f||b[c]}function x(a){for(var b="",c="",h="",n=0;n<a.length;n++){var k=a[n];if(k!==c)if(n&&"h"===k){if(h="a"===h||"e"===h||"i"===h||"o"===h||"u"===h||"y"===h,("a"===c||"e"===c||"i"===c||"o"===c||"u"===c||"y"===c)&&h||" "===c)b+=k}else b+=k;
|
||||
h=n===a.length-1?"":a[n+1];c=k}return b}function y(a,b){a=a.length-b.length;return 0>a?1:0<a?-1:0}function J(a,b){a=a.length-b.length;return 0>a?-1:0<a?1:0}function K(a,b){var c=[],h=a.length;if(1<h){a.sort(J);for(var n={},k=a[0],f=k.length,D=0;D<f;)n[k[D++]]=1;for(var d,e=0,g=1;g<h;){var p=!1;k=a[g];f=k.length;for(D=0;D<f;)if(n[d=k[D++]]===g){if(g===h-1&&(c[e++]=d,b&&e===b)){p=!1;break}p=!0;n[d]=g+1}if(!p)break;g++}}else h&&(c=a[0],b&&c&&c.length>b&&(c=c.slice(0,b)));return c}function A(a){a.w||
|
||||
(a.w=E(function(){a.w=null;var b=a.async;b&&(a.async=!1);if(a.c.length){for(var c=F(),h;(h=a.c.shift())||0===h;){var d=a.h[h];switch(d[0]){case z.add:a.add(d[1],d[2]);break;case z.update:a.update(d[1],d[2]);break;case z.remove:a.remove(d[1])}a.h[h]=null;delete a.h[h];if(100<F()-c)break}a.c.length&&A(a)}b&&(a.async=b)},1,"search-async-"+a.id))}function F(){return"undefined"!==typeof performance?performance.now():(new Date).getTime()}function L(a,b,c,h){a=l("flexsearch","id"+a,function(){var a,b;self.a=
|
||||
function(c){if(c=c.data)c.search?self.postMessage({result:b.search(c.content,c.threshold?{limit:c.limit,threshold:c.threshold}:c.limit),id:a,content:c.content,limit:c.limit}):c.add?b.add(c.id,c.content):c.update?b.update(c.id,c.content):c.remove?b.remove(c.id):c.reset?b.reset():c.info?(c=b.info(),c.worker=a,b.A&&console.log(c)):c.register&&(a=c.id,c.options.cache=!1,c.options.async=!0,c.options.worker=!1,b=(new Function(c.register.substring(c.register.indexOf("{")+1,c.register.lastIndexOf("}"))))(),
|
||||
b=new b(c.options))}},function(a){(a=a.data)&&a.result&&h(a.id,a.content,a.result,a.limit)},b);var d=H.toString();c.id=b;a.postMessage(b,{register:d,options:c,id:b});return a}var u={type:"integer",mode:"forward",cache:!1,async:!1,b:!1,threshold:0,depth:0,encode:"icase"},w=[],I=0,z={add:0,update:1,remove:2},G=d("[ -/]");e.new=function(a){return new this(a)};e.create=function(a){return e.new(a)};e.addMatcher=function(a){for(var b in a)a.hasOwnProperty(b)&&(w[w.length]=d(b),w[w.length]=a[b]);return this};
|
||||
e.register=function(a,b){B[a]=b;return this};e.encode=function(a,b){return B[a].call(B,b)};e.prototype.init=function(a){this.m=[];if(a){if(a.worker)if("undefined"===typeof Worker)a.worker=!1,a.async=!0,this.j=null;else{var b=this,c=parseInt(a.worker,10)||4;b.s=-1;b.o=0;b.i=[];b.v=null;b.j=Array(c);for(var h=0;h<c;h++)b.j[h]=L(b.id,h,a||u,function(a,c,h,d){b.o!==b.b&&(b.i=b.i.concat(h),b.o++,d&&b.i.length>=d&&(b.o=b.b),b.v&&b.o===b.b&&(b.i.length?b.f="":b.f||(b.f=c),b.cache&&b.l.set(c,b.i),b.v(b.i),
|
||||
b.i=[]))})}this.mode=a.mode||this.mode||u.mode;this.cache=a.cache||this.cache||u.cache;this.async=a.async||this.async||u.async;this.b=a.worker||this.b||u.b;this.threshold=a.threshold||this.threshold||u.threshold;this.depth=a.depth||this.depth||u.depth;this.encoder=a.encode&&B[a.encode]||("function"===typeof a.encode?a.encode:this.encoder||!1);this.A=a.debug||this.A;a.matcher&&this.addMatcher(a.matcher)}this.g=[{},{},{},{},{},{},{},{},{},{},{}];this.a={};this.h={};this.c=[];this.w=null;this.f="";this.u=
|
||||
!0;this.l=this.cache?new M(3E4,50,!0):!1;return this};e.prototype.encode=function(a){a&&w.length&&(a=v(a,w));a&&this.m.length&&(a=v(a,this.m));a&&this.encoder&&(a=this.encoder.call(B,a));return a};e.prototype.addMatcher=function(a){for(var b in a)a.hasOwnProperty(b)&&(this.m[this.m.length]=d(b),this.m[this.m.length]=a[b]);return this};e.prototype.add=function(a,b){if("string"===typeof b&&b&&(a||0===a))if(this.a[a])this.update(a,b);else{if(this.b)return++this.s>=this.j.length&&(this.s=0),this.j[this.s].postMessage(this.s,
|
||||
{add:!0,id:a,content:b}),this.a[a]=""+this.s,this;if(this.async)return this.h[a]||(this.c[this.c.length]=a),this.h[a]=[z.add,a,b],A(this),this;b=this.encode(b);if(!b.length)return this;if("ngram"===this.mode){var c=b;var h=[];if(c)for(var d=0,k=0,f=0,e="",r=c.length,m=0;m<r;m++){var g=c[m];"a"===g||"e"===g||"i"===g||"o"===g||"u"===g||"y"===g?d++:k++;" "!==g&&(e+=g);if(" "===g||1<d&&1<k||2<d||2<k||m===r-1)e&&(h[f]&&2<e.length&&f++,h[f]=h[f]?h[f]+e:e," "===g&&f++,e=""),k=d=0}c=h}else c=b.split(G);h=
|
||||
{_ctx:{}};d=this.threshold;k=this.depth;f=this.g;e=c.length;for(r=0;r<e;r++)if(m=c[r]){var p=m.length;switch(this.mode){case "reverse":case "both":var t="";for(g=p-1;1<=g;g--)t=m[g]+t,q(f,h,t,a,b,d);case "forward":t="";for(g=0;g<p;g++)t+=m[g],q(f,h,t,a,b,d);break;case "full":for(g=0;g<p;g++)for(var l=p;l>g;l--)t=m.substring(g,l),q(f,h,t,a,b,d);break;default:if(g=q(f,h,m,a,b,d),k&&1<e&&g>d)for(g=f[10],p=h._ctx[m]||(h._ctx[m]={}),m=g[m]||(g[m]=[{},{},{},{},{},{},{},{},{},{}]),g=r-k,l=r+k,0>g&&(g=0),
|
||||
l>e-1&&(l=e-1);g<=l;g++)g!==r&&q(m,p,c[g],a,b,d)}}this.a[a]="1";this.u=!1}return this};e.prototype.update=function(a,b){if("string"===typeof b&&(a||0===a)&&this.a[a]){if(this.b){var c=parseInt(this.a[a],10);this.j[c].postMessage(c,{update:!0,id:a,content:b});return this}if(this.async)return this.h[a]||(this.c[this.c.length]=a),this.h[a]=[z.update,a,b],A(this),this;this.remove(a);b&&this.add(a,b)}return this};e.prototype.remove=function(a){if(this.a[a]){if(this.b){var b=parseInt(this.a[a],10);this.j[b].postMessage(b,
|
||||
{remove:!0,id:a});delete this.a[a];return this}if(this.async)return this.h[a]||(this.c[this.c.length]=a),this.h[a]=[z.remove,a],A(this),this;for(b=0;10>b;b++)for(var c=Object.keys(this.g[b]),d=0;d<c.length;d++){var e=c[d],k=this.g[b];if((k=k&&k[e])&&k.length)for(var f=0;f<k.length;f++)if(k[f]===a){k.splice(f,1);break}k.length||delete this.g[b][e]}delete this.a[a];this.u=!1}return this};e.prototype.search=function(a,b,c){var d=[];if(a&&"object"===typeof a){c=a.callback||b;b=a.limit;var e=a.threshold;
|
||||
a=a.query}e||(e=0);"function"===typeof b?(c=b,b=1E3):b||(b=1E3);if(this.b){this.v=c;this.o=0;this.i=[];for(d=0;d<this.b;d++)this.j[d].postMessage(d,{search:!0,limit:b,threshold:e,content:a});return null}if(c){var k=this;E(function(){c(k.search(a,b));k=null},1,"search-"+this.id);return null}if(!a||"string"!==typeof a)return d;var f=a;if(!this.u)this.cache&&(this.f="",this.l.reset()),this.u=!0;else if(this.cache){var l=this.l.get(a);if(l)return l}else if(this.f&&0===a.indexOf(this.f))return d;f=this.encode(f);
|
||||
if(!f.length)return d;f=f.constructor===Array?f:f.split(G);l=f.length;var r=!0,m=[],g={};if(1<l)if(this.depth){var p=!0,t=f[0];g[t]="1"}else f.sort(y);var u;if(!p||(u=this.g[10])[t])for(var v=p?1:0;v<l;v++){var q=f[v];if(q&&!g[q]){for(var w,z=!1,x=[],B=0,A=9;A>=e;A--)if(w=(p?u[t]:this.g)[A][q])x[B++]=w,z=!0;if(z)m[m.length]=1<B?m.concat.apply([],x):x[0];else{r=!1;break}g[q]="1"}t=q}else r=!1;r&&(d=K(m,b));d.length?this.f="":this.f||(this.f=a);this.cache&&this.l.set(a,d);return d};e.prototype.info=
|
||||
function(){if(this.b)for(var a=0;a<this.b;a++)this.j[a].postMessage(a,{info:!0,id:this.id});else{for(var b,c,d=0,e=0,k=0,f=0;10>f;f++)for(b=Object.keys(this.g[f]),a=0;a<b.length;a++)c=this.g[f][b[a]].length,d+=c+2*b[a].length+4,e+=c,k+=2*b[a].length;b=Object.keys(this.a);c=b.length;for(a=0;a<c;a++)d+=2*b[a].length+2;return{id:this.id,memory:d,items:c,sequences:e,chars:k,status:this.u,cache:this.c.length,matcher:w.length,worker:this.b}}};e.prototype.reset=function(){this.destroy();return this.init()};
|
||||
e.prototype.destroy=function(){this.cache&&this.l.reset();this.g=this.a=this.l=null;return this};var B={icase:function(a){return a.toLowerCase()},simple:function(){var a=[d("[\u00e0\u00e1\u00e2\u00e3\u00e4\u00e5]"),"a",d("[\u00e8\u00e9\u00ea\u00eb]"),"e",d("[\u00ec\u00ed\u00ee\u00ef]"),"i",d("[\u00f2\u00f3\u00f4\u00f5\u00f6\u0151]"),"o",d("[\u00f9\u00fa\u00fb\u00fc\u0171]"),"u",d("[\u00fd\u0177\u00ff]"),"y",d("\u00f1"),"n",d("\u00e7"),"c",d("\u00df"),"s",d("[-/]")," ",d("[^a-z0-9 ]"),"",d("\\s\\s+"),
|
||||
" "];return function(b){b=v(b.toLowerCase(),a);return" "!==b?b:""}}(),advanced:function(){var a=[d("ae"),"a",d("ai"),"ei",d("ay"),"ei",d("ey"),"ei",d("oe"),"o",d("ue"),"u",d("ie"),"i",d("sz"),"s",d("zs"),"s",d("sh"),"s",d("ck"),"k",d("cc"),"k",d("dt"),"t",d("ph"),"f",d("pf"),"f",d("ou"),"o",d("uo"),"u"];return function(b,c){if(!b)return b;b=this.simple(b);2<b.length&&(b=v(b,a));c||1<b.length&&(b=x(b));return b}}(),extra:function(){var a=[d("p"),"b",d("z"),"s",d("[cgq]"),"k",d("n"),"m",d("d"),"t",
|
||||
d("[vw]"),"f",d("[aeiouy]"),""];return function(b){if(!b)return b;b=this.advanced(b,!0);if(1<b.length){b=b.split(" ");for(var c=0;c<b.length;c++){var d=b[c];1<d.length&&(b[c]=d[0]+v(d.substring(1),a))}b=b.join(" ");b=x(b)}return b}}()},E=function(){var a={};return function(b,c,d){var e=a[d];e&&clearTimeout(e);return a[d]=setTimeout(b,c)}}(),M=function(){function a(){this.cache={}}a.prototype.reset=function(){this.cache={}};a.prototype.set=function(a,c){this.cache[a]=c};a.prototype.get=function(a){return this.cache[a]};
|
||||
return a}();return e}(function(){var l={},C=!("undefined"===typeof Blob||"undefined"===typeof URL||!URL.createObjectURL);return function(e,d,v,q,x){var y=e;e=C?URL.createObjectURL(new Blob(["("+v.toString()+")()"],{type:"text/javascript"})):"../"+y+".js";y+="-"+d;l[y]||(l[y]=[]);l[y][x]=new Worker(e);l[y][x].onmessage=q;return{postMessage:function(d,e){l[y][d].postMessage(e)}}}}()),this);
|
||||
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "flexsearch",
|
||||
"version": "0.2.125",
|
||||
"version": "0.2.2",
|
||||
"description": "World's fastest and most memory efficient full text search library.",
|
||||
"keywords": [],
|
||||
"bugs": {
|
||||
|
345
test/matching.html
Normal file
345
test/matching.html
Normal file
File diff suppressed because one or more lines are too long
73
test/test.js
73
test/test.js
@@ -209,14 +209,6 @@ describe('Add (Sync)', function(){
|
||||
flexsearch_extra.add(3, " - ");
|
||||
|
||||
expect(flexsearch_extra.length).to.equal(0);
|
||||
|
||||
flexsearch_extra.add(4, "Thomas");
|
||||
flexsearch_extra.add(5, "Arithmetic");
|
||||
flexsearch_extra.add(6, "Mahagoni");
|
||||
|
||||
expect(flexsearch_extra.search("tomass")).to.include(4);
|
||||
expect(flexsearch_extra.search("arytmetik")).to.include(5);
|
||||
expect(flexsearch_extra.search("mahagony")).to.include(6);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -230,6 +222,14 @@ describe('Search (Sync)', function(){
|
||||
|
||||
expect(flexsearch_sync.search("foo foo")).to.have.members([0, 1]);
|
||||
expect(flexsearch_sync.search("foo foo")).to.have.members([0, 1]);
|
||||
|
||||
flexsearch_extra.add(4, "Thomas");
|
||||
flexsearch_extra.add(5, "Arithmetic");
|
||||
flexsearch_extra.add(6, "Mahagoni");
|
||||
|
||||
expect(flexsearch_extra.search("tomass")).to.include(4);
|
||||
expect(flexsearch_extra.search("arytmetik")).to.include(5);
|
||||
expect(flexsearch_extra.search("mahagony")).to.include(6);
|
||||
});
|
||||
|
||||
it('Should have been limited', function(){
|
||||
@@ -914,6 +914,63 @@ describe('Options', function(){
|
||||
});
|
||||
});
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// Relevance Tests
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
describe('Relevance', function(){
|
||||
|
||||
it('Should have been sorted by relevance properly', function(){
|
||||
|
||||
var index = new FlexSearch({
|
||||
encode: 'advanced',
|
||||
mode: 'strict'
|
||||
});
|
||||
|
||||
index.add(0, "1 2 3 2 4 1 5 3");
|
||||
index.add(1, "zero one two three four five six seven eight nine ten");
|
||||
index.add(2, "four two zero one three ten five seven eight six nine");
|
||||
|
||||
expect(index.search("1")).to.have.members([0]);
|
||||
expect(index.search("one")).to.have.members([1, 2]);
|
||||
expect(index.search("one two")).to.have.members([1, 2]);
|
||||
expect(index.search("four one")).to.have.members([1, 2]);
|
||||
|
||||
var index = new FlexSearch({
|
||||
encode: 'advanced',
|
||||
mode: 'strict',
|
||||
threshold: 5,
|
||||
depth: 3
|
||||
});
|
||||
|
||||
index.add(0, "1 2 3 2 4 1 5 3");
|
||||
index.add(1, "zero one two three four five six seven eight nine ten");
|
||||
index.add(2, "four two zero one three ten five seven eight six nine");
|
||||
|
||||
expect(index.search("1")).to.have.members([0]);
|
||||
expect(index.search("one")).to.have.members([1, 2]);
|
||||
expect(index.search("one two")).to.have.members([1, 2]);
|
||||
expect(index.search("four one")).to.have.members([1, 2]);
|
||||
|
||||
var index = new FlexSearch({
|
||||
encode: 'extra',
|
||||
mode: 'ngram',
|
||||
threshold: 5,
|
||||
depth: 3
|
||||
});
|
||||
|
||||
index.add(0, "1 2 3 2 4 1 5 3");
|
||||
index.add(1, "zero one two three four five six seven eight nine ten");
|
||||
index.add(2, "four two zero one three ten five seven eight six nine");
|
||||
|
||||
expect(index.search("1 3 4")).to.have.members([0]);
|
||||
expect(index.search("1 5 3 4")).to.have.members([0]);
|
||||
expect(index.search("one")).to.have.members([1, 2]);
|
||||
expect(index.search("one two")).to.have.members([1, 2]);
|
||||
expect(index.search("four one")).to.have.members([1, 2]);
|
||||
});
|
||||
});
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// Feature Tests
|
||||
// ------------------------------------------------------------------------
|
||||
|
Reference in New Issue
Block a user