Adapters
File size: 1,003 Bytes
ae1cb98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

import random
import math
import string
from prompt_injection.mutators.base import PromptMutator


class TypoPromptMutator(PromptMutator):
  def __init__(self,percentage=0.05):
    self.percentage=percentage

  def mutate(self,sample:str)->str:
      return self.__add_typos_to_text(sample,self.percentage)

  def get_name(self):
      return f'TypoPromptMutator-{self.percentage}'

  def __introduce_typos(self,word):
      char_list = list(word)
      i = random.randint(0, len(char_list) - 1)
      char_list[i] = random.choice(string.ascii_lowercase)
      return ''.join(char_list)

  def __add_typos_to_text(self,text,  change_percentage=0.05):
      words = text.split()
      num_words = len(words)
      num_changes = math.ceil(num_words * change_percentage)
      words_to_change = random.sample(words, min(num_changes, num_words))
      for i, word in enumerate(words):
          if word in words_to_change:
              words[i] = self.__introduce_typos(word)

      return ' '.join(words)