Vocabulary
npfl138.Vocabulary
A class for managing mapping between strings and indices.
The vocabulary is initialized with a list of strings. It provides:
__len__
: the number of strings in the vocabulary,__iter__
: the iterator over strings in the vocabulary,string(index: int) -> str
: the string for a given vocabulary index,strings(indices: Sequence[int]) -> list[str]
: the list of strings for the given indices,index(string: str) -> int
: the index of a given string in the vocabulary,indices(strings: Sequence[str]) -> list[int]
: the list of indices for given strings.
Source code in npfl138/vocabulary.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
__init__
Initializes the vocabulary with the given list of strings.
The Vocabulary.PAD
is always the first token in the vocabulary;
Vocabulary.UNK
is the second token but only when add_unk=True
.
Source code in npfl138/vocabulary.py
26 27 28 29 30 31 32 33 34 35 36 |
|
__len__
__len__() -> int
Returns the number of strings in the vocabulary.
Source code in npfl138/vocabulary.py
38 39 40 |
|
__iter__
Returns an iterator over strings in the vocabulary.
Source code in npfl138/vocabulary.py
42 43 44 |
|
string
Returns the string for a given vocabulary index.
Source code in npfl138/vocabulary.py
46 47 48 |
|
strings
Returns the list of strings for the given indices.
Source code in npfl138/vocabulary.py
50 51 52 |
|
index
Returns the index of a given string in the vocabulary.
Source code in npfl138/vocabulary.py
54 55 56 |
|
indices
Returns the list of indices for given strings.
Source code in npfl138/vocabulary.py
58 59 60 |
|