UNPKG

@a-s8h/liblevenshtein

Version:

Various utilities regarding Levenshtein transducers.

47 lines (39 loc) 1.33 kB
// Generated by CoffeeScript 1.7.1 /* Takes a list of arity-1, mutation functions and their corresponding probabilities, the maximum number of mutations to generate, an arity-0 function for generating random numbers between 0 and 1, and a target to apply mutations to, and returns the target, transformed according to a (pseudo-)random series of mutations. */ (function() { var global, mutate; mutate = function(mutations, max_mutations, random, target) { var i, j, mutation, num_mutations, p, p_sum, rand, _i, _len, _ref; p_sum = 0; for (_i = 0, _len = mutations.length; _i < _len; _i++) { _ref = mutations[_i], p = _ref[0], mutation = _ref[1]; p_sum += p; } i = -1; while ((++i) < mutations.length) { mutations[i][0] /= p_sum; } num_mutations = random() * max_mutations; i = -1; while ((++i) < num_mutations) { rand = random(); p_sum = 0; j = 0; while (mutations[j][0] + p_sum < rand) { p_sum += mutations[j++][0]; } mutation = mutations[j][1]; target = mutation(target); } return target; }; global = typeof exports === 'object' ? exports : typeof window === 'object' ? window : this; global['levenshtein'] || (global['levenshtein'] = {}); global['levenshtein']['mutate'] = mutate; }).call(this);