Get the index of the word corresponding (i.e. comprising) to an encoded token in a sequence of the batch. Can be called as: - ``self.token_to_word(token_index)`` if batch size is 1 - ``self.token_to_word(batch_index, token_index)`` if batch size is greater
(self, batch_or_token_index: int, token_index: Optional[int] = None)
| 229 | return self._encodings[batch_index].words |
| 230 | |
| 231 | def token_to_word(self, batch_or_token_index: int, token_index: Optional[int] = None) -> int: |
| 232 | """ |
| 233 | Get the index of the word corresponding (i.e. comprising) to an encoded token |
| 234 | in a sequence of the batch. |
| 235 | |
| 236 | Can be called as: |
| 237 | |
| 238 | - ``self.token_to_word(token_index)`` if batch size is 1 |
| 239 | - ``self.token_to_word(batch_index, token_index)`` if batch size is greater than 1 |
| 240 | |
| 241 | This method is particularly suited when the input sequences are provided as |
| 242 | pre-tokenized sequences (i.e. words are defined by the user). In this case it allows |
| 243 | to easily associate encoded tokens with provided tokenized words. |
| 244 | |
| 245 | Args: |
| 246 | batch_or_token_index (:obj:`int`): |
| 247 | Index of the sequence in the batch. If the batch only comprise one sequence, |
| 248 | this can be the index of the token in the sequence |
| 249 | token_index (:obj:`int`, `optional`): |
| 250 | If a batch index is provided in `batch_or_token_index`, this can be the index |
| 251 | of the token in the sequence. |
| 252 | |
| 253 | Returns: |
| 254 | :obj:`int`: |
| 255 | index of the word in the input sequence. |
| 256 | |
| 257 | """ |
| 258 | |
| 259 | if not self._encodings: |
| 260 | raise ValueError("token_to_word() is not available when using Python based tokenizers") |
| 261 | if token_index is not None: |
| 262 | batch_index = batch_or_token_index |
| 263 | else: |
| 264 | batch_index = 0 |
| 265 | token_index = batch_or_token_index |
| 266 | if batch_index < 0: |
| 267 | batch_index = self._batch_size + batch_index |
| 268 | if token_index < 0: |
| 269 | token_index = self._seq_len + token_index |
| 270 | return self._encodings[batch_index].token_to_word(token_index) |
| 271 | |
| 272 | def word_to_tokens(self, batch_or_word_index: int, word_index: Optional[int] = None) -> TokenSpan: |
| 273 | """ |
nothing calls this directly
no outgoing calls
no test coverage detected