get Vocab object
vocab_obj = build_vocab_from_iterator(yield_tokens(file_path), specials=[""])
creating Vocab through ordered dict
from torchtext.vocab import vocab
from collections import Counter, OrderedDict
counter = Counter(["a", "a", "b", "b", "b"])
sorted_by_freq_tuples = sorted(counter.items(), key=lambda x: x[1], reverse=True)
ordered_dict = OrderedDict(sorted_by_freq_tuples)
vocab_obj = vocab(ordered_dict)
common API usage
look-up index
vocab_obj["a"]
batch look-up indices
vocab_obj.looup_indices(["a","b"])
support forward API of PyTorch nn Modules
vocab_obj(["a","b"])
batch look-up tokens
vocab_obj.lookup_tokens([0,1])
set default index to return when token not found
vocab_obj.set_default_index(0)
vocab_obj["out_of_vocabulary"] #prints 0
```
For more details, refer to the documentation.