MCPcopy
hub / github.com/hankcs/HanLP / add

Method add

hanlp/common/vocab.py:75–93  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.

Callers 15

build_vocabsMethod · 0.95
build_vocabsMethod · 0.95
updateMethod · 0.95
lowerMethod · 0.95
build_modelMethod · 0.45
init_amr_vocabularyMethod · 0.45
_fix_and_make_graphMethod · 0.45
init_amr_vocabularyMethod · 0.45
_fix_and_make_graphMethod · 0.45
_linearizeMethod · 0.45

Calls 1

getMethod · 0.80

Tested by

no test coverage detected