Passes over every char (utf-8 char) on word and recursively adds it to the internal `data` trie representation. The special key `""` in `self._termination_char` is used to represent termination. This function is idempotent, adding twice the same word will leave the trie unc
(self, word: str)
| 73 | self.add(token) |
| 74 | |
| 75 | def add(self, word: str): |
| 76 | """ |
| 77 | Passes over every char (utf-8 char) on word and recursively adds it to the internal `data` trie representation. |
| 78 | The special key `""` in `self._termination_char` is used to represent termination. |
| 79 | |
| 80 | This function is idempotent, adding twice the same word will leave the trie unchanged |
| 81 | |
| 82 | Example: |
| 83 | |
| 84 | ```python |
| 85 | >>> trie = Trie() |
| 86 | >>> trie.add("Hello 友達") |
| 87 | >>> trie.data |
| 88 | {"H": {"e": {"l": {"l": {"o": {" ": {"友": {"達": {"": 1}}}}}}}}} |
| 89 | |
| 90 | >>> trie.add("Hello") |
| 91 | >>> trie.data |
| 92 | {"H": {"e": {"l": {"l": {"o": {"": 1, " ": {"友": {"達": {"": 1}}}}}}}}} |
| 93 | ``` |
| 94 | """ |
| 95 | if not word: |
| 96 | # Prevent empty string |
| 97 | return |
| 98 | |
| 99 | self._tokens.add(word) |
| 100 | ref = self.data |
| 101 | for char in word: |
| 102 | ref[char] = ref.setdefault(char, {}) |
| 103 | ref = ref[char] |
| 104 | ref[self._termination_char] = 1 |
| 105 | |
| 106 | def split(self, text: str) -> List[str]: |
| 107 | """ |