From e874ffbfc229d28677b509147e4060e8578510e3 Mon Sep 17 00:00:00 2001 From: Luke Slater Date: Wed, 26 Oct 2011 17:19:52 +0100 Subject: [PATCH] spelling --- modules/spelling.js | 39 +++++++++++++++++++++++++++++ snippets.js | 60 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 modules/spelling.js diff --git a/modules/spelling.js b/modules/spelling.js new file mode 100644 index 0000000..ba4b666 --- /dev/null +++ b/modules/spelling.js @@ -0,0 +1,39 @@ +var spelling = function(dbot) { + var dbot = dbot; + var last = {}; + + return { + 'listener': function(data, params) { + var q = data.message.valMatch(/^\*([\d\w\s]*)/, 2); + if(q) { + var correction = q[1]; + var candidates = last[data.channel][data.user].split(' '); + var winner = false; + + for(int i=0;i winner) { + winner = candidates[i]; + } + } + + if(winner < 3) { + var fix = data.message.replace(winner, correction); + dbot.say(data.channel, data.user + ':' + fix); + } + } else { + if(last.hasOwnProperty(data.channel)) { + last[data.channel][data.user] = data.message; + } else { + last[data.channel] = { }; + last[data.channel][data.user] = data.message; + } + } + }, + + 'on': 'PRIVMSG' +} + +exports.fetch = function(dbot) { + return spelling(dbot); +}; diff --git a/snippets.js b/snippets.js index d845ad9..be41eb1 100644 --- a/snippets.js +++ b/snippets.js @@ -46,6 +46,66 @@ String.prototype.startsWith = function(needle) { return needle === this.slice(0, needle.length); }; +String.prototype.distance = function(s1, s2) { + // Calculate Levenshtein distance between two strings + // + // version: 1109.2015 + // discuss at: http://phpjs.org/functions/levenshtein // + original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com) + // + bugfixed by: Onno Marsman + // + revised by: Andrea Giammarchi (http://webreflection.blogspot.com) + // + reimplemented by: Brett Zamir (http://brett-zamir.me) + // + reimplemented by: Alexander M Beedie // * example 1: levenshtein('Kevin van Zonneveld', 'Kevin van Sommeveld'); + // * returns 1: 3 + if (s1 == s2) { + return 0; + } + var s1_len = s1.length; + var s2_len = s2.length; + if (s1_len === 0) { + return s2_len; } + if (s2_len === 0) { + return s1_len; + } + // BEGIN STATIC + var split = false; + try { + split = !('0')[0]; + } catch (e) { split = true; // Earlier IE may not support access by string index + } + // END STATIC + if (split) { + s1 = s1.split(''); s2 = s2.split(''); + } + + var v0 = new Array(s1_len + 1); + var v1 = new Array(s1_len + 1); + var s1_idx = 0, + s2_idx = 0, + cost = 0; + for (s1_idx = 0; s1_idx < s1_len + 1; s1_idx++) { v0[s1_idx] = s1_idx; + } + var char_s1 = '', + char_s2 = ''; + for (s2_idx = 1; s2_idx <= s2_len; s2_idx++) { v1[0] = s2_idx; + char_s2 = s2[s2_idx - 1]; + + for (s1_idx = 0; s1_idx < s1_len; s1_idx++) { + char_s1 = s1[s1_idx]; cost = (char_s1 == char_s2) ? 0 : 1; + var m_min = v0[s1_idx + 1] + 1; + var b = v1[s1_idx] + 1; + var c = v0[s1_idx] + cost; + if (b < m_min) { m_min = b; + } + if (c < m_min) { + m_min = c; + } v1[s1_idx + 1] = m_min; + } + var v_tmp = v0; + v0 = v1; + v1 = v_tmp; } + return v0[s1_len]; +} + /*** Object ***/ Object.prototype.isFunction = function(obj) {