Download src/tokenizer.py from Suburst/NJU_Final_Transformer_Tanslation: direct link, hf CLI and curl.
- Browser
- Download file 422 Bytes
-
https://huggingface.co/spaces/Suburst/NJU_Final_Transformer_Tanslation/resolve/main/src/tokenizer.py
- Command line
-
hf download hf://spaces/Suburst/NJU_Final_Transformer_Tanslation/src/tokenizer.py
-
curl -L -o tokenizer.py https://huggingface.co/spaces/Suburst/NJU_Final_Transformer_Tanslation/resolve/main/src/tokenizer.py
422 Bytes
| import json | |
| class Tokenizer: | |
| def __init__(self, word2int_path, int2word_path): | |
| with open(word2int_path, 'r') as f: | |
| self.word2int = json.load(f) | |
| with open(int2word_path, 'r') as f: | |
| self.int2word = {int(k): v for k, v in json.load(f).items()} | |
| def encode(self, word): | |
| return self.word2int[word] | |
| def decode(self, int_val): | |
| return self.int2word[int_val] | |