code: def _build_vocab(generator, vocab_dir, vocab_name):
      """Build a vocabulary from examples.
  
      Args:
        generator: text generator for creating vocab.
        vocab_dir: directory where to save the vocabulary.
        vocab_name: vocab file name.
  
      Returns:
        text encoder.
      """
      vocab_path = os.path.join(vocab_dir, vocab_name)
      if not tf.gfile.Exists(vocab_path):
          data = []
          for line in generator:
              data.extend(line.split())
          counter = collections.Counter(data)
          count_pairs = sorted(
              counter.items(), key=lambda x: (-x[1], x[0])
          )
          words, _ = list(zip(*count_pairs))
          encoder = text_encoder.TokenTextEncoder(
              None, vocab_list=words
          )
          encoder.store_to_file(vocab_path)
      else:
          encoder = text_encoder.TokenTextEncoder(vocab_path)
      return encoder
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import collections
  import os
  import shutil
  import tarfile
  import requests
  import six
  from tensor2tensor.data_generators import problem
  from tensor2tensor.data_generators import text_encoder
  from tensor2tensor.data_generators import text_problems
  from tensor2tensor.data_generators import tokenizer
  from tensor2tensor.layers import modalities
  from tensor2tensor.utils import metrics
  from tensor2tensor.utils import registry
  import tensorflow.compat.v1
label_desc: Use image node_id 3 for calling a global function with example usage: _build_vocab(generator, vocab_dir, vocab_name) and returns: encoder