-
Notifications
You must be signed in to change notification settings - Fork 11
/
tokenization_sentencepiece.py
191 lines (155 loc) · 6.37 KB
/
tokenization_sentencepiece.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# coding=utf-8
#
# Copied from: https://github.com/yoheikikuta/bert-japanese/
#
# This file is based on https://github.com/google-research/bert/blob/master/tokenization.py.
# It is changed to use SentencePiece tokenizer for tokenizations.
"""Tokenization classes."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import collections
import re
import unicodedata
import sentencepiece as sp
import six
import tensorflow as tf
def validate_case_matches_checkpoint(do_lower_case, init_checkpoint):
"""Checks whether the casing config is consistent with the checkpoint name."""
# The casing has to be passed in by the user and there is no explicit check
# as to whether it matches the checkpoint. The casing information probably
# should have been stored in the bert_config.json file, but it's not, so
# we have to heuristically detect it to validate.
if not init_checkpoint:
return
m = re.match("^.*?([A-Za-z0-9_-]+)/bert_model.ckpt", init_checkpoint)
if m is None:
return
model_name = m.group(1)
lower_models = [
"uncased_L-24_H-1024_A-16", "uncased_L-12_H-768_A-12",
"multilingual_L-12_H-768_A-12", "chinese_L-12_H-768_A-12"
]
cased_models = [
"cased_L-12_H-768_A-12", "cased_L-24_H-1024_A-16",
"multi_cased_L-12_H-768_A-12"
]
is_bad_config = False
if model_name in lower_models and not do_lower_case:
is_bad_config = True
actual_flag = "False"
case_name = "lowercased"
opposite_flag = "True"
if model_name in cased_models and do_lower_case:
is_bad_config = True
actual_flag = "True"
case_name = "cased"
opposite_flag = "False"
if is_bad_config:
raise ValueError(
"You passed in `--do_lower_case=%s` with `--init_checkpoint=%s`. "
"However, `%s` seems to be a %s model, so you "
"should pass in `--do_lower_case=%s` so that the fine-tuning matches "
"how the model was pre-training. If this error is wrong, please "
"just comment out this check." % (actual_flag, init_checkpoint,
model_name, case_name, opposite_flag))
def convert_to_unicode(text):
"""Converts `text` to Unicode (if it's not already), assuming utf-8 input."""
if six.PY3:
if isinstance(text, str):
return text
elif isinstance(text, bytes):
return text.decode("utf-8", "ignore")
else:
raise ValueError("Unsupported string type: %s" % (type(text)))
elif six.PY2:
if isinstance(text, str):
return text.decode("utf-8", "ignore")
elif isinstance(text, unicode):
return text
else:
raise ValueError("Unsupported string type: %s" % (type(text)))
else:
raise ValueError("Not running on Python2 or Python 3?")
def printable_text(text):
"""Returns text encoded in a way suitable for print or `tf.logging`."""
# These functions want `str` for both Python2 and Python3, but in one case
# it's a Unicode string and in the other it's a byte string.
if six.PY3:
if isinstance(text, str):
return text
elif isinstance(text, bytes):
return text.decode("utf-8", "ignore")
else:
raise ValueError("Unsupported string type: %s" % (type(text)))
elif six.PY2:
if isinstance(text, str):
return text
elif isinstance(text, unicode):
return text.encode("utf-8")
else:
raise ValueError("Unsupported string type: %s" % (type(text)))
else:
raise ValueError("Not running on Python2 or Python 3?")
def load_vocab(vocab_file):
"""Loads a vocabulary file into a dictionary."""
vocab = collections.OrderedDict()
index = 0
with tf.gfile.GFile(vocab_file, "r") as reader:
while True:
token = convert_to_unicode(reader.readline())
if not token:
break
token, _ = token.split("\t")
token = token.strip()
vocab[token] = index
index += 1
return vocab
def convert_by_vocab(vocab, items, unk_info):
"""Converts a sequence of [tokens|ids] using the vocab."""
output = []
for item in items:
if item in vocab:
output.append(vocab[item])
else:
output.append(unk_info)
return output
def convert_tokens_to_ids(vocab, tokens):
"""Id of <unk> is assumed as 0 accroding to sentencepiece"""
return convert_by_vocab(vocab, tokens, unk_info=0)
def convert_ids_to_tokens(inv_vocab, ids):
"""Token of unknown word is assumed as <unk> according to sentencepiece"""
return convert_by_vocab(inv_vocab, ids, unk_info="<unk>")
class FullTokenizer(object):
"""Runs end-to-end tokenziation."""
def __init__(self, model_file, vocab_file, do_lower_case=True):
self.tokenizer = SentencePieceTokenizer(model_file, do_lower_case=do_lower_case)
self.vocab = load_vocab(vocab_file)
self.inv_vocab = {v: k for k, v in self.vocab.items()}
def tokenize(self, text):
split_tokens = self.tokenizer.tokenize(text)
return split_tokens
def convert_tokens_to_ids(self, tokens):
"""Id of <unk> is assumed as 0 accroding to sentencepiece"""
return convert_by_vocab(self.vocab, tokens, unk_info=0)
def convert_ids_to_tokens(self, ids):
"""Token of unknown word is assumed as <unk> according to sentencepiece"""
return convert_by_vocab(self.inv_vocab, ids, unk_info="<unk>")
class SentencePieceTokenizer(object):
"""Runs SentencePiece tokenization (from raw text to tokens list)"""
def __init__(self, model_file=None, do_lower_case=True):
"""Constructs a SentencePieceTokenizer."""
self.tokenizer = sp.SentencePieceProcessor()
if self.tokenizer.Load(model_file):
print("Loaded a trained SentencePiece model.")
else:
print("You have to give a path of trained SentencePiece model.")
sys.exit(1)
self.do_lower_case = do_lower_case
def tokenize(self, text):
"""Tokenizes a piece of text."""
text = convert_to_unicode(text)
if self.do_lower_case:
text = text.lower()
output_tokens = self.tokenizer.EncodeAsPieces(text)
return output_tokens