Download utils/trie.py from yuanzf/timemachine: direct link, hf CLI and curl.
- Browser
- Download file 718 Bytes
-
https://huggingface.co/yuanzf/timemachine/resolve/main/utils/trie.py
- Command line
-
hf download hf://yuanzf/timemachine/utils/trie.py
-
curl -L -o trie.py https://huggingface.co/yuanzf/timemachine/resolve/main/utils/trie.py
718 Bytes
| # Copyright 2022 The OFA-Sys Team. | |
| # All rights reserved. | |
| # This source code is licensed under the Apache 2.0 license | |
| # found in the LICENSE file in the root directory. | |
| from collections import defaultdict | |
| class TreeNode(): | |
| def __init__(self): | |
| self.child = defaultdict(TreeNode) | |
| class Trie: | |
| def __init__(self, eos): | |
| self.root = TreeNode() | |
| self.eos = eos | |
| def insert(self, word): | |
| cur = self.root | |
| for c in word: | |
| cur = cur.child[c] | |
| def get_next_layer(self, word): | |
| cur = self.root | |
| for c in word: | |
| cur = cur.child.get(c) | |
| if cur is None: | |
| return [self.eos] | |
| return list(cur.child.keys()) | |