-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsymspell.min.js
19 lines (19 loc) · 4.28 KB
/
symspell.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* SymSpell 0.6.0-19012014
* An implementation of the Symmetric Delete spelling correction algorithm.
* This is fast and language-independent.
*
* Based on C# code and algorithm version 1.6
* Copyright (C) 2012 Wolf Garbe <wolf.garbe@faroo.com>, FAROO Limited
* See: http://blog.faroo.com/2012/06/07/improved-edit-distance-based-spelling-correction/
* and http://blog.faroo.com/2012/06/24/1000x-faster-spelling-correction-source-code-released/
*
* This version was written by Isaac Sukin (@IceCreamYou).
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License,
* version 3.0 (LGPL-3.0) as published by the Free Software Foundation.
* http://www.opensource.org/licenses/LGPL-3.0
*/
!function(a){function b(a){return a.toLowerCase().match(/([\w\d_](-[\w\d_])?('(t|d|s|m|ll|re|ve))?)+/g)}function c(a,b,d){b=(b||0)+1;var e=[],f=a.length,g,h,i;if(f>1)for(g=0;f>g;g++){var k=new j;if(k.term=a.slice(0,g)+a.slice(g+1),k.distance=b,-1===e.indexOf(k)&&(e.push(k),"undefined"!=typeof d&&d>b)){var l=c(k.term,b,d);for(h=0,i=l.length;i>h;h++)-1===e.indexOf(l[h])&&e.push(l[h])}}return e}function d(a,b){for(var c=0,d=a.length;d>c;c++)if(a[c].equals(b))return!0;return!1}function e(a,b,c){c!==SymSpell.Modes.ALL&&a.length&&a[0].distance>b.distance&&(a.length=0),(c===SymSpell.Modes.ALL||!a.length||a[0].distance>=b.distance)&&a.push(b)}function f(a,b){if(!a)return b?b.length:0;if(!b)return a.length;var c=a.length,d=b.length,e=c+d,f=new Array(c+2),g={},h,i;for(h=0;c+2>h;h++)f[h]=new Array(d+2);for(f[0][0]=e,h=0;c>=h;h++)f[h+1][1]=h,f[h+1][0]=e,g[a[h]]=0;for(i=0;d>=i;i++)f[1][i+1]=i,f[0][i+1]=e,g[b[i]]=0;for(h=1;c>=h;h++){var j=0;for(i=1;d>=i;i++){var k=g[b[i-1]],l=j;a[h-1]===b[i-1]?(f[h+1][i+1]=f[h][i],j=i):f[h+1][i+1]=Math.min(f[h][i],Math.min(f[h+1][i],f[h][i+1]))+1,f[h+1][i+1]=Math.min(f[h+1][i+1],f[k]?f[k][l]+(h-k-1)+1+(i-l-1):1/0)}g[a[h-1]]=h}return f[c+1][d+1]}function g(a,b,c){return a.term===c?0:a.distance?b.distance?f(a.term,c):a.distance:b.distance}a.SymSpell=function(a,b){this.maxEditDistance=a||2,this.mode="undefined"==typeof b?SymSpell.Modes.ALL:b,this.dictionary={}},SymSpell.Modes={TOP:0,SMALLEST:1,ALL:2},SymSpell.prototype.addWords=function(a,c,d){c=c||"und",d=d||b;for(var e=d(a),f=0,g=e.length;g>f;f++)this.addWord(e[f],c)},SymSpell.prototype.addWord=function(a,b){var f=new i,g=b+a;if(this.dictionary[g])f=this.dictionary[g];else{f.term=a,this.dictionary[g]=f;for(var h=c(a,0,this.maxEditDistance),k=0,l=h.length;l>k;k++){var m=h[k],n=new j;if(n.term=a,n.distance=m.distance,this.dictionary[b+m.term]){var o=this.dictionary[b+m.term];d(o.suggestions,n)||e(o.suggestions,n,this.mode)}else{var p=new i;p.suggestions.push(n),this.dictionary[b+m.term]=p}}}f.count++},SymSpell.prototype.lookup=function(a,b){function e(){return i=i.sort(function(a,b){var c=a.distance-b.distance;return c?c:b.count-a.count}).filter(function(a){return a.term}),this.mode===SymSpell.Modes.TOP&&i.length>1?[i[0]]:i}b=b||"und";var f=[],h=new j;h.term=a,h.distance=0,f.push(h);for(var i=[];f.length>0;){var l=f.shift();if(this.mode!==SymSpell.Modes.ALL&&i.length&&l.distance>i[0].distance||l.distance>this.maxEditDistance)return e();var m=b+l.term;if(this.dictionary[m]){var n=new k;if(n.term=this.dictionary[m].term,n.count=this.dictionary[m].count,n.distance=l.distance,!d(i,n)&&(i.push(n),this.mode!=SymSpell.Modes.ALL&&!l.distance))return e();for(var o=this.dictionary[m].suggestions,p=0,q=o.length;q>p;p++){var r=o[p];if(!d(i,r)){var s=g(r,l,a);if(this.mode!==SymSpell.Modes.ALL&&i.length)if(i[0].distance>s)i.length=0;else if(s>i[0].distance)continue;if(s<this.maxEditDistance&&this.dictionary[b+r.term]){var t=this.dictionary[b+r.term],u=new k;u.term=t.term,u.count=t.count,u.distance=s,i.push(u)}}}}if(l.distance<this.maxEditDistance)for(var v=c(l.term,l.distance,0),w=0,x=v.length;x>w;w++)d(f,v[w])||f.push(v[w])}return e()};var h=function(){this.term=""};h.prototype.equals=function(a){return a&&"undefined"!=typeof a.term&&a.term==this.term};var i=function(){h.call(this),this.suggestions=[],this.count=0};i.prototype=Object.create(h.prototype);var j=function(){h.call(this),this.distance=0};j.prototype=Object.create(h.prototype);var k=function(){h.call(this),this.distance=0,this.count=0};k.prototype=Object.create(h.prototype)}(window);
//# sourceMappingURL=symspell.min.map