class_docstr: Individual fairness perturbation for dialect.
class_docstr_tok: ["Individual","fairness","perturbation","for","dialect","."]
docstr: Initialize the dialect perturbation.
  
  If mapping_file_path is not provided, (source_class, target_class)
  should be ("SAE", "AAVE").
  
  Args:
      prob: Probability of substituting a word in the original class with
          a word in the target class given that a substitution is
          available.
      source_class: The source dialect that will be substituted with
          the target dialect. Case-insensitive.
      target_class: The target dialect.
      mapping_file_path: The absolute path to a file containing the
          word mappings from the source dialect to the target dialect in
          a json format. The json dictionary must be of type
          Dict[str, List[str]]. Otherwise, the default dictionary in
          self.MAPPING_DICTS for the provided source and target classes
          will be used, if available.
docstr_tok: ["Initialize","the","dialect","perturbation",".","If","mapping_file_path","is","not","provided",",","(","source_class",",","target_class",")","should","be","(","``","SAE","''",",","``","AAVE","''",")",".","Args",":","prob",":","Probability","of","substituting","a","word","in","the","original","class","with","a","word","in","the","target","class","given","that","a","substitution","is","available",".","source_class",":","The","source","dialect","that","will","be","substituted","with","the","target","dialect",".","Case-insensitive",".","target_class",":","The","target","dialect",".","mapping_file_path",":","The","absolute","path","to","a","file","containing","the","word","mappings","from","the","source","dialect","to","the","target","dialect","in","a","json","format",".","The","json","dictionary","must","be","of","type","Dict","[","str",",","List","[","str","]","]",".","Otherwise",",","the","default","dictionary","in","self.MAPPING_DICTS","for","the","provided","source","and","target","classes","will","be","used",",","if","available","."]
code: def __init__(
      self,
      prob: float,
      source_class: str,
      target_class: str,
      mapping_file_path: Optional[str] = None,
  ):
      """Initialize the dialect perturbation.
  
      If mapping_file_path is not provided, (source_class, target_class)
      should be ("SAE", "AAVE").
  
      Args:
          prob: Probability of substituting a word in the original class with
              a word in the target class given that a substitution is
              available.
          source_class: The source dialect that will be substituted with
              the target dialect. Case-insensitive.
          target_class: The target dialect.
          mapping_file_path: The absolute path to a file containing the
              word mappings from the source dialect to the target dialect in
              a json format. The json dictionary must be of type
              Dict[str, List[str]]. Otherwise, the default dictionary in
              self.MAPPING_DICTS for the provided source and target classes
              will be used, if available.
      """
      # TODO: Update path so it is not hard-coded to benchmark_output
      # https://github.com/stanford-crfm/benchmarking/issues/493
      self.output_path: str = self.OUTPUT_PATH
      Path(self.output_path).mkdir(parents=True, exist_ok=True)
  
      # Assign parameters to instance variables
      assert 0 <= prob <= 1
      self.prob = prob
      self.source_class: str = source_class.upper()
      self.target_class: str = target_class.upper()
  
      if mapping_file_path:
          self.mapping_file_path: str = mapping_file_path
      else:
          self.mapping_file_path = self.retrieve_mapping_dict()
      self.mapping_dict: Dict[str, List[str]] = self.load_mapping_dict()
  
      # Pattern capturing any occurence of the given words in the text, surrounded by characters other than
      # alphanumeric characters and '-'. We use re.escape since the words in our dictionary may
      # contain special RegEx characters.
      words = [re.escape(w) for w in self.mapping_dict.keys()]
      words_string = "|".join(words)
      self.pattern = f"[^\\w-]({words_string})[^\\w-]"
  
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 1 to create a new DialectPerturbation object from inherited base classes: Perturbation with example: obj = DialectPerturbation(prob, source_class, target_class, mapping_file_path)