Пример #1
0
 def mask_instance_decorator(self,
                             args: ClassifierArgs,
                             instance: InputInstance,
                             numbers: int = 1,
                             return_indexes: bool = False):
     if self.forbidden_words is not None:
         forbidden_index = mask_forbidden_index(
             instance.perturbable_sentence(), self.forbidden_words)
         return mask_instance(instance, args.sparse_mask_rate,
                              self.tokenizer.mask_token, numbers,
                              return_indexes, forbidden_index)
     else:
         return mask_instance(instance, args.sparse_mask_rate,
                              self.tokenizer.mask_token, numbers,
                              return_indexes)
Пример #2
0
def mask_instance(instance: InputInstance,
                  rate: float,
                  token: str,
                  nums: int = 1,
                  return_indexes: bool = False,
                  forbidden_indexes: List[int] = None,
                  random_probs: List[float] = None) -> List[InputInstance]:
    sentence = instance.perturbable_sentence()
    results = mask_sentence(sentence, rate, token, nums, return_indexes,
                            forbidden_indexes, random_probs)
    if return_indexes:
        mask_sentences_list = results[0]
    else:
        mask_sentences_list = results
    tmp_instances = [
        InputInstance.from_instance_and_perturb_sentence(instance, sent)
        for sent in mask_sentences_list
    ]
    if return_indexes:
        return tmp_instances, results[1]
    else:
        return tmp_instances