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: Generate replacement pattern for whole word match.
  
  If the source word does not end or begin with non-word characters
  (e.g. punctuation) then do a whole word match by using the special word
  boundary character "\b". This allows us to replace whole words ignoring
  punctuation around them (e.g. "cat." becomes "dog." with rule "cat"->"dog").
  However, if the source word has a non-word character at its edges this
  fails. For example for the rule "."-> "," it would not find "cat. " as there
  is no boundary between "." and " ". Therefore, for patterns with punctuation
  at the word boundaries, we ignore the whole word match and replace all
  instances. So "cat.dog" will become "dogdog" for "cat."->"dog" instead of
  being ignored. Also "." -> "," will replace all instances of "." with ",".
  
  Args:
    replacements: A dict of word replacements
    ignore_casing: Ignore casing for source words if True.
  
  Returns:
    regexp_pattern: Compiled regexp pattern used to find source words in
                    replacements.
docstr_tok: ["Generate","replacement","pattern","for","whole","word","match",".","If","the","source","word","does","not","end","or","begin","with","non-word","characters","(","e.g",".","punctuation",")","then","do","a","whole","word","match","by","using","the","special","word","boundary","character","``","\\b","''",".","This","allows","us","to","replace","whole","words","ignoring","punctuation","around","them","(","e.g",".","``","cat",".","''","becomes","``","dog",".","''","with","rule","``","cat","''","-",">","''","dog","''",")",".","However",",","if","the","source","word","has","a","non-word","character","at","its","edges","this","fails",".","For","example","for","the","rule","``",".","``","-",">","``",",","''","it","would","not","find","``","cat.","``","as","there","is","no","boundary","between","``",".","''","and","``","``",".","Therefore",",","for","patterns","with","punctuation","at","the","word","boundaries",",","we","ignore","the","whole","word","match","and","replace","all","instances",".","So","``","cat.dog","''","will","become","``","dogdog","''","for","``","cat",".","``","-",">","''","dog","''","instead","of","being","ignored",".","Also","``",".","''","-",">","``",",","''","will","replace","all","instances","of","``",".","''","with","``",",","''",".","Args",":","replacements",":","A","dict","of","word","replacements","ignore_casing",":","Ignore","casing","for","source","words","if","True",".","Returns",":","regexp_pattern",":","Compiled","regexp","pattern","used","to","find","source","words","in","replacements","."]
code: def _get_replacement_pattern(
      self,
      replacements: dict[str, list[str]],
      ignore_casing: bool = True,
  ) -> re.Pattern[str]:
      r"""Generate replacement pattern for whole word match.
  
      If the source word does not end or begin with non-word characters
      (e.g. punctuation) then do a whole word match by using the special word
      boundary character "\b". This allows us to replace whole words ignoring
      punctuation around them (e.g. "cat." becomes "dog." with rule "cat"->"dog").
      However, if the source word has a non-word character at its edges this
      fails. For example for the rule "."-> "," it would not find "cat. " as there
      is no boundary between "." and " ". Therefore, for patterns with punctuation
      at the word boundaries, we ignore the whole word match and replace all
      instances. So "cat.dog" will become "dogdog" for "cat."->"dog" instead of
      being ignored. Also "." -> "," will replace all instances of "." with ",".
  
      Args:
        replacements: A dict of word replacements
        ignore_casing: Ignore casing for source words if True.
  
      Returns:
        regexp_pattern: Compiled regexp pattern used to find source words in
                        replacements.
      """
      re_strings = []
      for s in replacements:
          pattern_str = r"\b%s\b" % re.escape(s)
          # If the source word ends or begins with a non-word character (see above.)
          if not re.search(pattern_str, s):
              pattern_str = r"%s" % re.escape(s)
          re_strings.append(pattern_str)
  
      casing_flag = re.IGNORECASE if ignore_casing else 0
  
      return re.compile("|".join(re_strings), casing_flag)
  
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 3 for calling the WordReplacer obj's underlying member method code with example usage: obj._get_replacement_pattern(replacements, ignore_casing) and returns: re