-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer.py
58 lines (41 loc) · 1.39 KB
/
tokenizer.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from logging import getLogger
from typing import List
import sentencepiece as spm
import jieba
logger = getLogger()
class JiebaTokenizer(object):
def __init__(self, model_file, max_len=None):
self.max_len = max_len if max_len is not None else int(1e12)
self.sp = spm.SentencePieceProcessor(model_file=model_file)
self.translator = str.maketrans(" \n", "\u2582\u2583")
self.pad_id = self.sp.pad_id()
@property
def vocab_size(self):
return self.sp.vocab_size()
def __len__(self):
return self.sp.vocab_size()
@property
def eod(self):
return self.eod_id
def bos_id(self):
return self.sp.bos_id()
def eos_id(self):
return self.sp.eos_id()
def eod_id(self):
return self.sp.eod_id()
def tokenize(self, text):
""" Tokenize a string. """
seg_list = [x.translate(self.translator) for x in jieba.cut(text)]
new_seg = " ".join(seg_list)
return self.sp.encode(new_seg)
def convert_tokens_to_ids(self, tokens):
return tokens
def convert_ids_to_tokens(self, ids):
return self.decode(ids)
def encode(self, text):
res = self.tokenize(text)
return res
def decode(self, tokens):
text = self.sp.decode(tokens)
text = text.replace(' ', '').replace('\u2582', ' ').replace('\u2583', '\n')
return text