ShynBui commited on
Commit
4ff8b83
·
1 Parent(s): b213fb9

update AP fomula

Browse files
__pycache__/fomula.cpython-310.pyc ADDED
Binary file (450 Bytes). View file
 
__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,4 +1,6 @@
1
  import gradio as gr
 
 
2
  from utils import *
3
 
4
  def final_result(input_text):
@@ -55,4 +57,5 @@ def final_result(input_text):
55
  demo = gr.Interface(fn=final_result, inputs="textbox", outputs="textbox")
56
 
57
  if __name__ == "__main__":
 
58
  demo.launch()
 
1
  import gradio as gr
2
+
3
+ import fomula
4
  from utils import *
5
 
6
  def final_result(input_text):
 
57
  demo = gr.Interface(fn=final_result, inputs="textbox", outputs="textbox")
58
 
59
  if __name__ == "__main__":
60
+ print(fomula.AP_fomula(error_rate=0.12))
61
  demo.launch()
fomula.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+
4
+ 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
utils.py CHANGED
@@ -4,14 +4,10 @@ import numpy as np
4
  import random
5
  import math
6
 
7
- '''
8
- AP = C / log(e + 1)
9
- e: Tỉ lệ lỗi trong 1 văn bản: 0.5% - 10%
10
- AP: Tỉ lệ sửa (0.5 < AP <= 1)
11
-
12
- '''
13
- Ceta = 0.02069634258
14
- def character_replacement(text, error_rate=0.03, C=Ceta):
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
@@ -40,7 +36,7 @@ def character_replacement(text, error_rate=0.03, C=Ceta):
40
 
41
 
42
  # Tính toán xác suất thay thế ký tự dựa trên error_rate sử dụng hàm logarit
43
- augmentation_probability = C / math.log(error_rate + 1, 10)
44
 
45
  # Bàn phím QWERTY tiếng Việt
46
  keyboard = {
@@ -149,7 +145,7 @@ def character_replacement(text, error_rate=0.03, C=Ceta):
149
  return final_text
150
 
151
 
152
- def character_insertion(text, error_rate=0.03, C=Ceta):
153
  '''
154
  :param text: Gồm 1 câu đúng chính tả
155
  :param error_rate: tỷ lệ lỗi sai muốn thêm
@@ -186,7 +182,7 @@ def character_insertion(text, error_rate=0.03, C=Ceta):
186
  num_errors = 1 if random.random() > 0.5 else 0
187
 
188
  # Tính toán xác suất chèn ký tự dựa trên error_rate sử dụng hàm logarit
189
- augmentation_probability = C / math.log(error_rate + 1, 10)
190
 
191
  # Thực hiện thêm ký tự tại các vị trí ngẫu nhiên
192
  for _ in range(num_errors):
@@ -202,7 +198,7 @@ def character_insertion(text, error_rate=0.03, C=Ceta):
202
  return ''.join(text)
203
 
204
 
205
- def character_deletion(text, error_rate=0.03, C=Ceta):
206
  '''
207
  :param text: Gồm 1 câu đúng chính tả
208
  :param error_rate: tỷ lệ lỗi sai muốn thêm
@@ -228,7 +224,7 @@ def character_deletion(text, error_rate=0.03, C=Ceta):
228
 
229
 
230
  # Tính toán xác suất xóa ký tự dựa trên error_rate sử dụng hàm logarit
231
- augmentation_probability = C / math.log(error_rate + 1, 10)
232
 
233
  # Thực hiện xóa ký tự tại các vị trí ngẫu nhiên
234
  for _ in range(num_errors):
@@ -242,7 +238,7 @@ def character_deletion(text, error_rate=0.03, C=Ceta):
242
  # Ghép các ký tự lại thành chuỗi văn bản
243
  return ''.join(text)
244
 
245
- def character_transposition(text, error_rate=0.03, C=Ceta):
246
  '''
247
  :param text: Gồm 1 câu đúng chính tả
248
  :param error_rate: tỷ lệ lỗi sai muốn thêm
@@ -267,7 +263,7 @@ def character_transposition(text, error_rate=0.03, C=Ceta):
267
  num_errors = 1 if random.random() > 0.5 else 0
268
 
269
  # 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
270
- augmentation_probability = C / math.log(error_rate + 1, 10)
271
 
272
  # Thực hiện hoán đổi ký tự tại các vị trí ngẫu nhiên
273
  for _ in range(num_errors):
@@ -281,7 +277,7 @@ def character_transposition(text, error_rate=0.03, C=Ceta):
281
  # Ghép các ký tự lại thành chuỗi văn bản
282
  return ''.join(text)
283
 
284
- def homophone_replacement(text, error_rate=0.03, C=Ceta):
285
  '''
286
  :param text: Gồm 1 câu đúng chính tả
287
  :param error_rate: tỷ lệ lỗi sai muốn thêm
@@ -326,7 +322,7 @@ def homophone_replacement(text, error_rate=0.03, C=Ceta):
326
  break
327
 
328
  # Tính toán xác suất thay thế từ dựa trên error_rate sử dụng hàm logarit
329
- augmentation_probability = C / math.log(error_rate + 1, 10)
330
 
331
  # Thực hiện thay thế từ tại các vị trí ngẫu nhiên
332
  num_errors = int(len(text) * error_rate)
@@ -348,7 +344,7 @@ def homophone_replacement(text, error_rate=0.03, C=Ceta):
348
  return ' '.join(words)
349
 
350
 
351
- def common_misspelling_replacement(text, error_rate=0.12, C=Ceta):
352
  '''
353
  :param text: Gồm 1 câu đúng chính tả
354
  :param error_rate: tỷ lệ lỗi sai muốn thêm
@@ -384,7 +380,7 @@ def common_misspelling_replacement(text, error_rate=0.12, C=Ceta):
384
  return ' '.join(words)
385
 
386
  # Tính toán xác suất thay thế từ dựa trên error_rate sử dụng hàm logarit
387
- augmentation_probability = C / math.log(error_rate + 1)
388
 
389
  # Thực hiện thay thế từ tại các vị trí ngẫu nhiên
390
  num_errors = int(len(text) * error_rate)
@@ -404,7 +400,7 @@ def common_misspelling_replacement(text, error_rate=0.12, C=Ceta):
404
  return ' '.join(words)
405
 
406
 
407
- def similar_character_replacement(text, error_rate=0.03, C=Ceta):
408
  '''
409
  :param text: Gồm 1 câu đúng chính tả
410
  :param error_rate: tỷ lệ lỗi sai muốn thêm
@@ -441,7 +437,7 @@ def similar_character_replacement(text, error_rate=0.03, C=Ceta):
441
  candidate_indices = [i for i, char in enumerate(characters) if char in similar_characters]
442
 
443
  # Tính toán xác suất thay thế ký tự dựa trên error_rate sử dụng hàm logarit
444
- augmentation_probability = C / math.log(error_rate + 1, 10)
445
 
446
  # Thực hiện thay thế ký tự tại các vị trí ngẫu nhiên
447
  num_errors = int(len(text) * error_rate)
@@ -461,7 +457,7 @@ def similar_character_replacement(text, error_rate=0.03, C=Ceta):
461
  return ''.join(characters)
462
 
463
 
464
- def random_space_insertion(text, error_rate=0.025, C=Ceta):
465
  '''
466
  :param text: Gồm 1 câu đúng chính tả
467
  :param error_rate: tỷ lệ lỗi sai muốn thêm
@@ -478,7 +474,7 @@ def random_space_insertion(text, error_rate=0.025, C=Ceta):
478
  candidate_indices = [i for i in range(1, len(characters))]
479
 
480
  # 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
481
- augmentation_probability = C / math.log(error_rate + 1, 10)
482
 
483
  # Thực hiện chèn khoảng trắng tại các vị trí ngẫu nhiên
484
  num_errors = int(len(text) * error_rate)
@@ -494,7 +490,7 @@ def random_space_insertion(text, error_rate=0.025, C=Ceta):
494
  # Ghép các ký tự lại thành chuỗi văn bản
495
  return ''.join(characters)
496
 
497
- def random_space_removal(text, error_rate=0.01, C=Ceta):
498
  '''
499
  :param text: Gồm 1 câu đúng chính tả
500
  :param error_rate: tỷ lệ lỗi sai muốn thêm
@@ -505,7 +501,7 @@ def random_space_removal(text, error_rate=0.01, C=Ceta):
505
  '''
506
 
507
  # 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
508
- augmentation_probability = C / math.log(error_rate + 1, 10)
509
 
510
  # Tìm các vị trí có thể bỏ dấu cách (giữa các từ)
511
  words = text.split()
 
4
  import random
5
  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
 
36
 
37
 
38
  # Tính toán xác suất thay thế ký tự dựa trên error_rate sử dụng hàm logarit
39
+ augmentation_probability = fomula.AP_fomula(error_rate=error_rate)
40
 
41
  # Bàn phím QWERTY tiếng Việt
42
  keyboard = {
 
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
 
182
  num_errors = 1 if random.random() > 0.5 else 0
183
 
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):
 
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
 
224
 
225
 
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):
 
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
 
263
  num_errors = 1 if random.random() > 0.5 else 0
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):
 
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
 
322
  break
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)
 
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
 
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)
 
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
 
437
  candidate_indices = [i for i, char in enumerate(characters) if char in similar_characters]
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)
 
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
 
474
  candidate_indices = [i for i in range(1, len(characters))]
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)
 
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
 
501
  '''
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()