class_docstr: A constraint on word substitutions which places a maximum distance
  between the embedding of the word being deleted and the word being
  inserted.
  
  Args:
      embedding (obj): Wrapper for word embedding.
      include_unknown_words (bool): Whether or not the constraint is fulfilled if the embedding of x or x_adv is unknown.
      min_cos_sim (:obj:`float`, optional): The minimum cosine similarity between word embeddings.
      max_mse_dist (:obj:`float`, optional): The maximum euclidean distance between word embeddings.
      cased (bool): Whether embedding supports uppercase & lowercase (defaults to False, or just lowercase).
      compare_against_original (bool):  If `True`, compare new `x_adv` against the original `x`. Otherwise, compare it against the previous `x_adv`.
class_docstr_tok: ["A","constraint","on","word","substitutions","which","places","a","maximum","distance","between","the","embedding","of","the","word","being","deleted","and","the","word","being","inserted",".","Args",":","embedding","(","obj",")",":","Wrapper","for","word","embedding",".","include_unknown_words","(","bool",")",":","Whether","or","not","the","constraint","is","fulfilled","if","the","embedding","of","x","or","x_adv","is","unknown",".","min_cos_sim","(",":","obj",":","`","float","`",",","optional",")",":","The","minimum","cosine","similarity","between","word","embeddings",".","max_mse_dist","(",":","obj",":","`","float","`",",","optional",")",":","The","maximum","euclidean","distance","between","word","embeddings",".","cased","(","bool",")",":","Whether","embedding","supports","uppercase","&","lowercase","(","defaults","to","False",",","or","just","lowercase",")",".","compare_against_original","(","bool",")",":","If","`","True","`",",","compare","new","`","x_adv","`","against","the","original","`","x","`",".","Otherwise",",","compare","it","against","the","previous","`","x_adv","`","."]
docstr: Returns true if (``transformed_text`` and ``reference_text``) are
  closer than ``self.min_cos_sim`` or ``self.max_mse_dist``.
docstr_tok: ["Returns","true","if","(","``","transformed_text","``","and","``","reference_text","``",")","are","closer","than","``","self.min_cos_sim","``","or","``","self.max_mse_dist","``","."]
code: def _check_constraint(self, transformed_text, reference_text):
      """Returns true if (``transformed_text`` and ``reference_text``) are
      closer than ``self.min_cos_sim`` or ``self.max_mse_dist``."""
      try:
          indices = transformed_text.attack_attrs[
              "newly_modified_indices"
          ]
      except KeyError:
          raise KeyError(
              "Cannot apply part-of-speech constraint without `newly_modified_indices`"
          )
  
      # FIXME The index i is sometimes larger than the number of tokens - 1
      if any(
          i >= len(reference_text.words)
          or i >= len(transformed_text.words)
          for i in indices
      ):
          return False
  
      for i in indices:
          ref_word = reference_text.words[i]
          transformed_word = transformed_text.words[i]
  
          if not self.cased:
              # If embedding vocabulary is all lowercase, lowercase words.
              ref_word = ref_word.lower()
              transformed_word = transformed_word.lower()
  
          try:
              ref_id = self.embedding.word2index(ref_word)
              transformed_id = self.embedding.word2index(
                  transformed_word
              )
          except KeyError:
              # This error is thrown if x or x_adv has no corresponding ID.
              if self.include_unknown_words:
                  continue
              return False
  
          # Check cosine distance.
          if self.min_cos_sim:
              cos_sim = self.get_cos_sim(ref_id, transformed_id)
              if cos_sim < self.min_cos_sim:
                  return False
          # Check MSE distance.
          if self.max_mse_dist:
              mse_dist = self.get_mse_dist(ref_id, transformed_id)
              if mse_dist > self.max_mse_dist:
                  return False
  
      return True
  
file_path: TextAttack/textattack/constraints/semantics/word_embedding_distance.py
imports: from textattack.constraints import Constraint
  from textattack.shared import AbstractWordEmbedding, WordEmbedding
  from textattack.shared.validators import transformation_consists_of_word_swaps
label_desc: Use image node_id 4 for calling the WordEmbeddingDistance obj's underlying member method code with example usage: obj._check_constraint(transformed_text, reference_text) and returns: True, False, False, False, False