class_docstr: Individual fairness perturbation for gender terms and pronouns.
class_docstr_tok: ["Individual","fairness","perturbation","for","gender","terms","and","pronouns","."]
docstr: Substitute the occurences of word in text with its synonym with self.probability
docstr_tok: ["Substitute","the","occurences","of","word","in","text","with","its","synonym","with","self.probability"]
code: def substitute_word(
      self, text: str, word: str, synonym: str, rng: Random
  ) -> str:
      """Substitute the occurences of word in text with its synonym with self.probability"""
      # Pattern capturing any occurence of given word in the text, surrounded by non-alphanumeric characters
      pattern = f"[^\\w]({word})[^\\w]"
  
      # Substitution function
      def sub_func(m: re.Match):
          match_str = m.group(
              0
          )  # The full match (e.g. " Man ", " Man,", " Man.", "-Man.")
          match_word = m.group(1)  # Captured group (e.g. "Man")
          if rng.uniform(0, 1) < self.prob:
              syn = match_case(
                  match_word, synonym
              )  # Synoynm with matching case (e.g. "Woman")
              match_str = match_str.replace(
                  match_word, syn
              )  # Synonym placed in the matching group (e.g. " Woman ", " Woman,", " Woman.", "-Woman")
          return match_str
  
      # Execute the RegEx
      return re.sub(pattern, sub_func, text, flags=re.IGNORECASE)
  
file_path: H2O/h2o_hf/helm/src/helm/benchmark/augmentations/gender_perturbation.py
imports: from dataclasses import dataclass
  import json
  from random import Random
  import re
  from typing import Dict, List, Optional, Tuple
  from helm.common.general import match_case
  from .perturbation_description import PerturbationDescription
  from .perturbation import Perturbation
label_desc: Use image node_id 4 for calling the GenderPerturbation obj's underlying member method code with example usage: obj.substitute_word(text, word, synonym, rng) and returns: re, match_str