Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

tiny tweak to allow BatchEncoding.token_to_char when token doesn't correspond to chars #15901

Merged
merged 5 commits into from
Apr 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/transformers/tokenization_utils_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,8 @@ def token_to_chars(self, batch_or_token_index: int, token_index: Optional[int] =
the sequence.

Returns:
[`~tokenization_utils_base.CharSpan`]: Span of characters in the original string.
[`~tokenization_utils_base.CharSpan`]: Span of characters in the original string, or None, if the token
(e.g. <s>, </s>) doesn't correspond to any chars in the origin string.
"""

if not self._encodings:
Expand All @@ -513,7 +514,9 @@ def token_to_chars(self, batch_or_token_index: int, token_index: Optional[int] =
else:
batch_index = 0
token_index = batch_or_token_index
return CharSpan(*(self._encodings[batch_index].token_to_chars(token_index)))
span_indices = self._encodings[batch_index].token_to_chars(token_index)

return CharSpan(*span_indices) if span_indices is not None else None

def char_to_token(
self, batch_or_char_index: int, char_index: Optional[int] = None, sequence_index: int = 0
Expand Down