-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
31 lines (23 loc) · 1.01 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import torch
class TextProcess:
aux_vocab = ["<p>", "<s>", "<e>", " ", ":", "'"]
origin_list_vocab = {
"en": aux_vocab + list("abcdefghijklmnopqrstuvwxyz"),
"vi": aux_vocab
+ list(
"abcdefghijklmnopqrstuvwxyzàáâãèéêìíòóôõùúýăđĩũơưạảấầẩẫậắằẳẵặẹẻẽếềểễệỉịọỏốồổỗộớờởỡợụủứừửữựỳỵỷỹ"
),
}
origin_vocab = {
lang: dict(zip(vocab, range(len(vocab))))
for lang, vocab in origin_list_vocab.items()
}
def __init__(self, lang):
self.lang = lang
assert self.lang in ["vi", "en"], "Language not found"
self.vocab = self.origin_vocab[lang]
self.list_vocab = self.origin_list_vocab[lang]
def text2int(self, s: str) -> torch.Tensor:
return torch.Tensor([self.vocab[i] for i in s.lower()])
def int2text(self, s: torch.Tensor) -> str:
return "".join([self.list_vocab[i] for i in s if i > 2])