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

Method char_to_token

bert/tokenization_utils_base.py:362–396  ·  view source on GitHub ↗

Get the index of the token in the encoded output comprising a character in the original string for a sequence of the batch. Can be called as: - ``self.char_to_token(char_index)`` if batch size is 1 - ``self.char_to_token(batch_index, char_index)`` if batch

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

Source from the content-addressed store, hash-verified

360 return CharSpan(*(self._encodings[batch_index].token_to_chars(token_index)))
361
362 def char_to_token(self, batch_or_char_index: int, char_index: Optional[int] = None) -> int:
363 """
364 Get the index of the token in the encoded output comprising a character
365 in the original string for a sequence of the batch.
366
367 Can be called as:
368
369 - ``self.char_to_token(char_index)`` if batch size is 1
370 - ``self.char_to_token(batch_index, char_index)`` if batch size is greater or equal to 1
371
372 This method is particularly suited when the input sequences are provided as
373 pre-tokenized sequences (i.e. words are defined by the user). In this case it allows
374 to easily associate encoded tokens with provided tokenized words.
375
376 Args:
377 batch_or_char_index (:obj:`int`):
378 Index of the sequence in the batch. If the batch only comprise one sequence,
379 this can be the index of the word in the sequence
380 char_index (:obj:`int`, `optional`):
381 If a batch index is provided in `batch_or_token_index`, this can be the index
382 of the word in the sequence.
383
384
385 Returns:
386 :obj:`int`: Index of the token.
387 """
388
389 if not self._encodings:
390 raise ValueError("char_to_token() is not available when using Python based tokenizers")
391 if char_index is not None:
392 batch_index = batch_or_char_index
393 else:
394 batch_index = 0
395 char_index = batch_or_char_index
396 return self._encodings[batch_index].char_to_token(char_index)
397
398 def word_to_chars(self, batch_or_word_index: int, word_index: Optional[int] = None) -> CharSpan:
399 """

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected