def canonicalize_nts(source, target, arity):
  """Follows convention of source indexes being in order."""
  source_nts = []
  for token in source:
    if qcfg_rule.is_nt(token) and token not in source_nts:
      source_nts.append(token)
  if len(set(source_nts)) != arity:
    raise ValueError("Bad arity 2 source: %s" % (source,))
  old_to_new = {
      qcfg_rule.get_nt_index(nt): idx + 1 for idx, nt in enumerate(source_nts)
  }
  source = _swap_nt_order(source, old_to_new)
  target = _swap_nt_order(target, old_to_new)
  return source, target
Follows convention of source indexes being in order.
language/compgen/csl/induction/rule_utils.py