Get frequency of words using Vocab in pytorch Torchtext
how can i get the frequencies of tokens in a torchtext vocab that is created using build_vocab_from_iterator? link to doc:https://pytorch.org/text/stable/vocab.html#torchtext.vocab.Vocab
def build_vocab(data_iter, tokenizer):
&quot;&quot;&quot;Builds vocabulary from iterator&quot;&quot;&quot;

    vocab = build_vocab_from_iterator(
            map(tokenizer, data_iter),
            specials=[&quot;&lt;unk&gt;&quot;],
            min_freq=MIN_WORD_FREQUENCY,)
    vocab.set_default_index(vocab[&quot;&lt;unk&gt;&quot;])
    return vocab

data_iter = get_data_iterator(ds_name, ds_type, data_dir)
tokenizer = get_english_tokenizer()

if not vocab:
    vocab = build_vocab(data_iter, tokenizer)


You won't be able to get the frequency after you have built the vocab, since that data is lost during the build. It is just checking that the token occurs more than min_freq, and if so, adds it to the vocabulary.
However, you can get the frequency of the tokens before you build the vocabulary. One way to do that is with a Counter (Counter docs):
counter = Counter()
for text in data_iter:
    counter.update(tokenizer(text))

You can get the frequency of the tokens from the counter, then build the vocabulary from the counter:
vocab = vocab.vocab(counter, min_freq=MIN_WORD_FREQUENCY)

