code: def main(args: TrainKenlmConfig):
      train_paths = kenlm_utils.get_train_list(args.train_paths)
  
      if isinstance(args.ngram_prune, str):
          args.ngram_prune = [args.ngram_prune]
  
      (
          tokenizer,
          encoding_level,
          is_aggregate_tokenizer,
      ) = kenlm_utils.setup_tokenizer(args.nemo_model_file)
  
      if encoding_level == "subword":
          discount_arg = "--discount_fallback"  # --discount_fallback is needed for training KenLM for BPE-based models
      else:
          discount_arg = ""
  
      arpa_file = f"{args.kenlm_model_file}.tmp.arpa"
      """ LMPLZ ARGUMENT SETUP """
      kenlm_args = [
          os.path.join(args.kenlm_bin_path, "lmplz"),
          "-o",
          str(args.ngram_length),
          "--arpa",
          arpa_file,
          discount_arg,
          "--prune",
      ] + [str(n) for n in args.ngram_prune]
  
      if args.cache_path:
          if not os.path.exists(args.cache_path):
              os.makedirs(args.cache_path, exist_ok=True)
  
          """ DATASET SETUP """
          encoded_train_files = []
          for file_num, train_file in enumerate(train_paths):
              logging.info(
                  f"Encoding the train file '{train_file}' number {file_num+1} out of {len(train_paths)} ..."
              )
  
              cached_files = glob(
                  os.path.join(
                      args.cache_path, os.path.split(train_file)[1]
                  )
                  + "*"
              )
              encoded_train_file = os.path.join(
                  args.cache_path,
                  os.path.split(train_file)[1] + f"_{file_num}.tmp.txt",
              )
              if (
                  cached_files and cached_files[0] != encoded_train_file
              ):  # cached_files exists but has another file name: f"_{file_num}.tmp.txt"
                  os.rename(cached_files[0], encoded_train_file)
                  logging.info(
                      "Rename",
                      cached_files[0],
                      "to",
                      encoded_train_file,
                  )
  
              encoded_train_files.append(encoded_train_file)
  
          kenlm_utils.iter_files(
              source_path=train_paths,
              dest_path=encoded_train_files,
              tokenizer=tokenizer,
              encoding_level=encoding_level,
              is_aggregate_tokenizer=is_aggregate_tokenizer,
              verbose=args.verbose,
          )
  
          first_process_args = ["cat"] + encoded_train_files
          first_process = subprocess.Popen(
              first_process_args,
              stdout=subprocess.PIPE,
              stderr=sys.stderr,
          )
  
          logging.info(
              f"Running lmplz command \n\n{' '.join(kenlm_args)}\n\n"
          )
          kenlm_p = subprocess.run(
              kenlm_args,
              stdin=first_process.stdout,
              capture_output=False,
              text=True,
              stdout=sys.stdout,
              stderr=sys.stderr,
          )
          first_process.wait()
  
      else:
          logging.info(
              f"Running lmplz command \n\n{' '.join(kenlm_args)}\n\n"
          )
          kenlm_p = subprocess.Popen(
              kenlm_args,
              stdout=sys.stdout,
              stdin=subprocess.PIPE,
              stderr=sys.stderr,
          )
  
          kenlm_utils.iter_files(
              source_path=train_paths,
              dest_path=kenlm_p.stdin,
              tokenizer=tokenizer,
              encoding_level=encoding_level,
              is_aggregate_tokenizer=is_aggregate_tokenizer,
              verbose=args.verbose,
          )
  
          kenlm_p.communicate()
  
      if kenlm_p.returncode != 0:
          raise RuntimeError("Training KenLM was not successful!")
  
      """ BINARY BUILD """
  
      kenlm_args = [
          os.path.join(args.kenlm_bin_path, "build_binary"),
          "trie",
          arpa_file,
          args.kenlm_model_file,
      ]
      logging.info(
          f"Running binary_build command \n\n{' '.join(kenlm_args)}\n\n"
      )
      ret = subprocess.run(
          kenlm_args,
          capture_output=False,
          text=True,
          stdout=sys.stdout,
          stderr=sys.stderr,
      )
  
      if ret.returncode != 0:
          raise RuntimeError("Training KenLM was not successful!")
  
      if not args.preserve_arpa:
          os.remove(arpa_file)
          logging.info(f"Deleted the arpa file '{arpa_file}'.")
  
file_path: NeMo/scripts/asr_language_modeling/ngram_lm/train_kenlm.py
imports: import logging
  import os
  import subprocess
  import sys
  from dataclasses import dataclass, field
  from glob import glob
  from typing import List
  from omegaconf import MISSING
  from scripts.asr_language_modeling.ngram_lm import kenlm_utils
  from nemo.core.config import hydra_runner
  from nemo.utils import logging
label_desc: Use image node_id 1 for calling a global function with example usage: main(args) without return types