Tries to add a token into a vocab and returns its id. If it has already been there, its id will be returned and the vocab won't be updated. If the vocab is locked, an assertion failure will occur. Args: token: A new or existing token. Returns: Its a
(self, token: str)
| 73 | return False |
| 74 | |
| 75 | def add(self, token: str) -> int: |
| 76 | """ Tries to add a token into a vocab and returns its id. If it has already been there, its id will be returned |
| 77 | and the vocab won't be updated. If the vocab is locked, an assertion failure will occur. |
| 78 | |
| 79 | Args: |
| 80 | token: A new or existing token. |
| 81 | |
| 82 | Returns: |
| 83 | Its associated id. |
| 84 | |
| 85 | """ |
| 86 | assert self.mutable, 'It is not allowed to call add on an immutable Vocab' |
| 87 | assert isinstance(token, str), f'Token type must be str but got {type(token)} from {token}' |
| 88 | assert token is not None, 'Token must not be None' |
| 89 | idx = self.token_to_idx.get(token, None) |
| 90 | if idx is None: |
| 91 | idx = len(self.token_to_idx) |
| 92 | self.token_to_idx[token] = idx |
| 93 | return idx |
| 94 | |
| 95 | def update(self, tokens: Iterable[str]) -> None: |
| 96 | """Update the vocab with these tokens by adding them to vocab one by one. |
no test coverage detected