Views
No views yet
1import random
2
3def add_spelling_errors(text):
4 noisy_text = list(text)
5 modified_text = []
6 for i in range(len(noisy_text)):
7 if random.random() < 0.1:
8 if noisy_text[i] in ['은', '는', '이', '가','을','를']:
9 noisy_text[i] = random.choice(['은', '는', '이', '가','를','을']) # 语法
10 continue
11 elif noisy_text[i] in ['와','과']:
12 noisy_text[i] = random.choice(['와','과']) # 语法
13 continue
14 elif random.random() < 0.1:
15 # 随机插入字符
16 noisy_text.insert(i, random.choice(['하', '로', '니', '고', '었', '나']))
17 # 这里不需要增加i,因为insert操作会将插入位置之后的字符向后移动
18 #i += 1 # 移动到下一个位置,因为插入了一个字符
19
20 # 删除空格或交换字符
21 if noisy_text[i] == ' ' and random.random() < 0.1:
22 continue # 跳过空格
23
24 elif random.random() < 0.1: # 控制交换字符的概率
25 if i < len(noisy_text) - 1:
26 noisy_text[i], noisy_text[i + 1] = noisy_text[i + 1], noisy_text[i]
27
28 modified_text.append(noisy_text[i])
29
30 return ''.join(modified_text)
31