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 each token and yield a new string each time that succeeds.
  
  Note: ignores casing.
  
  Args:
    text: input sentence
    replacement_regex: The regexp string used to find source words.
    replacements: Dictionary of source and replacement tokens.
    ignore_casing: Ignore casing if this is true.
  
  Yields:
    counterfactual: a string
docstr_tok: ["Replace","each","token","and","yield","a","new","string","each","time","that","succeeds",".","Note",":","ignores","casing",".","Args",":","text",":","input","sentence","replacement_regex",":","The","regexp","string","used","to","find","source","words",".","replacements",":","Dictionary","of","source","and","replacement","tokens",".","ignore_casing",":","Ignore","casing","if","this","is","true",".","Yields",":","counterfactual",":","a","string"]
code: def generate_counterfactuals(
      self,
      text: str,
      replacement_regex: re.Pattern[str],
      replacements: dict[str, list[str]],
      ignore_casing: bool = True,
  ) -> Iterator[str]:
      """Replace each token and yield a new string each time that succeeds.
  
      Note: ignores casing.
  
      Args:
        text: input sentence
        replacement_regex: The regexp string used to find source words.
        replacements: Dictionary of source and replacement tokens.
        ignore_casing: Ignore casing if this is true.
  
      Yields:
        counterfactual: a string
      """
      # If replacement_regex is empty do not attempt to match.
      if not replacement_regex.pattern:
          return
  
      for s in replacement_regex.finditer(text):
          start, end = s.span()
          token = text[start:end]
          if ignore_casing:
              token = token.lower()
          # Yield one output for each target.
          for tgt in replacements[token]:
              yield text[:start] + tgt + text[end:]
  
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 5 for calling the WordReplacer obj's underlying member method code with example usage: obj.generate_counterfactuals(text, replacement_regex, replacements, ignore_casing) without return types