code: def _build_vocab(examples, example_field, vocab_dir, vocab_name):
      """Build a vocabulary from examples.
  
      Args:
        examples: a dict containing all the examples.
        example_field: field of example from which the vocabulary is built.
        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 e in examples:
              data.extend(e[example_field].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 csv
  import gzip
  import os
  import random
  from tensor2tensor.data_generators import generator_utils
  from tensor2tensor.data_generators import problem
  from tensor2tensor.data_generators import text_encoder
  from tensor2tensor.data_generators import text_problems
  from tensor2tensor.utils import metrics
  from tensor2tensor.utils import registry
  import tensorflow.compat.v1
label_desc: Use image node_id 1 for calling a global function with example usage: _build_vocab(examples, example_field, vocab_dir, vocab_name) and returns: encoder