Save the tokenizer vocabulary files together with: - added tokens, - special-tokens-to-class-attributes-mapping, - tokenizer instantiation positional and keywords inputs (e.g. do_lower_case for Bert). Warning: This won't save modification
(self, save_directory)
| 1332 | return tokenizer |
| 1333 | |
| 1334 | def save_pretrained(self, save_directory) -> Tuple[str]: |
| 1335 | """ Save the tokenizer vocabulary files together with: |
| 1336 | - added tokens, |
| 1337 | - special-tokens-to-class-attributes-mapping, |
| 1338 | - tokenizer instantiation positional and keywords inputs (e.g. do_lower_case for Bert). |
| 1339 | |
| 1340 | Warning: This won't save modifications you may have applied to the tokenizer after the instantiation |
| 1341 | (e.g. modifying tokenizer.do_lower_case after creation). |
| 1342 | |
| 1343 | This method make sure the full tokenizer can then be re-loaded using the |
| 1344 | :func:`~transformers.PreTrainedTokenizer.from_pretrained` class method. |
| 1345 | """ |
| 1346 | if os.path.isfile(save_directory): |
| 1347 | logger.error("Provided path ({}) should be a directory, not a file".format(save_directory)) |
| 1348 | return |
| 1349 | os.makedirs(save_directory, exist_ok=True) |
| 1350 | |
| 1351 | special_tokens_map_file = os.path.join(save_directory, SPECIAL_TOKENS_MAP_FILE) |
| 1352 | added_tokens_file = os.path.join(save_directory, ADDED_TOKENS_FILE) |
| 1353 | tokenizer_config_file = os.path.join(save_directory, TOKENIZER_CONFIG_FILE) |
| 1354 | |
| 1355 | tokenizer_config = copy.deepcopy(self.init_kwargs) |
| 1356 | if len(self.init_inputs) > 0: |
| 1357 | tokenizer_config["init_inputs"] = copy.deepcopy(self.init_inputs) |
| 1358 | for file_id in self.vocab_files_names.keys(): |
| 1359 | tokenizer_config.pop(file_id, None) |
| 1360 | |
| 1361 | with open(tokenizer_config_file, "w", encoding="utf-8") as f: |
| 1362 | f.write(json.dumps(tokenizer_config, ensure_ascii=False)) |
| 1363 | |
| 1364 | with open(special_tokens_map_file, "w", encoding="utf-8") as f: |
| 1365 | write_dict = {} |
| 1366 | for key, value in self.special_tokens_map_extended.items(): |
| 1367 | if isinstance(value, AddedToken): |
| 1368 | write_dict[key] = value.__getstate__() |
| 1369 | else: |
| 1370 | write_dict[key] = value |
| 1371 | f.write(json.dumps(write_dict, ensure_ascii=False)) |
| 1372 | |
| 1373 | added_vocab = self.get_added_vocab() |
| 1374 | if added_vocab: |
| 1375 | with open(added_tokens_file, "w", encoding="utf-8") as f: |
| 1376 | out_str = json.dumps(added_vocab, ensure_ascii=False) |
| 1377 | f.write(out_str) |
| 1378 | |
| 1379 | vocab_files = self.save_vocabulary(save_directory) |
| 1380 | |
| 1381 | return vocab_files + (special_tokens_map_file, added_tokens_file) |
| 1382 | |
| 1383 | @add_end_docstrings( |
| 1384 | ENCODE_KWARGS_DOCSTRING, |
nothing calls this directly
no test coverage detected