class_docstr: Individual fairness perturbation for gender terms and pronouns.
class_docstr_tok: ["Individual","fairness","perturbation","for","gender","terms","and","pronouns","."]
docstr: Initialize the gender perturbation.
  
  Args:
      mode: The mode of the gender perturbation, must be one of
          "terms" or "pronouns".
      prob: Probability of substituting a word in the source class with
          a word in the target class given that a substitution is
          available.
      source_class: The source gender that will be substituted with
          the target gender. If mapping_file_path is provided, the source
          class must be one of the genders in it. If not, it must be
          exactly one of `male`, `female`, and `neutral. Case-insensitive.
      target_class: Same as the source class, but for the target gender.
      mapping_file_path: The absolute path to a file containing the
          word mappings from the source gender to the target gender in
          a json format. The json dictionary must be of type
          List[List[str, ...]]. It is assumed that 0th index of the inner
          lists correspond to the 0th gender, 1st index to 1st gender
          and so on. All word cases are lowered.
          If mapping_file_path is None, the default dictionary in
          self.MODE_TO_MAPPINGS for the provided source and target classes
          will be used, if available.
      mapping_file_genders: The genders in the mapping supplied in the
          mapping_file_path. The inner lists read from mapping_file_path
          should have the same length as the mapping_file_genders. The
          order of the genders is assumed to reflect the order in the
          mapping_file_path. Must not be None if mapping_file_path
          is set. All word cases are lowered.
      bidirectional: Whether we should apply the perturbation in both
          directions. If we need to perturb a word, we first check if it
          is in list of source_class words, and replace it with the
          corresponding target_class word if so. If the word isn't in the
          source_class words, we check if it is in the target_class words,
          and replace it with the corresponding source_class word if so.
docstr_tok: ["Initialize","the","gender","perturbation",".","Args",":","mode",":","The","mode","of","the","gender","perturbation",",","must","be","one","of","``","terms","''","or","``","pronouns","''",".","prob",":","Probability","of","substituting","a","word","in","the","source","class","with","a","word","in","the","target","class","given","that","a","substitution","is","available",".","source_class",":","The","source","gender","that","will","be","substituted","with","the","target","gender",".","If","mapping_file_path","is","provided",",","the","source","class","must","be","one","of","the","genders","in","it",".","If","not",",","it","must","be","exactly","one","of","`","male","`",",","`","female","`",",","and","`","neutral",".","Case-insensitive",".","target_class",":","Same","as","the","source","class",",","but","for","the","target","gender",".","mapping_file_path",":","The","absolute","path","to","a","file","containing","the","word","mappings","from","the","source","gender","to","the","target","gender","in","a","json","format",".","The","json","dictionary","must","be","of","type","List","[","List","[","str",",","...","]","]",".","It","is","assumed","that","0th","index","of","the","inner","lists","correspond","to","the","0th","gender",",","1st","index","to","1st","gender","and","so","on",".","All","word","cases","are","lowered",".","If","mapping_file_path","is","None",",","the","default","dictionary","in","self.MODE_TO_MAPPINGS","for","the","provided","source","and","target","classes","will","be","used",",","if","available",".","mapping_file_genders",":","The","genders","in","the","mapping","supplied","in","the","mapping_file_path",".","The","inner","lists","read","from","mapping_file_path","should","have","the","same","length","as","the","mapping_file_genders",".","The","order","of","the","genders","is","assumed","to","reflect","the","order","in","the","mapping_file_path",".","Must","not","be","None","if","mapping_file_path","is","set",".","All","word","cases","are","lowered",".","bidirectional",":","Whether","we","should","apply","the","perturbation","in","both","directions",".","If","we","need","to","perturb","a","word",",","we","first","check","if","it","is","in","list","of","source_class","words",",","and","replace","it","with","the","corresponding","target_class","word","if","so",".","If","the","word","is","n't","in","the","source_class","words",",","we","check","if","it","is","in","the","target_class","words",",","and","replace","it","with","the","corresponding","source_class","word","if","so","."]
code: def __init__(
      self,
      mode: str,
      prob: float,
      source_class: str,
      target_class: str,
      mapping_file_path: Optional[str] = None,
      mapping_file_genders: List[str] = None,
      bidirectional: bool = False,
  ):
      """Initialize the gender perturbation.
  
      Args:
          mode: The mode of the gender perturbation, must be one of
              "terms" or "pronouns".
          prob: Probability of substituting a word in the source class with
              a word in the target class given that a substitution is
              available.
          source_class: The source gender that will be substituted with
              the target gender. If mapping_file_path is provided, the source
              class must be one of the genders in it. If not, it must be
              exactly one of `male`, `female`, and `neutral. Case-insensitive.
          target_class: Same as the source class, but for the target gender.
          mapping_file_path: The absolute path to a file containing the
              word mappings from the source gender to the target gender in
              a json format. The json dictionary must be of type
              List[List[str, ...]]. It is assumed that 0th index of the inner
              lists correspond to the 0th gender, 1st index to 1st gender
              and so on. All word cases are lowered.
              If mapping_file_path is None, the default dictionary in
              self.MODE_TO_MAPPINGS for the provided source and target classes
              will be used, if available.
          mapping_file_genders: The genders in the mapping supplied in the
              mapping_file_path. The inner lists read from mapping_file_path
              should have the same length as the mapping_file_genders. The
              order of the genders is assumed to reflect the order in the
              mapping_file_path. Must not be None if mapping_file_path
              is set. All word cases are lowered.
          bidirectional: Whether we should apply the perturbation in both
              directions. If we need to perturb a word, we first check if it
              is in list of source_class words, and replace it with the
              corresponding target_class word if so. If the word isn't in the
              source_class words, we check if it is in the target_class words,
              and replace it with the corresponding source_class word if so.
      """
      # Assign parameters to instance variables
      assert mode in self.MODES
      self.mode = mode
  
      assert 0 <= prob <= 1
      self.prob = prob
  
      self.source_class: str = source_class.lower()
      self.target_class: str = target_class.lower()
      self.mapping_file_path: Optional[str] = mapping_file_path
      self.bidirectional: bool = bidirectional
  
      # Get mappings and self.genders
      mappings: List[Tuple[str, ...]] = self.MODE_TO_MAPPINGS[self.mode]
      self.genders = self.GENDERS
      if self.mapping_file_path and mapping_file_genders:
          mappings = self.load_mappings(self.mapping_file_path)
          self.genders = [g.lower() for g in mapping_file_genders]
      assert (
          mappings
          and self.source_class in self.genders
          and self.target_class in self.genders
      )
      assert all([len(m) == len(self.genders) for m in mappings])
  
      # Get source and target words
      gender_to_ind: Dict[str, int] = {
          gender: ind for ind, gender in enumerate(self.genders)
      }
      word_lists = list(zip(*mappings))
      self.source_words: List[str] = list(
          word_lists[gender_to_ind[self.source_class]]
      )
      self.target_words: List[str] = list(
          word_lists[gender_to_ind[self.target_class]]
      )
  
      # Get word_synonym_pairs
      self.word_synonym_pairs = list(
          zip(self.source_words, self.target_words)
      )
  
      # If self.bidirectional flag is set, extend the pairs list
      if self.bidirectional:
          new_pairs = list(zip(self.target_words, self.source_words))
          self.word_synonym_pairs.extend(new_pairs)
  
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 1 to create a new GenderPerturbation object from inherited base classes: Perturbation with example: obj = GenderPerturbation(mode, prob, source_class, target_class, mapping_file_path, mapping_file_genders, bidirectional)