mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-26 20:59:27 +01:00
Added levenshtein distance
This commit is contained in:
parent
c88b43122e
commit
100d0fa016
@ -56,6 +56,7 @@ Commands include:
|
||||
last
|
||||
lastfrom
|
||||
lithp
|
||||
levenshtein
|
||||
pydoc
|
||||
"""
|
||||
|
||||
@ -423,6 +424,15 @@ class FunCommands(callbacks.Privmsg):
|
||||
return
|
||||
irc.error(msg, 'I don\'t remember a message from that person.')
|
||||
|
||||
def levenshtein(self, irc, msg, args):
|
||||
"""<string1> <string2>
|
||||
|
||||
Returns the levenshtein distance (also known as the "edit distance"
|
||||
between <string1> and <string2>
|
||||
"""
|
||||
(s1, s2) = privmsgs.getArgs(args, needed=2)
|
||||
irc.reply(msg, str(utils.distance(s1, s2)))
|
||||
|
||||
modulechars = '%s%s%s' % (string.ascii_letters, string.digits, '_.')
|
||||
def pydoc(self, irc, msg, args):
|
||||
"""<python function>
|
||||
|
20
src/utils.py
20
src/utils.py
@ -152,6 +152,26 @@ def timeElapsed(now, then, leadingZeroes=False, years=True, weeks=True,
|
||||
else:
|
||||
return ' and '.join([', '.join(ret[:-1]), ret[-1]])
|
||||
|
||||
def distance(s, t):
|
||||
n = len(s)
|
||||
m = len(t)
|
||||
if n == 0:
|
||||
return m
|
||||
elif m == 0:
|
||||
return n
|
||||
d = range(n+1)
|
||||
for i in range(len(d)):
|
||||
d[i] = range(m+1)
|
||||
for i in range(1, n+1):
|
||||
cs = s[i-1]
|
||||
for j in range(1, m+1):
|
||||
ct = t[j-1]
|
||||
if cs == ct:
|
||||
cost = 0
|
||||
else:
|
||||
cost = 1
|
||||
d[i][j] = min(d[i-1][j]+1, d[i][j-1]+1, d[i-1][j-1]+cost)
|
||||
return d[n][m]
|
||||
|
||||
|
||||
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||
|
@ -53,3 +53,12 @@ class UtilsTest(unittest.TestCase):
|
||||
s = 'foobar'
|
||||
L = ['f', 'fo', 'foo', 'foob', 'fooba', 'foobar']
|
||||
self.assertEqual(list(utils.eachSubstring(s)), L)
|
||||
|
||||
def testDistance(self):
|
||||
self.assertEqual(utils.distance('', ''), 0)
|
||||
self.assertEqual(utils.distance('a', 'b'), 1)
|
||||
self.assertEqual(utils.distance('a', 'a'), 0)
|
||||
self.assertEqual(utils.distance('foobar', 'jemfinch'), 8)
|
||||
self.assertEqual(utils.distance('a', 'ab'), 1)
|
||||
self.assertEqual(utils.distance('foo', ''), 3)
|
||||
self.assertEqual(utils.distance('', 'foo'), 3)
|
||||
|
Loading…
Reference in New Issue
Block a user