ShynBui commited on
Commit
f462bd2
·
1 Parent(s): 4ff8b83
__pycache__/fomula.cpython-310.pyc CHANGED
Binary files a/__pycache__/fomula.cpython-310.pyc and b/__pycache__/fomula.cpython-310.pyc differ
 
__pycache__/utils.cpython-310.pyc CHANGED
Binary files a/__pycache__/utils.cpython-310.pyc and b/__pycache__/utils.cpython-310.pyc differ
 
app.py CHANGED
@@ -1,55 +1,49 @@
1
  import gradio as gr
 
2
 
3
  import fomula
4
  from utils import *
5
 
6
  def final_result(input_text):
7
- # list_text_response = []
8
- #
9
- # ##character_replacement
10
- # input_text_process = character_replacement(input_text, error_rate=0.1)
11
- # list_text_response.append(input_text_process)
12
- # ##character_insertion
13
- # input_text_process = character_insertion(input_text, error_rate=0.01)
14
- # list_text_response.append(input_text_process)
15
- # ##character_deletion
16
- # input_text_process = character_deletion(input_text, error_rate=0.01)
17
- # list_text_response.append(input_text_process)
18
- # ##character_transposition
19
- # input_text_process = character_transposition(input_text, error_rate=0.03)
20
- # list_text_response.append(input_text_process)
21
- # ##homophone_replacement
22
- # input_text_process = homophone_replacement(input_text, error_rate=0.12)
23
- # list_text_response.append(input_text_process)
24
- # ## common_misspelling_replacement
25
- # input_text_process = common_misspelling_replacement(input_text)
26
- # list_text_response.append(input_text_process)
27
- # ##similar_character_replacement
28
- # input_text_process = similar_character_replacement(input_text)
29
- # list_text_response.append(input_text_process)
30
- # ##random_space_insertion
31
- # input_text_process = random_space_insertion(input_text)
32
- # list_text_response.append(input_text_process)
33
- # string_text_response = '\n'.join(list_text_response)
 
 
 
 
 
 
 
 
 
34
 
35
- ##character_replacement
36
- input_text_process = character_replacement(input_text, error_rate=0.01)
37
- ##character_insertion
38
- input_text_process = character_insertion(input_text_process, error_rate=0.01)
39
- ##character_deletion
40
- input_text_process = character_deletion(input_text_process, error_rate=0.01)
41
- ##character_transposition
42
- input_text_process = character_transposition(input_text_process, error_rate=0.01)
43
- ##homophone_replacement
44
- input_text_process = homophone_replacement(input_text_process, error_rate=0.01)
45
- ## common_misspelling_replacement
46
- input_text_process = common_misspelling_replacement(input_text_process, error_rate=0.01)
47
- ##similar_character_replacement
48
- input_text_process = similar_character_replacement(input_text_process, error_rate=0.01)
49
- ##random_space_insertion
50
- input_text_process = random_space_insertion(input_text_process, error_rate=0.01)
51
- ##random_space_removal
52
- input_text_process = random_space_removal(input_text_process)
53
 
54
  string_text_response = input_text_process
55
  return str(string_text_response)
 
1
  import gradio as gr
2
+ import random
3
 
4
  import fomula
5
  from utils import *
6
 
7
  def final_result(input_text):
8
+ total_change = 0
9
+ num_change = 0
10
+ list_do = random.sample(range(10), 10)
11
+ input_text_process = input_text
12
+ for i in list_do:
13
+ if i == 0:
14
+ ##character_replacement
15
+ input_text_process, num_change = character_replacement(input_text_process, total_change / len(input_text), error_rate=0.02)
16
+ elif i == 1:
17
+ ##character_insertion
18
+ input_text_process, num_change = character_insertion(input_text_process,total_change / len(input_text), error_rate=0.03)
19
+ elif i == 2:
20
+ ##character_deletion
21
+ input_text_process, num_change = character_deletion(input_text_process,total_change / len(input_text), error_rate=0.03)
22
+ elif i == 3:
23
+ ##character_transposition
24
+ input_text_process, num_change = character_transposition(input_text_process,total_change / len(input_text), error_rate=0.01)
25
+ elif i == 4:
26
+ ##homophone_replacement
27
+ input_text_process, num_change = homophone_replacement(input_text_process,total_change / len(input_text), error_rate=0.01)
28
+ elif i == 5:
29
+ ## common_misspelling_replacement
30
+ input_text_process, num_change = common_misspelling_replacement(input_text_process,total_change / len(input_text), error_rate=0.01)
31
+ elif i == 6:
32
+ ##similar_character_replacement
33
+ input_text_process, num_change = similar_character_replacement(input_text_process,total_change / len(input_text), error_rate=0.01)
34
+ elif i == 7:
35
+ ##random_space_insertion
36
+ input_text_process, num_change = random_space_insertion(input_text_process,total_change / len(input_text), error_rate=0.01)
37
+ elif i == 8:
38
+ ##random_space_removal
39
+ input_text_process, num_change = random_space_removal(input_text_process, total_change / len(input_text), error_rate=0.03)
40
+ elif i == 9:
41
+ ##remove_vietnamese_accents
42
+ input_text_process, num_change = remove_vietnamese_accents(input_text_process, total_change / len(input_text), error_rate=0.03)
43
+ total_change = total_change + num_change
44
 
45
+ print("Total change: ", total_change)
46
+ print("Tỷ lệ", total_change / len(input_text))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  string_text_response = input_text_process
49
  return str(string_text_response)
fomula.py CHANGED
@@ -5,14 +5,89 @@ import random
5
  import math
6
 
7
  '''
8
- AP = a.e^(bx)
9
- e: Tỉ lệ lỗi trong 1 văn bản: 0.5% - 10%
10
- AP: Tỉ lệ sửa (0.1 <= AP <= 0.5)
11
  a: weight (0.5442)
12
  b: bias (-16.94145)
 
 
 
 
13
  '''
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  def AP_fomula(a = 0.5442, b = -16.94145, error_rate=0.1):
 
 
 
 
 
16
  augmentation_probability = a * math.pow(np.e, b * error_rate)
17
 
18
  return augmentation_probability
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  import math
6
 
7
  '''
8
+ AP = a.ER^(bx) * sigmoidP(x)
9
+ ER: Tỉ lệ lỗi trong 1 phép tạo lỗi: 0.5% - 10%
10
+ AP (augmentation_probability) : Tỉ lệ sửa (0.1 <= AP <= 0.5)
11
  a: weight (0.5442)
12
  b: bias (-16.94145)
13
+
14
+
15
+ SER: Tỷ lệ lỗi đã thêm (mong muốn: < 15% - tối đa <= 30%)
16
+
17
  '''
18
 
19
+ ER_min = 0.01
20
+ ER_max = 0.1
21
+
22
+ def find_a_b_for_AP(min_AP = 0.5, max_AP = 1):
23
+
24
+ '''
25
+
26
+ :param min_AP:
27
+ :param max_AP:
28
+ :return: a, b
29
+
30
+ {a * e^(b * ER_min) = max_AP
31
+ {a * e^(b * ER_max) = min_AP
32
+
33
+ (ER_max - ER_min) * b = ln(min_AP / max_AP)
34
+ '''
35
+ exponential = ER_max - ER_min
36
+ b = np.log(min_AP / max_AP) / exponential
37
+
38
+ '''
39
+ a * e^(b * ER_min) = max_AP
40
+ '''
41
+
42
+ a = max_AP / math.pow(np.e, b * ER_min)
43
+
44
+ return a, b
45
+
46
+
47
  def AP_fomula(a = 0.5442, b = -16.94145, error_rate=0.1):
48
+
49
+ a, b = find_a_b_for_AP()
50
+
51
+ # print(a, b)
52
+
53
  augmentation_probability = a * math.pow(np.e, b * error_rate)
54
 
55
  return augmentation_probability
56
+
57
+
58
+
59
+ '''
60
+ Hàm sigmoid:
61
+
62
+ Sigmoid(x) = 1 / (1 + e^(-k * x))
63
+
64
+ Giới hạn tỉ lệ lỗi tối đa của một câu là <= 35%
65
+
66
+ x: là SER_max - SER_mong muốn
67
+
68
+ => Sigmoid - lim(x) -> 0.35 ~ 1
69
+
70
+ Sigmoid(x) = 1 / (1 + e^(-k * x)) ~ 1
71
+
72
+ => 1 / (1 + e^(-0.35k)) = 999/1000
73
+
74
+
75
+ Đk kich hoạt hàm sigmoid => Khi SER đạt đến một độ min nhật định vd: 10%
76
+ '''
77
+
78
+ def get_k_sigmoid(x=0.2, lim1 = 0.999):
79
+ '''
80
+
81
+ :param x: ##SER: Tỷ lệ lỗi đã thêm (mong muốn: < 15% - tối đa <= 30%)
82
+ :param lim1: Giá trị ~1
83
+ :return: k
84
+ '''
85
+ sum1 = 1 - lim1
86
+
87
+ sum1 = sum1 / lim1
88
+
89
+ k = -np.log(sum1) / x
90
+ return k
91
+
92
+
93
+ print(1 - (1 / (1 + pow(math.e, 0.0 * get_k_sigmoid(x=0.1)))))
utils.py CHANGED
@@ -6,12 +6,17 @@ import math
6
 
7
  import fomula
8
 
9
-
10
- def character_replacement(text, error_rate=0.03):
 
 
 
 
11
  '''
12
  :param text: Gồm 1 câu đúng chính tả
13
  :param error_rate: tỷ lệ lỗi sai muốn thêm
14
  :param C: hằng số để tính toán xác suất thay thế ký tự
 
15
  :return: Gồm 1 câu sai chính tả
16
 
17
  Kết hợp phương pháp error_rate và augmentation_probability để tạo lỗi chính tả.
@@ -130,8 +135,15 @@ def character_replacement(text, error_rate=0.03):
130
  # Chọn ngẫu nhiên các vị trí để thay thế ký tự
131
  indices = random.sample(range(len(text)), num_errors)
132
 
 
 
 
 
 
 
133
  for index in indices:
134
- if text[index].lower() in keyboard and random.random() <= augmentation_probability:
 
135
  # Lấy ngẫu nhiên một ký tự từ các ký tự gần trên bàn phím
136
  replacement_char = random.choice(keyboard[text[index].lower()])
137
  # Giữ nguyên kiểu chữ hoa hoặc chữ thường
@@ -142,10 +154,12 @@ def character_replacement(text, error_rate=0.03):
142
  # Ghép các ký tự lại thành chuỗi văn bản hoàn chỉnh
143
  final_text = ''.join(text)
144
 
145
- return final_text
 
 
146
 
147
 
148
- def character_insertion(text, error_rate=0.03):
149
  '''
150
  :param text: Gồm 1 câu đúng chính tả
151
  :param error_rate: tỷ lệ lỗi sai muốn thêm
@@ -184,10 +198,17 @@ def character_insertion(text, error_rate=0.03):
184
  # Tính toán xác suất chèn ký tự dựa trên error_rate sử dụng hàm logarit
185
  augmentation_probability = fomula.AP_fomula(error_rate=error_rate)
186
 
 
187
  # Thực hiện thêm ký tự tại các vị trí ngẫu nhiên
 
 
 
 
 
188
  for _ in range(num_errors):
189
  # Chọn ngẫu nhiên một vị trí trong văn bản để thêm ký tự
190
- if random.random() <= augmentation_probability:
 
191
  index = random.randint(0, len(text))
192
  # Chọn ngẫu nhiên một ký tự từ bàn phím
193
  char_to_insert = random.choice(keyboard)
@@ -195,10 +216,11 @@ def character_insertion(text, error_rate=0.03):
195
  text.insert(index, char_to_insert)
196
 
197
  # Ghép các ký tự lại thành chuỗi văn bản
198
- return ''.join(text)
 
199
 
200
 
201
- def character_deletion(text, error_rate=0.03):
202
  '''
203
  :param text: Gồm 1 câu đúng chính tả
204
  :param error_rate: tỷ lệ lỗi sai muốn thêm
@@ -226,19 +248,26 @@ def character_deletion(text, error_rate=0.03):
226
  # Tính toán xác suất xóa ký tự dựa trên error_rate sử dụng hàm logarit
227
  augmentation_probability = fomula.AP_fomula(error_rate=error_rate)
228
 
 
 
 
 
 
229
  # Thực hiện xóa ký tự tại các vị trí ngẫu nhiên
230
  for _ in range(num_errors):
231
  # Chọn ngẫu nhiên một vị trí trong văn bản để xóa ký tự
232
- if random.random() <= augmentation_probability:
233
  if len(text) > 0: # Đảm bảo rằng danh sách không trống
 
234
  index = random.randint(0, len(text) - 1)
235
  # Xóa ký tự tại vị trí đã chọn
236
  text.pop(index)
237
 
238
  # Ghép các ký tự lại thành chuỗi văn bản
239
- return ''.join(text)
 
240
 
241
- def character_transposition(text, error_rate=0.03):
242
  '''
243
  :param text: Gồm 1 câu đúng chính tả
244
  :param error_rate: tỷ lệ lỗi sai muốn thêm
@@ -264,20 +293,26 @@ def character_transposition(text, error_rate=0.03):
264
 
265
  # Tính toán xác suất hoán đổi ký tự dựa trên error_rate sử dụng hàm logarit
266
  augmentation_probability = fomula.AP_fomula(error_rate=error_rate)
 
 
 
 
267
 
268
  # Thực hiện hoán đổi ký tự tại các vị trí ngẫu nhiên
269
  for _ in range(num_errors):
270
  # Chọn ngẫu nhiên một vị trí trong văn bản để hoán đổi ký tự
271
- if random.random() <= augmentation_probability:
272
  if len(text) > 1: # Đảm bảo rằng danh sách có ít nhất 2 ký tự để hoán đổi
 
273
  index = random.randint(0, len(text) - 2)
274
  # Hoán đổi hai ký tự liên tiếp tại vị trí đã chọn
275
  text[index], text[index + 1] = text[index + 1], text[index]
276
 
277
  # Ghép các ký tự lại thành chuỗi văn bản
278
- return ''.join(text)
 
279
 
280
- def homophone_replacement(text, error_rate=0.03):
281
  '''
282
  :param text: Gồm 1 câu đúng chính tả
283
  :param error_rate: tỷ lệ lỗi sai muốn thêm
@@ -323,28 +358,33 @@ def homophone_replacement(text, error_rate=0.03):
323
 
324
  # Tính toán xác suất thay thế từ dựa trên error_rate sử dụng hàm logarit
325
  augmentation_probability = fomula.AP_fomula(error_rate=error_rate)
326
-
327
  # Thực hiện thay thế từ tại các vị trí ngẫu nhiên
328
  num_errors = int(len(text) * error_rate)
329
  if num_errors < 1:
330
  num_errors = 1 if random.random() > 0.5 else 0
331
 
 
 
 
332
  for _ in range(num_errors):
333
- if len(candidate_indices) > 0 and random.random() <= augmentation_probability:
334
  index = random.choice(candidate_indices)
335
  word = words[index]
336
  for key in homophones.keys():
337
  if key in word:
338
  word = word.replace(key, homophones[key])
 
339
  break # Dừng lại sau khi thay thế lần đầu tiên để tránh thay thế nhiều lần
340
  words[index] = word
341
  candidate_indices.remove(index) # Đảm bảo từ này không bị thay thế nhiều lần
342
 
343
  # Ghép các từ lại thành chuỗi văn bản
344
- return ' '.join(words)
 
345
 
346
 
347
- def common_misspelling_replacement(text, error_rate=0.12):
348
  '''
349
  :param text: Gồm 1 câu đúng chính tả
350
  :param error_rate: tỷ lệ lỗi sai muốn thêm
@@ -377,30 +417,35 @@ def common_misspelling_replacement(text, error_rate=0.12):
377
 
378
  #Kiểm tra xem có thể thay không
379
  if len(candidate_indices) == 0:
380
- return ' '.join(words)
381
 
382
  # Tính toán xác suất thay thế từ dựa trên error_rate sử dụng hàm logarit
383
  augmentation_probability = fomula.AP_fomula(error_rate=error_rate)
384
-
385
  # Thực hiện thay thế từ tại các vị trí ngẫu nhiên
386
  num_errors = int(len(text) * error_rate)
387
  if num_errors < 1:
388
  num_errors = 1 if random.random() > 0.5 else 0
389
 
 
 
 
390
  for _ in range(num_errors):
391
- if len(candidate_indices) > 0 and random.random() <= augmentation_probability:
392
  index = random.choice(candidate_indices)
393
  word = words[index]
394
  # Chọn ngẫu nhiên một phiên bản sai chính tả từ từ điển
395
  misspelled_word = random.choice(common_misspellings[word])
396
  words[index] = misspelled_word
 
397
  candidate_indices.remove(index) # Đảm bảo từ này không bị thay thế nhiều lần
398
 
399
  # Ghép các từ lại thành chuỗi văn bản
400
- return ' '.join(words)
 
401
 
402
 
403
- def similar_character_replacement(text, error_rate=0.03):
404
  '''
405
  :param text: Gồm 1 câu đúng chính tả
406
  :param error_rate: tỷ lệ lỗi sai muốn thêm
@@ -438,14 +483,18 @@ def similar_character_replacement(text, error_rate=0.03):
438
 
439
  # Tính toán xác suất thay thế ký tự dựa trên error_rate sử d���ng hàm logarit
440
  augmentation_probability = fomula.AP_fomula(error_rate=error_rate)
441
-
442
  # Thực hiện thay thế ký tự tại các vị trí ngẫu nhiên
443
  num_errors = int(len(text) * error_rate)
444
  if num_errors < 1:
445
  num_errors = 1 if random.random() > 0.5 else 0
446
 
 
 
 
447
  for _ in range(num_errors):
448
- if len(candidate_indices) > 0 and random.random() <= augmentation_probability:
 
449
  index = random.choice(candidate_indices)
450
  char = characters[index]
451
  # Chọn ngẫu nhiên một ký tự tương tự từ từ điển
@@ -454,10 +503,11 @@ def similar_character_replacement(text, error_rate=0.03):
454
  candidate_indices.remove(index) # Đảm bảo ký tự này không bị thay thế nhiều lần
455
 
456
  # Ghép các ký tự lại thành chuỗi văn bản
457
- return ''.join(characters)
 
458
 
459
 
460
- def random_space_insertion(text, error_rate=0.025):
461
  '''
462
  :param text: Gồm 1 câu đúng chính tả
463
  :param error_rate: tỷ lệ lỗi sai muốn thêm
@@ -475,22 +525,27 @@ def random_space_insertion(text, error_rate=0.025):
475
 
476
  # Tính toán xác suất chèn khoảng trắng dựa trên error_rate sử dụng hàm logarit
477
  augmentation_probability = fomula.AP_fomula(error_rate=error_rate)
478
-
479
  # Thực hiện chèn khoảng trắng tại các vị trí ngẫu nhiên
480
  num_errors = int(len(text) * error_rate)
481
  if num_errors < 1:
482
  num_errors = 1 if random.random() > 0.5 else 0
483
 
 
 
 
484
  for _ in range(num_errors):
485
- if len(candidate_indices) > 0 and random.random() <= augmentation_probability:
 
486
  index = random.choice(candidate_indices)
487
  characters.insert(index, ' ')
488
  candidate_indices.remove(index) # Đảm bảo không chèn khoảng trắng nhiều lần tại cùng một vị trí
489
 
490
  # Ghép các ký tự lại thành chuỗi văn bản
491
- return ''.join(characters)
 
492
 
493
- def random_space_removal(text, error_rate=0.01):
494
  '''
495
  :param text: Gồm 1 câu đúng chính tả
496
  :param error_rate: tỷ lệ lỗi sai muốn thêm
@@ -502,7 +557,7 @@ def random_space_removal(text, error_rate=0.01):
502
 
503
  # Tính toán xác suất bỏ dấu cách dựa trên error_rate sử dụng hàm logarit
504
  augmentation_probability = fomula.AP_fomula(error_rate=error_rate)
505
-
506
  # Tìm các vị trí có thể bỏ dấu cách (giữa các từ)
507
  words = text.split()
508
  candidate_indices = [i for i in range(len(words) - 1)]
@@ -512,15 +567,60 @@ def random_space_removal(text, error_rate=0.01):
512
  if num_errors < 1:
513
  num_errors = 1 if random.random() > 0.5 else 0
514
 
 
 
 
515
  # Thực hiện bỏ dấu cách tại các vị trí ngẫu nhiên
516
  selected_indices = random.sample(candidate_indices, min(num_errors, len(candidate_indices)))
517
  for index in selected_indices:
518
- if random.random() <= augmentation_probability:
 
519
  words[index] = words[index] + words[index + 1]
520
  words[index + 1] = '' # Xóa từ đã ghép để tránh lặp lại
521
 
522
  # Ghép các từ lại thành chuỗi văn bản, bỏ qua các từ trống
523
- return ' '.join([word for word in words if word])
 
524
 
525
 
526
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  import fomula
8
 
9
+ ##SER: Tỷ lệ lỗi đã thêm (mong muốn: < 15% - tối đa <= 30%)
10
+ SER_want = 0.15
11
+ SER_max = 0.3
12
+ k_sigmoid = fomula.get_k_sigmoid(x=SER_max - SER_want)
13
+ print("k_sigmoid", k_sigmoid)
14
+ def character_replacement(text, SER, error_rate=0.03):
15
  '''
16
  :param text: Gồm 1 câu đúng chính tả
17
  :param error_rate: tỷ lệ lỗi sai muốn thêm
18
  :param C: hằng số để tính toán xác suất thay thế ký tự
19
+ :SER: Tỷ lệ lỗi đã sửa / tổng lỗi
20
  :return: Gồm 1 câu sai chính tả
21
 
22
  Kết hợp phương pháp error_rate và augmentation_probability để tạo lỗi chính tả.
 
135
  # Chọn ngẫu nhiên các vị trí để thay thế ký tự
136
  indices = random.sample(range(len(text)), num_errors)
137
 
138
+ num_of_change = 0
139
+
140
+ sigmoid = 1 if SER <= SER_want else (1 - (1 / (1 + pow(math.e, - (SER - SER_want) * k_sigmoid))))
141
+ print(sigmoid)
142
+
143
+
144
  for index in indices:
145
+ if text[index].lower() in keyboard and random.random() <= augmentation_probability * sigmoid:
146
+ num_of_change += 1
147
  # Lấy ngẫu nhiên một ký tự từ các ký tự gần trên bàn phím
148
  replacement_char = random.choice(keyboard[text[index].lower()])
149
  # Giữ nguyên kiểu chữ hoa hoặc chữ thường
 
154
  # Ghép các ký tự lại thành chuỗi văn bản hoàn chỉnh
155
  final_text = ''.join(text)
156
 
157
+ print("character_replacement: ", num_of_change)
158
+
159
+ return final_text, num_of_change
160
 
161
 
162
+ def character_insertion(text, SER, error_rate=0.03):
163
  '''
164
  :param text: Gồm 1 câu đúng chính tả
165
  :param error_rate: tỷ lệ lỗi sai muốn thêm
 
198
  # Tính toán xác suất chèn ký tự dựa trên error_rate sử dụng hàm logarit
199
  augmentation_probability = fomula.AP_fomula(error_rate=error_rate)
200
 
201
+ num_of_change = 0
202
  # Thực hiện thêm ký tự tại các vị trí ngẫu nhiên
203
+
204
+ sigmoid = 1 if SER <= SER_want else (1 - (1 / (1 + pow(math.e, -(SER - SER_want) * k_sigmoid))))
205
+ print(sigmoid)
206
+
207
+
208
  for _ in range(num_errors):
209
  # Chọn ngẫu nhiên một vị trí trong văn bản để thêm ký tự
210
+ if random.random() <= augmentation_probability * sigmoid:
211
+ num_of_change += 1
212
  index = random.randint(0, len(text))
213
  # Chọn ngẫu nhiên một ký tự từ bàn phím
214
  char_to_insert = random.choice(keyboard)
 
216
  text.insert(index, char_to_insert)
217
 
218
  # Ghép các ký tự lại thành chuỗi văn bản
219
+ print("character_insertion: ", num_of_change)
220
+ return ''.join(text), num_of_change
221
 
222
 
223
+ def character_deletion(text, SER, error_rate=0.03):
224
  '''
225
  :param text: Gồm 1 câu đúng chính tả
226
  :param error_rate: tỷ lệ lỗi sai muốn thêm
 
248
  # Tính toán xác suất xóa ký tự dựa trên error_rate sử dụng hàm logarit
249
  augmentation_probability = fomula.AP_fomula(error_rate=error_rate)
250
 
251
+ num_of_change = 0
252
+
253
+ sigmoid = 1 if SER <= SER_want else (1 - (1 / (1 + pow(math.e, -(SER - SER_want) * k_sigmoid))))
254
+ print(sigmoid)
255
+
256
  # Thực hiện xóa ký tự tại các vị trí ngẫu nhiên
257
  for _ in range(num_errors):
258
  # Chọn ngẫu nhiên một vị trí trong văn bản để xóa ký tự
259
+ if random.random() <= augmentation_probability * sigmoid:
260
  if len(text) > 0: # Đảm bảo rằng danh sách không trống
261
+ num_of_change += 1
262
  index = random.randint(0, len(text) - 1)
263
  # Xóa ký tự tại vị trí đã chọn
264
  text.pop(index)
265
 
266
  # Ghép các ký tự lại thành chuỗi văn bản
267
+ print("character_deletion: ", num_of_change)
268
+ return ''.join(text), num_of_change
269
 
270
+ def character_transposition(text, SER, error_rate=0.03):
271
  '''
272
  :param text: Gồm 1 câu đúng chính tả
273
  :param error_rate: tỷ lệ lỗi sai muốn thêm
 
293
 
294
  # Tính toán xác suất hoán đổi ký tự dựa trên error_rate sử dụng hàm logarit
295
  augmentation_probability = fomula.AP_fomula(error_rate=error_rate)
296
+ num_of_change = 0
297
+
298
+ sigmoid = 1 if SER <= SER_want else (1 - (1 / (1 + pow(math.e, -(SER - SER_want) * k_sigmoid))))
299
+ print(sigmoid)
300
 
301
  # Thực hiện hoán đổi ký tự tại các vị trí ngẫu nhiên
302
  for _ in range(num_errors):
303
  # Chọn ngẫu nhiên một vị trí trong văn bản để hoán đổi ký tự
304
+ if random.random() <= augmentation_probability * sigmoid:
305
  if len(text) > 1: # Đảm bảo rằng danh sách có ít nhất 2 ký tự để hoán đổi
306
+ num_of_change += 2
307
  index = random.randint(0, len(text) - 2)
308
  # Hoán đổi hai ký tự liên tiếp tại vị trí đã chọn
309
  text[index], text[index + 1] = text[index + 1], text[index]
310
 
311
  # Ghép các ký tự lại thành chuỗi văn bản
312
+ print("character_transposition: ", num_of_change)
313
+ return ''.join(text), num_of_change
314
 
315
+ def homophone_replacement(text, SER, error_rate=0.03):
316
  '''
317
  :param text: Gồm 1 câu đúng chính tả
318
  :param error_rate: tỷ lệ lỗi sai muốn thêm
 
358
 
359
  # Tính toán xác suất thay thế từ dựa trên error_rate sử dụng hàm logarit
360
  augmentation_probability = fomula.AP_fomula(error_rate=error_rate)
361
+ num_of_change = 0
362
  # Thực hiện thay thế từ tại các vị trí ngẫu nhiên
363
  num_errors = int(len(text) * error_rate)
364
  if num_errors < 1:
365
  num_errors = 1 if random.random() > 0.5 else 0
366
 
367
+ sigmoid = 1 if SER <= SER_want else (1 - (1 / (1 + pow(math.e, -(SER - SER_want) * k_sigmoid))))
368
+ print(sigmoid)
369
+
370
  for _ in range(num_errors):
371
+ if len(candidate_indices) > 0 and random.random() <= augmentation_probability * sigmoid:
372
  index = random.choice(candidate_indices)
373
  word = words[index]
374
  for key in homophones.keys():
375
  if key in word:
376
  word = word.replace(key, homophones[key])
377
+ num_of_change += np.abs(len(key) - len(homophones[key]))
378
  break # Dừng lại sau khi thay thế lần đầu tiên để tránh thay thế nhiều lần
379
  words[index] = word
380
  candidate_indices.remove(index) # Đảm bảo từ này không bị thay thế nhiều lần
381
 
382
  # Ghép các từ lại thành chuỗi văn bản
383
+ print("homophone_replacement: ", num_of_change)
384
+ return ' '.join(words), num_of_change
385
 
386
 
387
+ def common_misspelling_replacement(text, SER, error_rate=0.12):
388
  '''
389
  :param text: Gồm 1 câu đúng chính tả
390
  :param error_rate: tỷ lệ lỗi sai muốn thêm
 
417
 
418
  #Kiểm tra xem có thể thay không
419
  if len(candidate_indices) == 0:
420
+ return ' '.join(words), 0
421
 
422
  # Tính toán xác suất thay thế từ dựa trên error_rate sử dụng hàm logarit
423
  augmentation_probability = fomula.AP_fomula(error_rate=error_rate)
424
+ num_of_change = 0
425
  # Thực hiện thay thế từ tại các vị trí ngẫu nhiên
426
  num_errors = int(len(text) * error_rate)
427
  if num_errors < 1:
428
  num_errors = 1 if random.random() > 0.5 else 0
429
 
430
+ sigmoid = 1 if SER <= SER_want else (1 - (1 / (1 + pow(math.e, -(SER - SER_want) * k_sigmoid))))
431
+ print(sigmoid)
432
+
433
  for _ in range(num_errors):
434
+ if len(candidate_indices) > 0 and random.random() <= augmentation_probability * sigmoid:
435
  index = random.choice(candidate_indices)
436
  word = words[index]
437
  # Chọn ngẫu nhiên một phiên bản sai chính tả từ từ điển
438
  misspelled_word = random.choice(common_misspellings[word])
439
  words[index] = misspelled_word
440
+ num_of_change += np.abs(len(misspelled_word) - len(word))
441
  candidate_indices.remove(index) # Đảm bảo từ này không bị thay thế nhiều lần
442
 
443
  # Ghép các từ lại thành chuỗi văn bản
444
+ print("common_misspelling_replacement: ", num_of_change)
445
+ return ' '.join(words), num_of_change
446
 
447
 
448
+ def similar_character_replacement(text, SER, error_rate=0.03):
449
  '''
450
  :param text: Gồm 1 câu đúng chính tả
451
  :param error_rate: tỷ lệ lỗi sai muốn thêm
 
483
 
484
  # Tính toán xác suất thay thế ký tự dựa trên error_rate sử d���ng hàm logarit
485
  augmentation_probability = fomula.AP_fomula(error_rate=error_rate)
486
+ num_of_change = 0
487
  # Thực hiện thay thế ký tự tại các vị trí ngẫu nhiên
488
  num_errors = int(len(text) * error_rate)
489
  if num_errors < 1:
490
  num_errors = 1 if random.random() > 0.5 else 0
491
 
492
+ sigmoid = 1 if SER <= SER_want else (1 - (1 / (1 + pow(math.e, -(SER - SER_want) * k_sigmoid))))
493
+ print(sigmoid)
494
+
495
  for _ in range(num_errors):
496
+ if len(candidate_indices) > 0 and random.random() <= augmentation_probability * sigmoid:
497
+ num_of_change += 1
498
  index = random.choice(candidate_indices)
499
  char = characters[index]
500
  # Chọn ngẫu nhiên một ký tự tương tự từ từ điển
 
503
  candidate_indices.remove(index) # Đảm bảo ký tự này không bị thay thế nhiều lần
504
 
505
  # Ghép các ký tự lại thành chuỗi văn bản
506
+ print("similar_character_replacement: ", num_of_change)
507
+ return ''.join(characters), num_of_change
508
 
509
 
510
+ def random_space_insertion(text, SER, error_rate=0.025):
511
  '''
512
  :param text: Gồm 1 câu đúng chính tả
513
  :param error_rate: tỷ lệ lỗi sai muốn thêm
 
525
 
526
  # Tính toán xác suất chèn khoảng trắng dựa trên error_rate sử dụng hàm logarit
527
  augmentation_probability = fomula.AP_fomula(error_rate=error_rate)
528
+ num_of_change = 0
529
  # Thực hiện chèn khoảng trắng tại các vị trí ngẫu nhiên
530
  num_errors = int(len(text) * error_rate)
531
  if num_errors < 1:
532
  num_errors = 1 if random.random() > 0.5 else 0
533
 
534
+ sigmoid = 1 if SER <= SER_want else (1 - (1 / (1 + pow(math.e, -(SER - SER_want) * k_sigmoid))))
535
+ print(sigmoid)
536
+
537
  for _ in range(num_errors):
538
+ if len(candidate_indices) > 0 and random.random() <= augmentation_probability * sigmoid:
539
+ num_of_change += 1
540
  index = random.choice(candidate_indices)
541
  characters.insert(index, ' ')
542
  candidate_indices.remove(index) # Đảm bảo không chèn khoảng trắng nhiều lần tại cùng một vị trí
543
 
544
  # Ghép các ký tự lại thành chuỗi văn bản
545
+ print("random_space_insertion: ", num_of_change)
546
+ return ''.join(characters), num_of_change
547
 
548
+ def random_space_removal(text, SER, error_rate=0.01):
549
  '''
550
  :param text: Gồm 1 câu đúng chính tả
551
  :param error_rate: tỷ lệ lỗi sai muốn thêm
 
557
 
558
  # Tính toán xác suất bỏ dấu cách dựa trên error_rate sử dụng hàm logarit
559
  augmentation_probability = fomula.AP_fomula(error_rate=error_rate)
560
+ num_of_change = 0
561
  # Tìm các vị trí có thể bỏ dấu cách (giữa các từ)
562
  words = text.split()
563
  candidate_indices = [i for i in range(len(words) - 1)]
 
567
  if num_errors < 1:
568
  num_errors = 1 if random.random() > 0.5 else 0
569
 
570
+ sigmoid = 1 if SER <= SER_want else (1 - (1 / (1 + pow(math.e, -(SER - SER_want) * k_sigmoid))))
571
+ print(sigmoid)
572
+
573
  # Thực hiện bỏ dấu cách tại các vị trí ngẫu nhiên
574
  selected_indices = random.sample(candidate_indices, min(num_errors, len(candidate_indices)))
575
  for index in selected_indices:
576
+ if random.random() <= augmentation_probability * sigmoid:
577
+ num_of_change += 1
578
  words[index] = words[index] + words[index + 1]
579
  words[index + 1] = '' # Xóa từ đã ghép để tránh lặp lại
580
 
581
  # Ghép các từ lại thành chuỗi văn bản, bỏ qua các từ trống
582
+ print("random_space_removal: ", num_of_change)
583
+ return ' '.join([word for word in words if word]), num_of_change
584
 
585
 
586
 
587
+ import unidecode
588
+
589
+ def remove_vietnamese_accents(text, SER, error_rate=0.05):
590
+ '''
591
+ :param text: Gồm 1 câu có dấu tiếng Việt
592
+ :param error_rate: tỷ lệ lỗi sai muốn thêm
593
+ :param C: hằng số để tính toán xác suất chuyển đổi ký tự
594
+ :param num_errors: số lượng lỗi sai muốn thêm (tuỳ chọn)
595
+ :return: Gồm 1 câu không dấu tiếng Việt với một tỷ lệ lỗi nhất định
596
+
597
+ Thực hiện chuyển đổi các ký tự có dấu thành các ký tự không dấu tương ứng với xác suất nhất định.
598
+ '''
599
+
600
+ # Tính toán xác suất chuy��n đổi ký tự dựa trên error_rate sử dụng hàm logarit
601
+ augmentation_probability = fomula.AP_fomula(error_rate=error_rate)
602
+
603
+ # Chuyển đổi văn bản thành danh sách ký tự để có thể chuyển đổi ký tự
604
+ characters = list(text)
605
+
606
+ num_of_change = 0
607
+ num_errors = int(len(text) * error_rate)
608
+ if num_errors < 1:
609
+ num_errors = 1 if random.random() > 0.5 else 0
610
+
611
+ # Xác định các ký tự có thể chuyển đổi
612
+ candidate_indices = [i for i, char in enumerate(characters) if unidecode.unidecode(char) != char]
613
+
614
+ sigmoid = 1 if SER <= SER_want else (1 - (1 / (1 + pow(math.e, -(SER - SER_want) * k_sigmoid))))
615
+ print(sigmoid)
616
+
617
+ # Thực hiện chuyển đổi ký tự tại các vị trí ngẫu nhiên
618
+ selected_indices = random.sample(candidate_indices, min(num_errors, len(candidate_indices)))
619
+ for index in selected_indices:
620
+ if random.random() <= augmentation_probability * sigmoid:
621
+ num_of_change += 1
622
+ characters[index] = unidecode.unidecode(characters[index])
623
+
624
+ print("remove_vietnamese_accents: ", num_of_change)
625
+ # Ghép các ký tự lại thành chuỗi văn bản
626
+ return ''.join(characters), num_of_change