Commit 5f6cdc3a by Jeroen F.J. Laros

Refactoring.

parent bbc2a94f
 def _fill(node, alphabet, length): """Make a full trie using the characters in {alphabet}. def _add(root, word): """Add a word to the trie. :arg dict node: Current node. :arg tuple alphabet: Used alphabet. :arg int length: Length of the words to be generated. :arg dict root: Root of the trie. :arg str word: A word. """ node = root :returns iter: Trie containing all words of length {length} over alphabet {alphabet}. for char in word: if char not in node: node[char] = {} node = node[char] node[''] = {} def _find(root, word): """Find the node after following the path in the trie given by {word}. :arg dict root: Root of the trie. :arg str word: A word. :returns dict: The node if found, {} otherwise. """ if not length: node[''] = {} return node = root for char in word: if char not in node: return {} node = node[char] for car in alphabet: node[car] = {} _fill(node[car], alphabet, length - 1) return node def _remove(node, word): ... ... @@ -43,7 +58,7 @@ def _remove(node, word): return result def _to_list(path, node): def _iterate(path, node): """Convert a trie into a list. :arg str path: Path taken so far to reach the current node. ... ... @@ -54,11 +69,30 @@ def _to_list(path, node): if '' in node: yield path for car in node: for result in _to_list(path + car, node[car]): for char in node: for result in _iterate(path + char, node[char]): yield result def _fill(node, alphabet, length): """Make a full trie using the characters in {alphabet}. :arg dict node: Current node. :arg tuple alphabet: Used alphabet. :arg int length: Length of the words to be generated. :returns iter: Trie containing all words of length {length} over alphabet {alphabet}. """ if not length: node[''] = {} return for char in alphabet: node[char] = {} _fill(node[char], alphabet, length - 1) def _hamming(path, node, word, distance): """Find all paths in the trie that are within a certain hamming distance of {word}. ... ... @@ -129,61 +163,27 @@ class Trie(object): self.root = {} if words: self._build(words) def _build(self, words): """Build the trie. :arg list words: List of words. """ for word in words: self.add(word) def _find(self, word): """Find the node after following the path in the trie given by {word}. :arg str word: A word. :returns dict: The node if found, {} otherwise. """ node = self.root for char in word: if char not in node: return {} node = node[char] return node for word in words: self.add(word) def __contains__(self, word): return '' in self._find(word) def add(self, word): """Add a word to the trie. :arg str word: A word. """ node = self.root return '' in _find(self.root, word) for char in word: if char not in node: node[char] = {} node = node[char] def __iter__(self): return _iterate('', self.root) node[''] = {} def add(self, word): _add(self.root, word) def remove(self, word): return _remove(self.root, word) def has_prefix(self, word): return self._find(word) != {} return _find(self.root, word) != {} def fill(self, alphabet, length): _fill(self.root, alphabet, length) def to_list(self): return _to_list('', self.root) def all_hamming(self, word, distance): return _hamming('', self.root, word, distance) ... ... @@ -201,7 +201,7 @@ class Trie(object): :returns str: Best match with {word}. """ if word in self: if _find(self.root, word): return word for i in range(1, distance + 1): ... ... @@ -228,7 +228,7 @@ class Trie(object): :returns str: Best match with {word}. """ if word in self: if _find(self.root, word): return word for i in range(1, distance + 1): ... ...
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!