@a-s8h/liblevenshtein
Version:
Various utilities regarding Levenshtein transducers.
56 lines (51 loc) • 1.76 kB
JavaScript
// Generated by CoffeeScript 1.7.1
(function() {
var global;
global = typeof exports === 'object' ? exports : typeof window === 'object' ? window : this;
global['levenshtein'] || (global['levenshtein'] = {});
global['levenshtein']['operations'] = {
insertion: function(term, alphabet, random) {
var c, i;
c = alphabet[(random() * alphabet.length) >> 0];
i = (random() * (1 + term.length)) >> 0;
return term.slice(0, i) + c + term.slice(1 + i);
},
deletion: function(term, alphabet, random) {
var i;
i = (random() * term.length) >> 0;
return term.slice(0, i) + term.slice(i + 1);
},
substitution: function(term, alphabet, random) {
var c, i;
c = alphabet[(random() * alphabet.length) >> 0];
i = (random() * term.length) >> 0;
return term.slice(0, i) + c + term.slice(i + 1);
},
transposition: function(term, alphabet, random) {
var i;
if (term.length > 1) {
i = (random() * (term.length - 1)) >> 0;
return term.slice(0, i) + term[i + 1] + term[i] + term.slice(i + 2);
} else {
return term;
}
},
merge: function(term, alphabet, random) {
var c, i;
if (term.length > 1) {
c = alphabet[(random() * alphabet.length) >> 0];
i = (random() * (term.length - 1)) >> 0;
return term.slice(0, i) + c + term.slice(i + 2);
} else {
return term;
}
},
split: function(term, alphabet, random) {
var c, d, i;
c = alphabet[(random() * alphabet.length) >> 0];
d = alphabet[(random() * alphabet.length) >> 0];
i = (random() * (term.length - 1)) >> 0;
return term.slice(0, i) + c + d + term.slice(i + 1);
}
};
}).call(this);