def METHOD_NAME(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