class_docstr: Constraints word swaps to only swap words with the same part of speech.
  Uses the NLTK universal part-of-speech tagger by default. An implementation
  of `<https://arxiv.org/abs/1907.11932>`_ adapted from
  `<https://github.com/jind11/TextFooler>`_.
  
  POS taggers from Flair `<https://github.com/flairNLP/flair>`_ and
  Stanza `<https://github.com/stanfordnlp/stanza>`_ are also available
  
  Args:
      tagger_type (str): Name of the tagger to use (available choices: "nltk", "flair", "stanza").
      tagset (str): tagset to use for POS tagging (e.g. "universal")
      allow_verb_noun_swap (bool): If `True`, allow verbs to be swapped with nouns and vice versa.
      compare_against_original (bool): If `True`, compare against the original text.
          Otherwise, compare against the most recent text.
      language_nltk: Language to be used for nltk POS-Tagger
          (available choices: "eng", "rus")
      language_stanza: Language to be used for stanza POS-Tagger
          (available choices: https://stanfordnlp.github.io/stanza/available_models.html)
class_docstr_tok: ["Constraints","word","swaps","to","only","swap","words","with","the","same","part","of","speech",".","Uses","the","NLTK","universal","part-of-speech","tagger","by","default",".","An","implementation","of","`","<","https",":","\/\/arxiv.org\/abs\/1907.11932",">","`","_","adapted","from","`","<","https",":","\/\/github.com\/jind11\/TextFooler",">","`","_",".","POS","taggers","from","Flair","`","<","https",":","\/\/github.com\/flairNLP\/flair",">","`","_","and","Stanza","`","<","https",":","\/\/github.com\/stanfordnlp\/stanza",">","`","_","are","also","available","Args",":","tagger_type","(","str",")",":","Name","of","the","tagger","to","use","(","available","choices",":","``","nltk","''",",","``","flair","''",",","``","stanza","''",")",".","tagset","(","str",")",":","tagset","to","use","for","POS","tagging","(","e.g",".","``","universal","''",")","allow_verb_noun_swap","(","bool",")",":","If","`","True","`",",","allow","verbs","to","be","swapped","with","nouns","and","vice","versa",".","compare_against_original","(","bool",")",":","If","`","True","`",",","compare","against","the","original","text",".","Otherwise",",","compare","against","the","most","recent","text",".","language_nltk",":","Language","to","be","used","for","nltk","POS-Tagger","(","available","choices",":","``","eng","''",",","``","rus","''",")","language_stanza",":","Language","to","be","used","for","stanza","POS-Tagger","(","available","choices",":","https",":","\/\/stanfordnlp.github.io\/stanza\/available_models.html",")"]
code: def _check_constraint(self, transformed_text, reference_text):
      try:
          indices = transformed_text.attack_attrs[
              "newly_modified_indices"
          ]
      except KeyError:
          raise KeyError(
              "Cannot apply part-of-speech constraint without `newly_modified_indices`"
          )
  
      for i in indices:
          reference_word = reference_text.words[i]
          transformed_word = transformed_text.words[i]
          before_ctx = reference_text.words[max(i - 4, 0) : i]
          after_ctx = reference_text.words[
              i + 1 : min(i + 4, len(reference_text.words))
          ]
          ref_pos = self._get_pos(before_ctx, reference_word, after_ctx)
          replace_pos = self._get_pos(
              before_ctx, transformed_word, after_ctx
          )
          if not self._can_replace_pos(ref_pos, replace_pos):
              return False
  
      return True
  
file_path: TextAttack/textattack/constraints/grammaticality/part_of_speech.py
imports: import flair
  from flair.data import Sentence
  from flair.models import SequenceTagger
  import lru
  import nltk
  import textattack
  from textattack.constraints import Constraint
  from textattack.shared.utils import LazyLoader, device
  from textattack.shared.validators import transformation_consists_of_word_swaps
label_desc: Use image node_id 5 for calling the PartOfSpeech obj's underlying member method code with example usage: obj._check_constraint(transformed_text, reference_text) and returns: True, False