class_docstr: Generate potential replacements for a word using a masked language
  model.
  
  Based off of following papers
      - "Robustness to Modification with Shared Words in Paraphrase Identification" (Shi et al., 2019) https://arxiv.org/abs/1909.02560
      - "BAE: BERT-based Adversarial Examples for Text Classification" (Garg et al., 2020) https://arxiv.org/abs/2004.01970
      - "BERT-ATTACK: Adversarial Attack Against BERT Using BERT" (Li et al, 2020) https://arxiv.org/abs/2004.09984
      - "CLARE: Contextualized Perturbation for Textual Adversarial Attack" (Li et al, 2020): https://arxiv.org/abs/2009.07502
  
  BAE and CLARE simply masks the word we want to replace and selects replacements predicted by the masked language model.
  
  BERT-Attack instead performs replacement on token level. For words that are consisted of two or more sub-word tokens,
      it takes the top-K replacements for seach sub-word token and produces all possible combinations of the top replacments.
      Then, it selects the top-K combinations based on their perplexity calculated using the masked language model.
  
  Choose which method to use by specifying "bae" or "bert-attack" for `method` argument.
  
  Args:
      method (str): the name of replacement method (e.g. "bae", "bert-attack")
      masked_language_model (Union[str|transformers.AutoModelForMaskedLM]): Either the name of pretrained masked language model from `transformers` model hub
          or the actual model. Default is `bert-base-uncased`.
      tokenizer (obj): The tokenizer of the corresponding model. If you passed in name of a pretrained model for `masked_language_model`,
          you can skip this argument as the correct tokenizer can be infered from the name. However, if you're passing the actual model, you must
          provide a tokenizer.
      max_length (int): the max sequence length the masked language model is designed to work with. Default is 512.
      window_size (int): The number of surrounding words to include when making top word prediction.
          For each word to swap, we take `window_size // 2` words to the left and `window_size // 2` words to the right and pass the text within the window
          to the masked language model. Default is `float("inf")`, which is equivalent to using the whole text.
      max_candidates (int): maximum number of candidates to consider as replacements for each word. Replacements are ranked by model's confidence.
      min_confidence (float): minimum confidence threshold each replacement word must pass.
      batch_size (int): Size of batch for "bae" replacement method.
class_docstr_tok: ["Generate","potential","replacements","for","a","word","using","a","masked","language","model",".","Based","off","of","following","papers","-","``","Robustness","to","Modification","with","Shared","Words","in","Paraphrase","Identification","''","(","Shi","et","al.",",","2019",")","https",":","\/\/arxiv.org\/abs\/1909.02560","-","``","BAE",":","BERT-based","Adversarial","Examples","for","Text","Classification","''","(","Garg","et","al.",",","2020",")","https",":","\/\/arxiv.org\/abs\/2004.01970","-","``","BERT-ATTACK",":","Adversarial","Attack","Against","BERT","Using","BERT","''","(","Li","et","al",",","2020",")","https",":","\/\/arxiv.org\/abs\/2004.09984","-","``","CLARE",":","Contextualized","Perturbation","for","Textual","Adversarial","Attack","''","(","Li","et","al",",","2020",")",":","https",":","\/\/arxiv.org\/abs\/2009.07502","BAE","and","CLARE","simply","masks","the","word","we","want","to","replace","and","selects","replacements","predicted","by","the","masked","language","model",".","BERT-Attack","instead","performs","replacement","on","token","level",".","For","words","that","are","consisted","of","two","or","more","sub-word","tokens",",","it","takes","the","top-K","replacements","for","seach","sub-word","token","and","produces","all","possible","combinations","of","the","top","replacments",".","Then",",","it","selects","the","top-K","combinations","based","on","their","perplexity","calculated","using","the","masked","language","model",".","Choose","which","method","to","use","by","specifying","``","bae","''","or","``","bert-attack","''","for","`","method","`","argument",".","Args",":","method","(","str",")",":","the","name","of","replacement","method","(","e.g",".","``","bae","''",",","``","bert-attack","''",")","masked_language_model","(","Union","[","str|transformers.AutoModelForMaskedLM","]",")",":","Either","the","name","of","pretrained","masked","language","model","from","`","transformers","`","model","hub","or","the","actual","model",".","Default","is","`","bert-base-uncased","`",".","tokenizer","(","obj",")",":","The","tokenizer","of","the","corresponding","model",".","If","you","passed","in","name","of","a","pretrained","model","for","`","masked_language_model","`",",","you","can","skip","this","argument","as","the","correct","tokenizer","can","be","infered","from","the","name",".","However",",","if","you","'re","passing","the","actual","model",",","you","must","provide","a","tokenizer",".","max_length","(","int",")",":","the","max","sequence","length","the","masked","language","model","is","designed","to","work","with",".","Default","is","512.","window_size","(","int",")",":","The","number","of","surrounding","words","to","include","when","making","top","word","prediction",".","For","each","word","to","swap",",","we","take","`","window_size","\/\/","2","`","words","to","the","left","and","`","window_size","\/\/","2","`","words","to","the","right","and","pass","the","text","within","the","window","to","the","masked","language","model",".","Default","is","`","float","(","``","inf","''",")","`",",","which","is","equivalent","to","using","the","whole","text",".","max_candidates","(","int",")",":","maximum","number","of","candidates","to","consider","as","replacements","for","each","word",".","Replacements","are","ranked","by","model","'s","confidence",".","min_confidence","(","float",")",":","minimum","confidence","threshold","each","replacement","word","must","pass",".","batch_size","(","int",")",":","Size","of","batch","for","``","bae","''","replacement","method","."]
code: def _get_transformations(self, current_text, indices_to_modify):
      indices_to_modify = list(indices_to_modify)
      if self.method == "bert-attack":
          current_inputs = self._encode_text(current_text.text)
          with torch.no_grad():
              pred_probs = self._language_model(**current_inputs)[0][0]
          top_probs, top_ids = torch.topk(
              pred_probs, self.max_candidates
          )
          id_preds = top_ids.cpu()
          masked_lm_logits = pred_probs.cpu()
  
          transformed_texts = []
  
          for i in indices_to_modify:
              word_at_index = current_text.words[i]
              replacement_words = self._bert_attack_replacement_words(
                  current_text,
                  i,
                  id_preds=id_preds,
                  masked_lm_logits=masked_lm_logits,
              )
  
              for r in replacement_words:
                  r = r.strip("Ġ")
                  if r != word_at_index:
                      transformed_texts.append(
                          current_text.replace_word_at_index(i, r)
                      )
  
          return transformed_texts
  
      elif self.method == "bae":
          replacement_words = self._bae_replacement_words(
              current_text, indices_to_modify
          )
          transformed_texts = []
          for i in range(len(replacement_words)):
              index_to_modify = indices_to_modify[i]
              word_at_index = current_text.words[index_to_modify]
              for word in replacement_words[i]:
                  word = word.strip("Ġ")
                  if (
                      word != word_at_index
                      and re.search("[a-zA-Z]", word)
                      and len(utils.words_from_text(word)) == 1
                  ):
                      transformed_texts.append(
                          current_text.replace_word_at_index(
                              index_to_modify, word
                          )
                      )
          return transformed_texts
      else:
          raise ValueError(
              f"Unrecognized value {self.method} for `self.method`."
          )
  
file_path: TextAttack/textattack/transformations/word_swaps/word_swap_masked_lm.py
imports: import itertools
  import re
  import torch
  from transformers import AutoModelForMaskedLM, AutoTokenizer
  from textattack.shared import utils
  from .word_swap import WordSwap
label_desc: Use image node_id 5 for calling the WordSwapMaskedLM obj's underlying member method code with example usage: obj._get_transformations(current_text, indices_to_modify) and returns: transformed_texts, transformed_texts