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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers.models.bert.tokenization_bert import *
|
2 |
+
import os
|
3 |
+
|
4 |
+
|
5 |
+
class CLIPTokenizerRoberta(PreTrainedTokenizer):
|
6 |
+
r"""
|
7 |
+
Construct a BERT tokenizer. Based on WordPiece.
|
8 |
+
|
9 |
+
This tokenizer inherits from [`PreTrainedTokenizer`] which contains most of the main methods. Users should refer to
|
10 |
+
this superclass for more information regarding those methods.
|
11 |
+
|
12 |
+
Args:
|
13 |
+
vocab_file (`str`):
|
14 |
+
File containing the vocabulary.
|
15 |
+
do_lower_case (`bool`, *optional*, defaults to `True`):
|
16 |
+
Whether or not to lowercase the input when tokenizing.
|
17 |
+
do_basic_tokenize (`bool`, *optional*, defaults to `True`):
|
18 |
+
Whether or not to do basic tokenization before WordPiece.
|
19 |
+
never_split (`Iterable`, *optional*):
|
20 |
+
Collection of tokens which will never be split during tokenization. Only has an effect when
|
21 |
+
`do_basic_tokenize=True`
|
22 |
+
unk_token (`str`, *optional*, defaults to `"[UNK]"`):
|
23 |
+
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
|
24 |
+
token instead.
|
25 |
+
sep_token (`str`, *optional*, defaults to `"[SEP]"`):
|
26 |
+
The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences for
|
27 |
+
sequence classification or for a text and a question for question answering. It is also used as the last
|
28 |
+
token of a sequence built with special tokens.
|
29 |
+
pad_token (`str`, *optional*, defaults to `"[PAD]"`):
|
30 |
+
The token used for padding, for example when batching sequences of different lengths.
|
31 |
+
cls_token (`str`, *optional*, defaults to `"[CLS]"`):
|
32 |
+
The classifier token which is used when doing sequence classification (classification of the whole sequence
|
33 |
+
instead of per-token classification). It is the first token of the sequence when built with special tokens.
|
34 |
+
mask_token (`str`, *optional*, defaults to `"[MASK]"`):
|
35 |
+
The token used for masking values. This is the token used when training this model with masked language
|
36 |
+
modeling. This is the token which the model will try to predict.
|
37 |
+
tokenize_chinese_chars (`bool`, *optional*, defaults to `True`):
|
38 |
+
Whether or not to tokenize Chinese characters.
|
39 |
+
|
40 |
+
This should likely be deactivated for Japanese (see this
|
41 |
+
[issue](https://github.com/huggingface/transformers/issues/328)).
|
42 |
+
strip_accents (`bool`, *optional*):
|
43 |
+
Whether or not to strip all accents. If this option is not specified, then it will be determined by the
|
44 |
+
value for `lowercase` (as in the original BERT).
|
45 |
+
"""
|
46 |
+
|
47 |
+
vocab_files_names = VOCAB_FILES_NAMES
|
48 |
+
pretrained_vocab_files_map = PRETRAINED_VOCAB_FILES_MAP
|
49 |
+
pretrained_init_configuration = PRETRAINED_INIT_CONFIGURATION
|
50 |
+
max_model_input_sizes = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
|
51 |
+
|
52 |
+
def __init__(
|
53 |
+
self,
|
54 |
+
vocab_file,
|
55 |
+
do_lower_case=True,
|
56 |
+
do_basic_tokenize=True,
|
57 |
+
never_split=None,
|
58 |
+
unk_token="[UNK]",
|
59 |
+
sep_token="[SEP]",
|
60 |
+
pad_token="[PAD]",
|
61 |
+
cls_token="[CLS]",
|
62 |
+
mask_token="[MASK]",
|
63 |
+
tokenize_chinese_chars=True,
|
64 |
+
strip_accents=None,
|
65 |
+
**kwargs
|
66 |
+
):
|
67 |
+
if not os.path.isfile(vocab_file):
|
68 |
+
raise ValueError(
|
69 |
+
f"Can't find a vocabulary file at path '{vocab_file}'. To load the vocabulary from a Google pretrained"
|
70 |
+
" model use `tokenizer = BertTokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`"
|
71 |
+
)
|
72 |
+
self.vocab = load_vocab(vocab_file)
|
73 |
+
self.ids_to_tokens = collections.OrderedDict([(ids, tok) for tok, ids in self.vocab.items()])
|
74 |
+
self.do_basic_tokenize = do_basic_tokenize
|
75 |
+
if do_basic_tokenize:
|
76 |
+
self.basic_tokenizer = BasicTokenizer(
|
77 |
+
do_lower_case=do_lower_case,
|
78 |
+
never_split=never_split,
|
79 |
+
tokenize_chinese_chars=tokenize_chinese_chars,
|
80 |
+
strip_accents=strip_accents,
|
81 |
+
)
|
82 |
+
self.wordpiece_tokenizer = WordpieceTokenizer(vocab=self.vocab, unk_token=str(unk_token))
|
83 |
+
|
84 |
+
super().__init__(
|
85 |
+
do_lower_case=do_lower_case,
|
86 |
+
do_basic_tokenize=do_basic_tokenize,
|
87 |
+
never_split=never_split,
|
88 |
+
unk_token=unk_token,
|
89 |
+
sep_token=sep_token,
|
90 |
+
pad_token=pad_token,
|
91 |
+
cls_token=cls_token,
|
92 |
+
mask_token=mask_token,
|
93 |
+
tokenize_chinese_chars=tokenize_chinese_chars,
|
94 |
+
strip_accents=strip_accents,
|
95 |
+
**kwargs,
|
96 |
+
)
|
97 |
+
|
98 |
+
@property
|
99 |
+
def do_lower_case(self):
|
100 |
+
return self.basic_tokenizer.do_lower_case
|
101 |
+
|
102 |
+
@property
|
103 |
+
def vocab_size(self):
|
104 |
+
return len(self.vocab)
|
105 |
+
|
106 |
+
def get_vocab(self):
|
107 |
+
return dict(self.vocab, **self.added_tokens_encoder)
|
108 |
+
|
109 |
+
def _tokenize(self, text):
|
110 |
+
split_tokens = []
|
111 |
+
if self.do_basic_tokenize:
|
112 |
+
for token in self.basic_tokenizer.tokenize(text, never_split=self.all_special_tokens):
|
113 |
+
|
114 |
+
# If the token is part of the never_split set
|
115 |
+
if token in self.basic_tokenizer.never_split:
|
116 |
+
split_tokens.append(token)
|
117 |
+
else:
|
118 |
+
split_tokens += self.wordpiece_tokenizer.tokenize(token)
|
119 |
+
else:
|
120 |
+
split_tokens = self.wordpiece_tokenizer.tokenize(text)
|
121 |
+
return split_tokens
|
122 |
+
|
123 |
+
def _convert_token_to_id(self, token):
|
124 |
+
"""Converts a token (str) in an id using the vocab."""
|
125 |
+
return self.vocab.get(token, self.vocab.get(self.unk_token))
|
126 |
+
|
127 |
+
def _convert_id_to_token(self, index):
|
128 |
+
"""Converts an index (integer) in a token (str) using the vocab."""
|
129 |
+
return self.ids_to_tokens.get(index, self.unk_token)
|
130 |
+
|
131 |
+
def convert_tokens_to_string(self, tokens):
|
132 |
+
"""Converts a sequence of tokens (string) in a single string."""
|
133 |
+
out_string = " ".join(tokens).replace(" ##", "").strip()
|
134 |
+
return out_string
|
135 |
+
|
136 |
+
def build_inputs_with_special_tokens(
|
137 |
+
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
|
138 |
+
) -> List[int]:
|
139 |
+
"""
|
140 |
+
Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
|
141 |
+
adding special tokens. A BERT sequence has the following format:
|
142 |
+
|
143 |
+
- single sequence: `[CLS] X [SEP]`
|
144 |
+
- pair of sequences: `[CLS] A [SEP] B [SEP]`
|
145 |
+
|
146 |
+
Args:
|
147 |
+
token_ids_0 (`List[int]`):
|
148 |
+
List of IDs to which the special tokens will be added.
|
149 |
+
token_ids_1 (`List[int]`, *optional*):
|
150 |
+
Optional second list of IDs for sequence pairs.
|
151 |
+
|
152 |
+
Returns:
|
153 |
+
`List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens.
|
154 |
+
"""
|
155 |
+
sep = [49407]
|
156 |
+
cls = [49406]
|
157 |
+
|
158 |
+
if token_ids_1 is None:
|
159 |
+
return cls + token_ids_0 + sep
|
160 |
+
# return [self.cls_token_id] + token_ids_0 + [self.sep_token_id]
|
161 |
+
# cls = [self.cls_token_id]
|
162 |
+
# sep = [self.sep_token_id]
|
163 |
+
|
164 |
+
return cls + token_ids_0 + sep + token_ids_1 + sep
|
165 |
+
|
166 |
+
def get_special_tokens_mask(
|
167 |
+
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None,
|
168 |
+
already_has_special_tokens: bool = False
|
169 |
+
) -> List[int]:
|
170 |
+
"""
|
171 |
+
Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
|
172 |
+
special tokens using the tokenizer `prepare_for_model` method.
|
173 |
+
|
174 |
+
Args:
|
175 |
+
token_ids_0 (`List[int]`):
|
176 |
+
List of IDs.
|
177 |
+
token_ids_1 (`List[int]`, *optional*):
|
178 |
+
Optional second list of IDs for sequence pairs.
|
179 |
+
already_has_special_tokens (`bool`, *optional*, defaults to `False`):
|
180 |
+
Whether or not the token list is already formatted with special tokens for the model.
|
181 |
+
|
182 |
+
Returns:
|
183 |
+
`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
|
184 |
+
"""
|
185 |
+
|
186 |
+
if already_has_special_tokens:
|
187 |
+
return super().get_special_tokens_mask(
|
188 |
+
token_ids_0=token_ids_0, token_ids_1=token_ids_1, already_has_special_tokens=True
|
189 |
+
)
|
190 |
+
|
191 |
+
if token_ids_1 is not None:
|
192 |
+
return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1]
|
193 |
+
return [1] + ([0] * len(token_ids_0)) + [1]
|
194 |
+
|
195 |
+
def create_token_type_ids_from_sequences(
|
196 |
+
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
|
197 |
+
) -> List[int]:
|
198 |
+
"""
|
199 |
+
Create a mask from the two sequences passed to be used in a sequence-pair classification task. A BERT sequence
|
200 |
+
pair mask has the following format:
|
201 |
+
|
202 |
+
```
|
203 |
+
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
|
204 |
+
| first sequence | second sequence |
|
205 |
+
```
|
206 |
+
|
207 |
+
If `token_ids_1` is `None`, this method only returns the first portion of the mask (0s).
|
208 |
+
|
209 |
+
Args:
|
210 |
+
token_ids_0 (`List[int]`):
|
211 |
+
List of IDs.
|
212 |
+
token_ids_1 (`List[int]`, *optional*):
|
213 |
+
Optional second list of IDs for sequence pairs.
|
214 |
+
|
215 |
+
Returns:
|
216 |
+
`List[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s).
|
217 |
+
"""
|
218 |
+
# sep = [self.sep_token_id]
|
219 |
+
# cls = [self.cls_token_id]
|
220 |
+
sep = [49407]
|
221 |
+
cls = [49406]
|
222 |
+
if token_ids_1 is None:
|
223 |
+
return len(cls + token_ids_0 + sep) * [0]
|
224 |
+
return len(cls + token_ids_0 + sep) * [0] + len(token_ids_1 + sep) * [1]
|
225 |
+
|
226 |
+
def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]:
|
227 |
+
index = 0
|
228 |
+
if os.path.isdir(save_directory):
|
229 |
+
vocab_file = os.path.join(
|
230 |
+
save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
|
231 |
+
)
|
232 |
+
else:
|
233 |
+
vocab_file = (filename_prefix + "-" if filename_prefix else "") + save_directory
|
234 |
+
with open(vocab_file, "w", encoding="utf-8") as writer:
|
235 |
+
for token, token_index in sorted(self.vocab.items(), key=lambda kv: kv[1]):
|
236 |
+
if index != token_index:
|
237 |
+
logger.warning(
|
238 |
+
f"Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive."
|
239 |
+
" Please check that the vocabulary is not corrupted!"
|
240 |
+
)
|
241 |
+
index = token_index
|
242 |
+
writer.write(token + "\n")
|
243 |
+
index += 1
|
244 |
+
return (vocab_file,)
|
245 |
+
|
246 |
+
|
special_tokens_map.json
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cls_token": {
|
3 |
+
"content": "[CLS]",
|
4 |
+
"lstrip": false,
|
5 |
+
"normalized": false,
|
6 |
+
"rstrip": false,
|
7 |
+
"single_word": false
|
8 |
+
},
|
9 |
+
"mask_token": {
|
10 |
+
"content": "[MASK]",
|
11 |
+
"lstrip": false,
|
12 |
+
"normalized": false,
|
13 |
+
"rstrip": false,
|
14 |
+
"single_word": false
|
15 |
+
},
|
16 |
+
"pad_token": {
|
17 |
+
"content": "[PAD]",
|
18 |
+
"lstrip": false,
|
19 |
+
"normalized": false,
|
20 |
+
"rstrip": false,
|
21 |
+
"single_word": false
|
22 |
+
},
|
23 |
+
"sep_token": {
|
24 |
+
"content": "[SEP]",
|
25 |
+
"lstrip": false,
|
26 |
+
"normalized": false,
|
27 |
+
"rstrip": false,
|
28 |
+
"single_word": false
|
29 |
+
},
|
30 |
+
"unk_token": {
|
31 |
+
"content": "[UNK]",
|
32 |
+
"lstrip": false,
|
33 |
+
"normalized": false,
|
34 |
+
"rstrip": false,
|
35 |
+
"single_word": false
|
36 |
+
}
|
37 |
+
}
|
tokenizer_config.json
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"added_tokens_decoder": {
|
3 |
+
"0": {
|
4 |
+
"content": "[PAD]",
|
5 |
+
"lstrip": false,
|
6 |
+
"normalized": false,
|
7 |
+
"rstrip": false,
|
8 |
+
"single_word": false,
|
9 |
+
"special": true
|
10 |
+
},
|
11 |
+
"100": {
|
12 |
+
"content": "[UNK]",
|
13 |
+
"lstrip": false,
|
14 |
+
"normalized": false,
|
15 |
+
"rstrip": false,
|
16 |
+
"single_word": false,
|
17 |
+
"special": true
|
18 |
+
},
|
19 |
+
"101": {
|
20 |
+
"content": "[CLS]",
|
21 |
+
"lstrip": false,
|
22 |
+
"normalized": false,
|
23 |
+
"rstrip": false,
|
24 |
+
"single_word": false,
|
25 |
+
"special": true
|
26 |
+
},
|
27 |
+
"102": {
|
28 |
+
"content": "[SEP]",
|
29 |
+
"lstrip": false,
|
30 |
+
"normalized": false,
|
31 |
+
"rstrip": false,
|
32 |
+
"single_word": false,
|
33 |
+
"special": true
|
34 |
+
},
|
35 |
+
"103": {
|
36 |
+
"content": "[MASK]",
|
37 |
+
"lstrip": false,
|
38 |
+
"normalized": false,
|
39 |
+
"rstrip": false,
|
40 |
+
"single_word": false,
|
41 |
+
"special": true
|
42 |
+
}
|
43 |
+
},
|
44 |
+
"auto_map": {
|
45 |
+
"AutoTokenizer": [
|
46 |
+
"clip_tokenizer_roberta.CLIPTokenizerRoberta",
|
47 |
+
null
|
48 |
+
]
|
49 |
+
},
|
50 |
+
"clean_up_tokenization_spaces": true,
|
51 |
+
"cls_token": "[CLS]",
|
52 |
+
"do_basic_tokenize": true,
|
53 |
+
"do_lower_case": true,
|
54 |
+
"mask_token": "[MASK]",
|
55 |
+
"model_max_length": 77,
|
56 |
+
"never_split": null,
|
57 |
+
"pad_token": "[PAD]",
|
58 |
+
"sep_token": "[SEP]",
|
59 |
+
"strip_accents": null,
|
60 |
+
"tokenize_chinese_chars": true,
|
61 |
+
"tokenizer_class": "CLIPTokenizerRoberta",
|
62 |
+
"unk_token": "[UNK]",
|
63 |
+
"use_fast": true
|
64 |
+
}
|
vocab.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|