Add lunr library for toolbox (and potentially more things in the future) search.

This commit is contained in:
Dannon Baker
2015-02-05 08:12:54 -05:00
parent caa8cd0a56
commit e98f5e0e51
5 changed files with 3887 additions and 3 deletions
+2 -2
View File
@@ -69,11 +69,11 @@ module.exports = function(grunt) {
libraryLocations : {
"jquery": [ "dist/jquery.js", "jquery/jquery.js" ],
"jquery-migrate": [ "jquery-migrate.js", "jquery/jquery.migrate.js" ],
"traceKit": [ "tracekit.js", "tracekit.js" ],
"ravenjs": [ "dist/raven.js", "raven.js" ],
"underscore": [ "underscore.js", "underscore.js" ],
"handlebars": [ "handlebars.runtime.js", "handlebars.runtime.js" ]
"handlebars": [ "handlebars.runtime.js", "handlebars.runtime.js" ],
"lunr.js": [ "lunr.js", "lunr.js" ]
//"backbone": [ "backbone.js", "backbone/backbone.js" ],
// these need to be updated and tested
+2 -1
View File
@@ -32,7 +32,8 @@
"jquery-ui": "git://github.com/jquery/jquery-ui.git#~1.11.2",
"jquery.event.drag-drop": "~2.2.1",
"handlebars": "~2.0.0",
"jquery-migrate": "~1.2.1"
"jquery-migrate": "~1.2.1",
"lunr.js": "git://github.com/olivernn/lunr.js.git#~0.5.7"
},
"resolutions": {
"jquery": "~1.11.1"
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+63
View File
@@ -0,0 +1,63 @@
(function(){var a=function(c){var b=new a.Index;b.pipeline.add(a.trimmer,a.stopWordFilter,a.stemmer);if(c){c.call(b,b)}return b};a.version="0.5.7"
/*
* lunr.utils
* Copyright (C) 2014 Oliver Nightingale
*/
;a.utils={};a.utils.warn=(function(b){return function(c){if(b.console&&console.warn){console.warn(c)}}})(this)
/*
* lunr.EventEmitter
* Copyright (C) 2014 Oliver Nightingale
*/
;a.EventEmitter=function(){this.events={}};a.EventEmitter.prototype.addListener=function(){var b=Array.prototype.slice.call(arguments),c=b.pop(),d=b;if(typeof c!=="function"){throw new TypeError("last argument must be a function")}d.forEach(function(e){if(!this.hasHandler(e)){this.events[e]=[]}this.events[e].push(c)},this)};a.EventEmitter.prototype.removeListener=function(c,d){if(!this.hasHandler(c)){return}var b=this.events[c].indexOf(d);this.events[c].splice(b,1);if(!this.events[c].length){delete this.events[c]}};a.EventEmitter.prototype.emit=function(c){if(!this.hasHandler(c)){return}var b=Array.prototype.slice.call(arguments,1);this.events[c].forEach(function(d){d.apply(undefined,b)})};a.EventEmitter.prototype.hasHandler=function(b){return b in this.events}
/*
* lunr.tokenizer
* Copyright (C) 2014 Oliver Nightingale
*/
;a.tokenizer=function(c){if(!arguments.length||c==null||c==undefined){return[]}if(Array.isArray(c)){return c.map(function(e){return e.toLowerCase()})}var d=c.toString().replace(/^\s+/,"");for(var b=d.length-1;b>=0;b--){if(/\S/.test(d.charAt(b))){d=d.substring(0,b+1);break}}return d.split(/(?:\s+|\-)/).filter(function(e){return !!e}).map(function(e){return e.toLowerCase()})}
/*
* lunr.Pipeline
* Copyright (C) 2014 Oliver Nightingale
*/
;a.Pipeline=function(){this._stack=[]};a.Pipeline.registeredFunctions={};a.Pipeline.registerFunction=function(c,b){if(b in this.registeredFunctions){a.utils.warn("Overwriting existing registered function: "+b)}c.label=b;a.Pipeline.registeredFunctions[c.label]=c};a.Pipeline.warnIfFunctionNotRegistered=function(b){var c=b.label&&(b.label in this.registeredFunctions);if(!c){a.utils.warn("Function is not registered with pipeline. This may cause problems when serialising the index.\n",b)}};a.Pipeline.load=function(c){var b=new a.Pipeline;c.forEach(function(e){var d=a.Pipeline.registeredFunctions[e];if(d){b.add(d)}else{throw new Error("Cannot load un-registered function: "+e)}});return b};a.Pipeline.prototype.add=function(){var b=Array.prototype.slice.call(arguments);b.forEach(function(c){a.Pipeline.warnIfFunctionNotRegistered(c);this._stack.push(c)},this)};a.Pipeline.prototype.after=function(b,c){a.Pipeline.warnIfFunctionNotRegistered(c);var d=this._stack.indexOf(b)+1;this._stack.splice(d,0,c)};a.Pipeline.prototype.before=function(b,c){a.Pipeline.warnIfFunctionNotRegistered(c);var d=this._stack.indexOf(b);this._stack.splice(d,0,c)};a.Pipeline.prototype.remove=function(b){var c=this._stack.indexOf(b);this._stack.splice(c,1)};a.Pipeline.prototype.run=function(f){var c=[],h=f.length,g=this._stack.length;for(var e=0;e<h;e++){var d=f[e];for(var b=0;b<g;b++){d=this._stack[b](d,e,f);if(d===void 0){break}}if(d!==void 0){c.push(d)}}return c};a.Pipeline.prototype.reset=function(){this._stack=[]};a.Pipeline.prototype.toJSON=function(){return this._stack.map(function(b){a.Pipeline.warnIfFunctionNotRegistered(b);return b.label})}
/*
* lunr.Vector
* Copyright (C) 2014 Oliver Nightingale
*/
;a.Vector=function(){this._magnitude=null;this.list=undefined;this.length=0};a.Vector.Node=function(b,d,c){this.idx=b;this.val=d;this.next=c};a.Vector.prototype.insert=function(b,f){var e=this.list;if(!e){this.list=new a.Vector.Node(b,f,e);return this.length++}var d=e,c=e.next;while(c!=undefined){if(b<c.idx){d.next=new a.Vector.Node(b,f,c);return this.length++}d=c,c=c.next}d.next=new a.Vector.Node(b,f,c);return this.length++};a.Vector.prototype.magnitude=function(){if(this._magniture){return this._magnitude}var c=this.list,b=0,d;while(c){d=c.val;b+=d*d;c=c.next}return this._magnitude=Math.sqrt(b)};a.Vector.prototype.dot=function(e){var d=this.list,c=e.list,b=0;while(d&&c){if(d.idx<c.idx){d=d.next}else{if(d.idx>c.idx){c=c.next}else{b+=d.val*c.val;d=d.next;c=c.next}}}return b};a.Vector.prototype.similarity=function(b){return this.dot(b)/(this.magnitude()*b.magnitude())}
/*
* lunr.SortedSet
* Copyright (C) 2014 Oliver Nightingale
*/
;a.SortedSet=function(){this.length=0;this.elements=[]};a.SortedSet.load=function(b){var c=new this;c.elements=b;c.length=b.length;return c};a.SortedSet.prototype.add=function(){Array.prototype.slice.call(arguments).forEach(function(b){if(~this.indexOf(b)){return}this.elements.splice(this.locationFor(b),0,b)},this);this.length=this.elements.length};a.SortedSet.prototype.toArray=function(){return this.elements.slice()};a.SortedSet.prototype.map=function(c,b){return this.elements.map(c,b)};a.SortedSet.prototype.forEach=function(c,b){return this.elements.forEach(c,b)};a.SortedSet.prototype.indexOf=function(f,g,d){var g=g||0,d=d||this.elements.length,c=d-g,b=g+Math.floor(c/2),e=this.elements[b];if(c<=1){if(e===f){return b}else{return -1}}if(e<f){return this.indexOf(f,b,d)}if(e>f){return this.indexOf(f,g,b)}if(e===f){return b}};a.SortedSet.prototype.locationFor=function(f,g,d){var g=g||0,d=d||this.elements.length,c=d-g,b=g+Math.floor(c/2),e=this.elements[b];if(c<=1){if(e>f){return b}if(e<f){return b+1}}if(e<f){return this.locationFor(f,b,d)}if(e>f){return this.locationFor(f,g,b)}};a.SortedSet.prototype.intersect=function(g){var l=new a.SortedSet,h=0,f=0,d=this.length,k=g.length,e=this.elements,c=g.elements;while(true){if(h>d-1||f>k-1){break}if(e[h]===c[f]){l.add(e[h]);h++,f++;continue}if(e[h]<c[f]){h++;continue}if(e[h]>c[f]){f++;continue}}return l};a.SortedSet.prototype.clone=function(){var b=new a.SortedSet;b.elements=this.toArray();b.length=b.elements.length;return b};a.SortedSet.prototype.union=function(b){var c,e,d;if(this.length>=b.length){c=this,e=b}else{c=b,e=this}d=c.clone();d.add.apply(d,e.toArray());return d};a.SortedSet.prototype.toJSON=function(){return this.toArray()}
/*
* lunr.Index
* Copyright (C) 2014 Oliver Nightingale
*/
;a.Index=function(){this._fields=[];this._ref="id";this.pipeline=new a.Pipeline;this.documentStore=new a.Store;this.tokenStore=new a.TokenStore;this.corpusTokens=new a.SortedSet;this.eventEmitter=new a.EventEmitter;this._idfCache={};this.on("add","remove","update",(function(){this._idfCache={}}).bind(this))};a.Index.prototype.on=function(){var b=Array.prototype.slice.call(arguments);return this.eventEmitter.addListener.apply(this.eventEmitter,b)};a.Index.prototype.off=function(b,c){return this.eventEmitter.removeListener(b,c)};a.Index.load=function(c){if(c.version!==a.version){a.utils.warn("version mismatch: current "+a.version+" importing "+c.version)}var b=new this;b._fields=c.fields;b._ref=c.ref;b.documentStore=a.Store.load(c.documentStore);b.tokenStore=a.TokenStore.load(c.tokenStore);b.corpusTokens=a.SortedSet.load(c.corpusTokens);b.pipeline=a.Pipeline.load(c.pipeline);return b};a.Index.prototype.field=function(d,b){var b=b||{},c={name:d,boost:b.boost||1};this._fields.push(c);return this};a.Index.prototype.ref=function(b){this._ref=b;return this};a.Index.prototype.add=function(h,g){var b={},f=new a.SortedSet,j=h[this._ref],g=g===undefined?true:g;this._fields.forEach(function(k){var i=this.pipeline.run(a.tokenizer(h[k.name]));b[k.name]=i;a.SortedSet.prototype.add.apply(f,i)},this);this.documentStore.set(j,f);a.SortedSet.prototype.add.apply(this.corpusTokens,f.toArray());for(var d=0;d<f.length;d++){var c=f.elements[d];var e=this._fields.reduce(function(i,l){var m=b[l.name].length;if(!m){return i}var k=b[l.name].filter(function(n){return n===c}).length;return i+(k/m*l.boost)},0);this.tokenStore.add(c,{ref:j,tf:e})}if(g){this.eventEmitter.emit("add",h,this)}};a.Index.prototype.remove=function(d,c){var e=d[this._ref],c=c===undefined?true:c;if(!this.documentStore.has(e)){return}var b=this.documentStore.get(e);this.documentStore.remove(e);b.forEach(function(f){this.tokenStore.remove(f,e)},this);if(c){this.eventEmitter.emit("remove",d,this)}};a.Index.prototype.update=function(c,b){var b=b===undefined?true:b;this.remove(c,false);this.add(c,false);if(b){this.eventEmitter.emit("update",c,this)}};a.Index.prototype.idf=function(c){var e="@"+c;if(Object.prototype.hasOwnProperty.call(this._idfCache,e)){return this._idfCache[e]}var b=this.tokenStore.count(c),d=1;if(b>0){d=1+Math.log(this.tokenStore.length/b)}return this._idfCache[e]=d};a.Index.prototype.search=function(f){var e=this.pipeline.run(a.tokenizer(f)),g=new a.Vector,b=[],d=this._fields.reduce(function(i,j){return i+j.boost},0);var h=e.some(function(i){return this.tokenStore.has(i)},this);if(!h){return[]}e.forEach(function(l,k,n){var m=1/n.length*this._fields.length*d,j=this;var o=this.tokenStore.expand(l).reduce(function(i,p){var u=j.corpusTokens.indexOf(p),q=j.idf(p),s=1,t=new a.SortedSet;if(p!==l){var r=Math.max(3,p.length-l.length);s=1/Math.log(r)}if(u>-1){g.insert(u,m*q*s)}Object.keys(j.tokenStore.get(p)).forEach(function(v){t.add(v)});return i.union(t)},new a.SortedSet);b.push(o)},this);var c=b.reduce(function(i,j){return i.intersect(j)});return c.map(function(i){return{ref:i,score:g.similarity(this.documentVector(i))}},this).sort(function(j,i){return i.score-j.score})};a.Index.prototype.documentVector=function(j){var d=this.documentStore.get(j),h=d.length,g=new a.Vector;for(var c=0;c<h;c++){var b=d.elements[c],f=this.tokenStore.get(b)[j].tf,e=this.idf(b);g.insert(this.corpusTokens.indexOf(b),f*e)}return g};a.Index.prototype.toJSON=function(){return{version:a.version,fields:this._fields,ref:this._ref,documentStore:this.documentStore.toJSON(),tokenStore:this.tokenStore.toJSON(),corpusTokens:this.corpusTokens.toJSON(),pipeline:this.pipeline.toJSON()}};a.Index.prototype.use=function(c){var b=Array.prototype.slice.call(arguments,1);b.unshift(this);c.apply(this,b)}
/*
* lunr.Store
* Copyright (C) 2014 Oliver Nightingale
*/
;a.Store=function(){this.store={};this.length=0};a.Store.load=function(c){var b=new this;b.length=c.length;b.store=Object.keys(c.store).reduce(function(d,e){d[e]=a.SortedSet.load(c.store[e]);return d},{});return b};a.Store.prototype.set=function(c,b){if(!this.has(c)){this.length++}this.store[c]=b};a.Store.prototype.get=function(b){return this.store[b]};a.Store.prototype.has=function(b){return b in this.store};a.Store.prototype.remove=function(b){if(!this.has(b)){return}delete this.store[b];this.length--};a.Store.prototype.toJSON=function(){return{store:this.store,length:this.length}}
/*
* lunr.stemmer
* Copyright (C) 2014 Oliver Nightingale
* Includes code from - http://tartarus.org/~martin/PorterStemmer/js.txt
*/
;a.stemmer=(function(){var j={ational:"ate",tional:"tion",enci:"ence",anci:"ance",izer:"ize",bli:"ble",alli:"al",entli:"ent",eli:"e",ousli:"ous",ization:"ize",ation:"ate",ator:"ate",alism:"al",iveness:"ive",fulness:"ful",ousness:"ous",aliti:"al",iviti:"ive",biliti:"ble",logi:"log"},y={icate:"ic",ative:"",alize:"al",iciti:"ic",ical:"ic",ful:"",ness:""},B="[^aeiou]",q="[aeiouy]",w=B+"[^aeiouy]*",k=q+"[aeiou]*",I="^("+w+")?"+k+w,s="^("+w+")?"+k+w+"("+k+")?$",H="^("+w+")?"+k+w+k+w,o="^("+w+")?"+q;var h=new RegExp(I);var f=new RegExp(H);var x=new RegExp(s);var E=new RegExp(o);var u=/^(.+?)(ss|i)es$/;var G=/^(.+?)([^s])s$/;var t=/^(.+?)eed$/;var D=/^(.+?)(ed|ing)$/;var A=/.$/;var l=/(at|bl|iz)$/;var n=new RegExp("([^aeiouylsz])\\1$");var p=new RegExp("^"+w+q+"[^aeiouwxy]$");var r=/^(.+?[^aeiou])y$/;var i=/^(.+?)(ational|tional|enci|anci|izer|bli|alli|entli|eli|ousli|ization|ation|ator|alism|iveness|fulness|ousness|aliti|iviti|biliti|logi)$/;var g=/^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$/;var e=/^(.+?)(al|ance|ence|er|ic|able|ible|ant|ement|ment|ent|ou|ism|ate|iti|ous|ive|ize)$/;var m=/^(.+?)(s|t)(ion)$/;var b=/^(.+?)e$/;var d=/ll$/;var z=new RegExp("^"+w+q+"[^aeiouwxy]$");var F=function F(L){var c,N,M,O,J,C,v;if(L.length<3){return L}M=L.substr(0,1);if(M=="y"){L=M.toUpperCase()+L.substr(1)}O=u;J=G;if(O.test(L)){L=L.replace(O,"$1$2")}else{if(J.test(L)){L=L.replace(J,"$1$2")}}O=t;J=D;if(O.test(L)){var K=O.exec(L);O=h;if(O.test(K[1])){O=A;L=L.replace(O,"")}}else{if(J.test(L)){var K=J.exec(L);c=K[1];J=E;if(J.test(c)){L=c;J=l;C=n;v=p;if(J.test(L)){L=L+"e"}else{if(C.test(L)){O=A;L=L.replace(O,"")}else{if(v.test(L)){L=L+"e"}}}}}}O=r;if(O.test(L)){var K=O.exec(L);c=K[1];L=c+"i"}O=i;if(O.test(L)){var K=O.exec(L);c=K[1];N=K[2];O=h;if(O.test(c)){L=c+j[N]}}O=g;if(O.test(L)){var K=O.exec(L);c=K[1];N=K[2];O=h;if(O.test(c)){L=c+y[N]}}O=e;J=m;if(O.test(L)){var K=O.exec(L);c=K[1];O=f;if(O.test(c)){L=c}}else{if(J.test(L)){var K=J.exec(L);c=K[1]+K[2];J=f;if(J.test(c)){L=c}}}O=b;if(O.test(L)){var K=O.exec(L);c=K[1];O=f;J=x;C=z;if(O.test(c)||(J.test(c)&&!(C.test(c)))){L=c}}O=d;J=f;if(O.test(L)&&J.test(L)){O=A;L=L.replace(O,"")}if(M=="y"){L=M.toLowerCase()+L.substr(1)}return L};return F})();a.Pipeline.registerFunction(a.stemmer,"stemmer")
/*
* lunr.stopWordFilter
* Copyright (C) 2014 Oliver Nightingale
*/
;a.stopWordFilter=function(b){if(a.stopWordFilter.stopWords.indexOf(b)===-1){return b}};a.stopWordFilter.stopWords=new a.SortedSet;a.stopWordFilter.stopWords.length=119;a.stopWordFilter.stopWords.elements=["","a","able","about","across","after","all","almost","also","am","among","an","and","any","are","as","at","be","because","been","but","by","can","cannot","could","dear","did","do","does","either","else","ever","every","for","from","get","got","had","has","have","he","her","hers","him","his","how","however","i","if","in","into","is","it","its","just","least","let","like","likely","may","me","might","most","must","my","neither","no","nor","not","of","off","often","on","only","or","other","our","own","rather","said","say","says","she","should","since","so","some","than","that","the","their","them","then","there","these","they","this","tis","to","too","twas","us","wants","was","we","were","what","when","where","which","while","who","whom","why","will","with","would","yet","you","your"];a.Pipeline.registerFunction(a.stopWordFilter,"stopWordFilter")
/*
* lunr.trimmer
* Copyright (C) 2014 Oliver Nightingale
*/
;a.trimmer=function(b){return b.replace(/^\W+/,"").replace(/\W+$/,"")};a.Pipeline.registerFunction(a.trimmer,"trimmer")
/*
* lunr.stemmer
* Copyright (C) 2014 Oliver Nightingale
* Includes code from - http://tartarus.org/~martin/PorterStemmer/js.txt
*/
;a.TokenStore=function(){this.root={docs:{}};this.length=0};a.TokenStore.load=function(c){var b=new this;b.root=c.root;b.length=c.length;return b};a.TokenStore.prototype.add=function(d,f,b){var b=b||this.root,c=d[0],e=d.slice(1);if(!(c in b)){b[c]={docs:{}}}if(e.length===0){b[c].docs[f.ref]=f;this.length+=1;return}else{return this.add(e,f,b[c])}};a.TokenStore.prototype.has=function(c){if(!c){return false}var d=this.root;for(var b=0;b<c.length;b++){if(!d[c[b]]){return false}d=d[c[b]]}return true};a.TokenStore.prototype.getNode=function(c){if(!c){return{}}var d=this.root;for(var b=0;b<c.length;b++){if(!d[c[b]]){return{}}d=d[c[b]]}return d};a.TokenStore.prototype.get=function(c,b){return this.getNode(c,b).docs||{}};a.TokenStore.prototype.count=function(c,b){return Object.keys(this.get(c,b)).length};a.TokenStore.prototype.remove=function(c,e){if(!c){return}var d=this.root;for(var b=0;b<c.length;b++){if(!(c[b] in d)){return}d=d[c[b]]}delete d.docs[e]};a.TokenStore.prototype.expand=function(d,c){var b=this.getNode(d),e=b.docs||{},c=c||[];if(Object.keys(e).length){c.push(d)}Object.keys(b).forEach(function(f){if(f==="docs"){return}c.concat(this.expand(d+f,c))},this);return c};a.TokenStore.prototype.toJSON=function(){return{root:this.root,length:this.length}};(function(b,c){if(typeof define==="function"&&define.amd){define(c)}else{if(typeof exports==="object"){module.exports=c()}else{b.lunr=c()}}}(this,function(){return a}))})();