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

Method char_to_word

bert/tokenization_utils_base.py:439–474  ·  view source on GitHub ↗

Get the word in the original string corresponding to a character in the original string of a sequence of the batch. Can be called as: - ``self.char_to_word(char_index)`` if batch size is 1 - ``self.char_to_word(batch_index, char_index)`` if batch size is gr

(self, batch_or_char_index: int, char_index: Optional[int] = None)

Source from the content-addressed store, hash-verified

437 return CharSpan(*(self._encodings[batch_index].word_to_chars(word_index)))
438
439 def char_to_word(self, batch_or_char_index: int, char_index: Optional[int] = None) -> int:
440 """
441 Get the word in the original string corresponding to a character in the original string of
442 a sequence of the batch.
443
444 Can be called as:
445
446 - ``self.char_to_word(char_index)`` if batch size is 1
447 - ``self.char_to_word(batch_index, char_index)`` if batch size is greater than 1
448
449 This method is particularly suited when the input sequences are provided as
450 pre-tokenized sequences (i.e. words are defined by the user). In this case it allows
451 to easily associate encoded tokens with provided tokenized words.
452
453 Args:
454 batch_or_char_index (:obj:`int`):
455 Index of the sequence in the batch. If the batch only comprise one sequence,
456 this can be the index of the character in the orginal string.
457 char_index (:obj:`int`, `optional`):
458 If a batch index is provided in `batch_or_token_index`, this can be the index
459 of the character in the orginal string.
460
461
462 Returns:
463 :obj:`int` or :obj:`List[int]`:
464 Index or indices of the associated encoded token(s).
465 """
466
467 if not self._encodings:
468 raise ValueError("char_to_word() is not available when using Python based tokenizers")
469 if char_index is not None:
470 batch_index = batch_or_char_index
471 else:
472 batch_index = 0
473 char_index = batch_or_char_index
474 return self._encodings[batch_index].char_to_word(char_index)
475
476 def convert_to_tensors(self, tensor_type: Union[None, str, TensorType], prepend_batch_axis: bool = False):
477 if tensor_type is None:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected