| 73 | |
| 74 | class Dawg: |
| 75 | def __init__(self): |
| 76 | self.previous_word = "" |
| 77 | self.next_id = 0 |
| 78 | self.root = DawgNode(self) |
| 79 | |
| 80 | # Here is a list of nodes that have not been checked for duplication. |
| 81 | self.unchecked_nodes = [] |
| 82 | |
| 83 | # To deduplicate, maintain a dictionary with |
| 84 | # minimized_nodes[canonical_node] is canonical_node. |
| 85 | # Based on __hash__ and __eq__, minimized_nodes[n] is the |
| 86 | # canonical node equal to n. |
| 87 | # In other words, self.minimized_nodes[x] == x for all nodes found in |
| 88 | # the dict. |
| 89 | self.minimized_nodes = {} |
| 90 | |
| 91 | # word: value mapping |
| 92 | self.data = {} |
| 93 | # value: word mapping |
| 94 | self.inverse = {} |
| 95 | |
| 96 | def insert(self, word, value): |
| 97 | if not all(0 <= ord(c) < 128 for c in word): |