Commit c699ee6b authored by Jeroen F.J. Laros's avatar Jeroen F.J. Laros

Fixed bug in Levenshtein function.

parent c4696045
...@@ -136,19 +136,21 @@ def _levenshtein(path, node, word, distance): ...@@ -136,19 +136,21 @@ def _levenshtein(path, node, word, distance):
if not word: if not word:
if '' in node: if '' in node:
yield path yield path
return car, cdr = '', ''
else:
car, cdr = word[0], word[1:] car, cdr = word[0], word[1:]
# Deletion. # Deletion.
for result in _levenshtein(path, node, cdr, distance - 1): for result in _levenshtein(path, node, cdr, distance - 1):
yield result yield result
for char in node: for char in node:
# Substitution and insertion. # Substitution.
for result in _levenshtein( if car:
path + char, node[char], cdr, distance - int(char != car)): for result in _levenshtein(
yield result path + char, node[char], cdr, distance - int(char != car)):
yield result
# Insertion.
for result in _levenshtein( for result in _levenshtein(
path + char, node[char], word, distance - 1): path + char, node[char], word, distance - 1):
yield result yield result
......
...@@ -116,5 +116,8 @@ class TestTrie(object): ...@@ -116,5 +116,8 @@ class TestTrie(object):
def test_levenshtein_1_del(self): def test_levenshtein_1_del(self):
assert self._trie.levenshtein('ac', 1) == 'abc' assert self._trie.levenshtein('ac', 1) == 'abc'
def test_levenshtein_1_prefex(self):
assert self._trie.levenshtein('ab', 1) == 'abc'
def test_levenshtein_1_ins(self): def test_levenshtein_1_ins(self):
assert self._trie.levenshtein('abbc', 1) == 'abc' assert self._trie.levenshtein('abbc', 1) == 'abc'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment