MCPcopy Create free account
hub / github.com/SooLab/CGFormer / _encode_plus

Method _encode_plus

bert/tokenization_utils.py:402–474  ·  view source on GitHub ↗
(
        self,
        text: Union[TextInput, PreTokenizedInput, EncodedInput],
        text_pair: Optional[Union[TextInput, PreTokenizedInput, EncodedInput]] = None,
        add_special_tokens: bool = True,
        padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD,
        truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE,
        max_length: Optional[int] = None,
        stride: int = 0,
        is_pretokenized: bool = False,
        pad_to_multiple_of: Optional[int] = None,
        return_tensors: Optional[Union[str, TensorType]] = None,
        return_token_type_ids: Optional[bool] = None,
        return_attention_mask: Optional[bool] = None,
        return_overflowing_tokens: bool = False,
        return_special_tokens_mask: bool = False,
        return_offsets_mapping: bool = False,
        return_length: bool = False,
        verbose: bool = True,
        **kwargs
    )

Source from the content-addressed store, hash-verified

400 raise NotImplementedError
401
402 def _encode_plus(
403 self,
404 text: Union[TextInput, PreTokenizedInput, EncodedInput],
405 text_pair: Optional[Union[TextInput, PreTokenizedInput, EncodedInput]] = None,
406 add_special_tokens: bool = True,
407 padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD,
408 truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE,
409 max_length: Optional[int] = None,
410 stride: int = 0,
411 is_pretokenized: bool = False,
412 pad_to_multiple_of: Optional[int] = None,
413 return_tensors: Optional[Union[str, TensorType]] = None,
414 return_token_type_ids: Optional[bool] = None,
415 return_attention_mask: Optional[bool] = None,
416 return_overflowing_tokens: bool = False,
417 return_special_tokens_mask: bool = False,
418 return_offsets_mapping: bool = False,
419 return_length: bool = False,
420 verbose: bool = True,
421 **kwargs
422 ) -> BatchEncoding:
423 def get_input_ids(text):
424 if isinstance(text, str):
425 tokens = self.tokenize(text, **kwargs)
426 return self.convert_tokens_to_ids(tokens)
427 elif isinstance(text, (list, tuple)) and len(text) > 0 and isinstance(text[0], str):
428 if is_pretokenized:
429 tokens = list(itertools.chain(*(self.tokenize(t, is_pretokenized=True, **kwargs) for t in text)))
430 return self.convert_tokens_to_ids(tokens)
431 else:
432 return self.convert_tokens_to_ids(text)
433 elif isinstance(text, (list, tuple)) and len(text) > 0 and isinstance(text[0], int):
434 return text
435 else:
436 if is_pretokenized:
437 raise ValueError(
438 f"Input {text} is not valid. Should be a string or a list/tuple of strings when `is_pretokenized=True`."
439 )
440 else:
441 raise ValueError(
442 f"Input {text} is not valid. Should be a string, a list/tuple of strings or a list/tuple of integers."
443 )
444
445 if return_offsets_mapping:
446 raise NotImplementedError(
447 "return_offset_mapping is not available when using Python tokenizers."
448 "To use this feature, change your tokenizer to one deriving from "
449 "transformers.PreTrainedTokenizerFast."
450 "More information on available tokenizers at "
451 "https://github.com/huggingface/transformers/pull/2674"
452 )
453
454 first_ids = get_input_ids(text)
455 second_ids = get_input_ids(text_pair) if text_pair is not None else None
456
457 return self.prepare_for_model(
458 first_ids,
459 pair_ids=second_ids,

Callers

nothing calls this directly

Calls 1

prepare_for_modelMethod · 0.80

Tested by

no test coverage detected