code: def filter_by_vocab() -> None:
      """Given a restricted vocabulary of replacements,
      loops through the files with alignment results in each semiotic class subfolder,
      discards the examples containing a replacement which is not in our restricted vocabulary.
      """
  
      if not os.path.exists(args.vocab_filename):
          raise ValueError(
              f"Alignments dir {args.giza_dir} does not exist"
          )
      # load vocab from file
      vocab = {}
      with open(args.vocab_filename, "r", encoding="utf-8") as f:
          for line in f:
              k, v = line.strip().split("\t")
              vocab[k] = int(v)
      print("len(vocab)=", len(vocab))
      alignment_files = glob.glob(
          args.giza_dir + "/*/" + args.alignment_filename
      )
      for fn in alignment_files:
          fn_parts = fn.split("/")
          if len(fn_parts) < 2:
              raise ValueError("Bad filename: " + fn)
          semiotic_class = fn_parts[-2]
          out = open(
              args.giza_dir
              + "/"
              + semiotic_class
              + "/"
              + args.out_filename,
              "w",
              encoding="utf-8",
          )
          with open(fn, "r", encoding="utf-8") as f:
              for line in f:
                  t = process_line(semiotic_class, line)
                  if t is None:
                      continue
                  src, dst, replacement, freq = t
                  ok = True
                  for s, r in zip(
                      src.split(" "), replacement.split(" ")
                  ):
                      if s != r and r not in vocab:
                          ok = False
                  if ok:
                      out.write(
                          semiotic_class
                          + "\t"
                          + src
                          + "\t"
                          + dst
                          + "\t"
                          + replacement
                          + "\n"
                      )
          out.close()
  
file_path: NeMo/examples/nlp/text_normalization_as_tagging/dataset_preparation/prepare_corpora_after_alignment.py
imports: import glob
  import os
  from argparse import ArgumentParser
  from collections import Counter
  from typing import Dict, Optional, TextIO, Tuple
  from nemo.collections.nlp.data.text_normalization_as_tagging.utils import get_src_and_dst_for_alignment
  from nemo.utils import logging
label_desc: Use image node_id 4 for calling a global function with example usage: filter_by_vocab() without return types