code: def build_vocab(json, threshold):
      """Build a simple vocabulary wrapper."""
      coco = COCO(json)
      counter = Counter()
      ids = coco.anns.keys()
      for i, id in enumerate(ids):
          caption = str(coco.anns[id]["caption"])
          tokens = nltk.tokenize.word_tokenize(caption.lower())
          counter.update(tokens)
  
          if (i + 1) % 1000 == 0:
              print(
                  "[{}/{}] Tokenized the captions.".format(
                      i + 1, len(ids)
                  )
              )
  
      # If the word frequency is less than 'threshold', then the word is discarded.
      words = [
          word for word, cnt in counter.items() if cnt >= threshold
      ]
  
      # Create a vocab wrapper and add some special tokens.
      vocab = Vocabulary()
      vocab.add_word("<pad>")
      vocab.add_word("<start>")
      vocab.add_word("<end>")
      vocab.add_word("<unk>")
  
      # Add the words to the vocabulary.
      for i, word in enumerate(words):
          vocab.add_word(word)
      return vocab
  
file_path: pytorch-tutorial/tutorials/03-advanced/image_captioning/build_vocab.py
imports: import nltk
  import pickle
  import argparse
  from collections import Counter
  from pycocotools.coco import COCO
label_desc: Use image node_id 1 for calling a global function with example usage: build_vocab(json, threshold) and returns: vocab