class_docstr: Generate new examples by replacing words in examples.
  
  Substitutions must be of the form 'foo -> bar, spam -> eggs'.
class_docstr_tok: ["Generate","new","examples","by","replacing","words","in","examples",".","Substitutions","must","be","of","the","form","'foo","-",">","bar",",","spam","-",">","eggs","'","."]
docstr: Replace words based on replacement list.
  
  Note: If multiple fields are selected for replacement, this method will
  generate an example per field. For example, if there are two fields on which
  to perform replacement, the method will perform replacement first on one
  field to produce an example (other fields left intact), and then perform
  replacement on the second field (again copying all other fields from the
  original datum).
  
  Args:
    example: the example used for basis of generated examples.
    model: unused.
    dataset: the dataset.
    config: user-provided config properties.
    text_fields_to_replace: Opionally the fields over which the replacer runs.
  
  Returns:
    examples: a list of generated examples.
docstr_tok: ["Replace","words","based","on","replacement","list",".","Note",":","If","multiple","fields","are","selected","for","replacement",",","this","method","will","generate","an","example","per","field",".","For","example",",","if","there","are","two","fields","on","which","to","perform","replacement",",","the","method","will","perform","replacement","first","on","one","field","to","produce","an","example","(","other","fields","left","intact",")",",","and","then","perform","replacement","on","the","second","field","(","again","copying","all","other","fields","from","the","original","datum",")",".","Args",":","example",":","the","example","used","for","basis","of","generated","examples",".","model",":","unused",".","dataset",":","the","dataset",".","config",":","user-provided","config","properties",".","text_fields_to_replace",":","Opionally","the","fields","over","which","the","replacer","runs",".","Returns",":","examples",":","a","list","of","generated","examples","."]
code: def generate(
      self,
      example: _JsonDict,
      model: lit_model.Model,
      dataset: lit_dataset.Dataset,
      config: Optional[_JsonDict] = None,
      text_fields_to_replace: Optional[Sequence[str]] = None,
  ) -> list[_JsonDict]:
      """Replace words based on replacement list.
  
      Note: If multiple fields are selected for replacement, this method will
      generate an example per field. For example, if there are two fields on which
      to perform replacement, the method will perform replacement first on one
      field to produce an example (other fields left intact), and then perform
      replacement on the second field (again copying all other fields from the
      original datum).
  
      Args:
        example: the example used for basis of generated examples.
        model: unused.
        dataset: the dataset.
        config: user-provided config properties.
        text_fields_to_replace: Opionally the fields over which the replacer runs.
  
      Returns:
        examples: a list of generated examples.
      """
      del model  # Unused.
  
      config: _JsonDict = config or {}
  
      ignore_casing = config.get(IGNORE_CASING_KEY, True)
      subs_string = config.get(SUBSTITUTIONS_KEY, None)
      if subs_string:
          replacements = self.parse_subs_string(
              subs_string, ignore_casing=ignore_casing
          )
      else:
          replacements = self.default_replacements
  
      # If replacements dictionary is empty, do not attempt to match.
      if not replacements:
          return []
  
      replacement_regex = self._get_replacement_pattern(
          replacements, ignore_casing=ignore_casing
      )
  
      # If config key is missing, generate no examples.
      if text_fields_to_replace is None:
          text_fields_to_replace = self._compute_fields_to_replace(
              dataset, config
          )
      if not text_fields_to_replace:
          return []
  
      new_examples = []
      for text_field in text_fields_to_replace:
          for new_val in self.generate_counterfactuals(
              example[text_field],
              replacement_regex,
              replacements,
              ignore_casing=ignore_casing,
          ):
              new_examples.append(
                  utils.make_modified_input(
                      example, {text_field: new_val}, "WordReplacer"
                  )
              )
  
      return new_examples
  
file_path: lit/lit_nlp/components/word_replacer.py
imports: from collections.abc import Iterator, Sequence
  import re
  from typing import Optional
  from absl import logging
  from lit_nlp.api import components
  from lit_nlp.api import dataset
  from lit_nlp.api import model
  from lit_nlp.api import types
  from lit_nlp.lib import utils
label_desc: Use image node_id 7 for calling the WordReplacer obj's underlying member method code with example usage: obj.generate(example, model, dataset, config, text_fields_to_replace) and returns: new_examples, list, list