code: def build_vocab(opts, transforms, n_sample=3):
      """Build vocabulary from data."""
  
      if n_sample == -1:
          logger.info(
              f"n_sample={n_sample}: Build vocab on full datasets."
          )
      elif n_sample > 0:
          logger.info(
              f"Build vocab on {n_sample} transformed examples/corpus."
          )
      else:
          raise ValueError(
              f"n_sample should > 0 or == -1, get {n_sample}."
          )
  
      if opts.dump_samples:
          logger.info(
              "The samples on which the vocab is built will be "
              "dumped to disk. It may slow down the process."
          )
      corpora = get_corpora(opts, task=CorpusTask.TRAIN)
      counter_src = Counter()
      counter_tgt = Counter()
      counter_src_feats = [Counter() for _ in range(opts.n_src_feats)]
  
      queues = {
          c_name: [
              mp.Queue(opts.vocab_sample_queue_size)
              for i in range(opts.num_threads)
          ]
          for c_name in corpora.keys()
      }
      sample_path = os.path.join(
          os.path.dirname(opts.save_data), CorpusName.SAMPLE
      )
      if opts.dump_samples:
          write_process = mp.Process(
              target=write_files_from_queues,
              args=(sample_path, queues),
              daemon=True,
          )
          write_process.start()
      with mp.Pool(opts.num_threads, init_pool, [queues]) as p:
          func = partial(
              build_sub_vocab,
              corpora,
              transforms,
              opts,
              n_sample,
              opts.num_threads,
          )
          for (
              sub_counter_src,
              sub_counter_tgt,
              sub_counter_src_feats,
          ) in p.imap(func, range(0, opts.num_threads)):
              counter_src.update(sub_counter_src)
              counter_tgt.update(sub_counter_tgt)
              for i in range(opts.n_src_feats):
                  counter_src_feats[i].update(sub_counter_src_feats[i])
      if opts.dump_samples:
          write_process.join()
      return counter_src, counter_tgt, counter_src_feats
  
imports: import os
  import copy
  import multiprocessing
  import pyonmttok
  from functools import partial
  from onmt.utils.logging import init_logger, logger
  from onmt.utils.misc import set_random_seed, check_path
  from onmt.utils.parse import ArgumentParser
  from onmt.opts import data_prepare_opts
  from onmt.inputters.text_corpus import build_corpora_iters, get_corpora
  from onmt.inputters.text_utils import process, append_features_to_text
  from onmt.transforms import make_transforms, get_transforms_cls
  from onmt.constants import CorpusName, CorpusTask
  from collections import Counter
label_desc: Use image node_id 4 for calling a global function with example usage: build_vocab(opts, transforms, n_sample) and returns: counter_src,  counter_tgt,  counter_src_feats