class_docstr: Individual fairness perturbation for person names.
class_docstr_tok: ["Individual","fairness","perturbation","for","person","names","."]
docstr: Substitute the names in text with persistency across `Instance` and their `Reference`s.
docstr_tok: ["Substitute","the","names","in","text","with","persistency","across","`","Instance","`","and","their","`","Reference","`","s","."]
code: def perturb_with_persistency(
      self,
      text: str,
      rng: Random,
      name_substitution_mapping: Dict[str, str],
      skipped_tokens: Set[str],
  ) -> str:
      """Substitute the names in text with persistency across `Instance` and their `Reference`s."""
      # Tokenize the text
      sep_pattern = r"([^\w])"
      tokens: List[str] = re.split(sep_pattern, text)
  
      new_tokens: List[str] = []
      for token in tokens:
          token_lowered: str = token.lower()
  
          # Find a substitution for the name, if possible
          skip: bool = (
              token_lowered in name_substitution_mapping
              or token_lowered in skipped_tokens
          )
          if not skip and token_lowered in self.source_names:
              if rng.uniform(0, 1) < self.prob:
                  name = self.get_substitute_name(token, rng)
                  if name:
                      name_substitution_mapping[token_lowered] = name
              else:
                  skipped_tokens.add(token_lowered)
  
          # Substitute the token if a substitution exist
          if token_lowered in name_substitution_mapping:
              substitution = name_substitution_mapping[token_lowered]
              token = match_case(token, substitution)
          new_tokens.append(token)
  
      return "".join(new_tokens)
  
imports: import os
  import re
  from collections import defaultdict
  from dataclasses import dataclass, replace
  from functools import reduce
  from pathlib import Path
  from random import Random
  from typing import Dict, List, Optional, Set
  from helm.benchmark.scenarios.scenario import Input, Instance, Reference, Output
  from helm.common.general import ensure_file_downloaded, ensure_directory_exists, match_case
  from .perturbation_description import PerturbationDescription
  from .perturbation import Perturbation
label_desc: Use image node_id 10 for calling the PersonNamePerturbation obj's underlying member method code with example usage: obj.perturb_with_persistency(text, rng, name_substitution_mapping, skipped_tokens) and returns: str