class_docstr: Individual fairness perturbation for dialect.
class_docstr_tok: ["Individual","fairness","perturbation","for","dialect","."]
docstr: Substitute the source dialect in text with the target dialect with probability self.prob.
docstr_tok: ["Substitute","the","source","dialect","in","text","with","the","target","dialect","with","probability","self.prob","."]
code: def perturb(self, text: str, rng: Random) -> str:
      """Substitute the source dialect in text with the target dialect with probability self.prob."""
  
      # Substitution function
      def sub_func(m: re.Match):
          match_str = m.group(
              0
          )  # The full match (e.g. " With ", " With,", " With.")
          word = m.group(1)  # Captured group (e.g. "With")
          if rng.uniform(0, 1) < self.prob:
              synonyms = self.mapping_dict[word.lower()]
              synonym = rng.choice(synonyms)  # Synonym (e.g. "wit")
              synonym = match_case(
                  word, synonym
              )  # Synoynm with matching case (e.g. "Wit")
              match_str = match_str.replace(
                  word, synonym
              )  # Synonym placed in the matching group (e.g. " Wit ", " Wit,", " Wit.")
          return match_str
  
      # Execute the RegEx
      return re.sub(self.pattern, sub_func, text, flags=re.IGNORECASE)
  
file_path: H2O/h2o_hf/helm/src/helm/benchmark/augmentations/dialect_perturbation.py
imports: from dataclasses import dataclass
  import json
  import os
  from random import Random
  import re
  from pathlib import Path
  from typing import Dict, Optional, List
  from helm.common.general import match_case, ensure_file_downloaded
  from .perturbation_description import PerturbationDescription
  from .perturbation import Perturbation
label_desc: Use image node_id 5 for calling the DialectPerturbation obj's underlying member method code with example usage: obj.perturb(text, rng) and returns: re, match_str