ShynBui commited on
Commit
7b19804
·
1 Parent(s): e3e69c0

add similar_character_replacement

Browse files
Files changed (3) hide show
  1. __pycache__/utils.cpython-310.pyc +0 -0
  2. app.py +3 -0
  3. utils.py +56 -0
__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
@@ -23,6 +23,9 @@ def final_result(input_text):
23
  ## common_misspelling_replacement
24
  input_text_process = common_misspelling_replacement(input_text)
25
  list_text_response.append(input_text_process)
 
 
 
26
  ##
27
  string_text_response = '\n'.join(list_text_response)
28
  return str(string_text_response)
 
23
  ## common_misspelling_replacement
24
  input_text_process = common_misspelling_replacement(input_text)
25
  list_text_response.append(input_text_process)
26
+ ##similar_character_replacement
27
+ input_text_process = similar_character_replacement(input_text)
28
+ list_text_response.append(input_text_process)
29
  ##
30
  string_text_response = '\n'.join(list_text_response)
31
  return str(string_text_response)
utils.py CHANGED
@@ -395,3 +395,59 @@ def common_misspelling_replacement(text, error_rate=0.12, C=0.01):
395
  # Ghép các từ lại thành chuỗi văn bản
396
  return ' '.join(words)
397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
395
  # Ghép các từ lại thành chuỗi văn bản
396
  return ' '.join(words)
397
 
398
+
399
+ def similar_character_replacement(text, error_rate=0.03, C=0.01):
400
+ '''
401
+ :param text: Gồm 1 câu đúng chính tả
402
+ :param error_rate: tỷ lệ lỗi sai muốn thêm
403
+ :param C: hằng số để tính toán xác suất thay thế ký tự tương tự
404
+ :return: Gồm 1 câu sai chính tả
405
+
406
+ Thực hiện thay thế ký tự bằng ký tự tương tự trong bộ mã Unicode.
407
+ - Tìm các ký tự có thể thay thế trước
408
+ - Sử dụng augmentation_probability để xác định liệu ký tự đó có bị thay thế hay không
409
+ '''
410
+
411
+ # Từ điển các ký tự và các ký tự tương tự của chúng trong bộ mã Unicode
412
+ similar_characters = {
413
+ 'a': ['á', 'à', 'ả', 'ã', 'ạ', 'â', 'ă'],
414
+ 'e': ['é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê'],
415
+ 'i': ['í', 'ì', 'ỉ', 'ĩ', 'ị'],
416
+ 'o': ['ó', 'ò', 'ỏ', 'õ', 'ọ', 'ô', 'ơ'],
417
+ 'u': ['ú', 'ù', 'ủ', 'ũ', 'ụ', 'ư'],
418
+ 'y': ['ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ'],
419
+ 'd': ['đ'],
420
+ 'A': ['Á', 'À', 'Ả', 'Ã', 'Ạ', 'Â', 'Ă'],
421
+ 'E': ['É', 'È', 'Ẻ', 'Ẽ', 'Ẹ', 'Ê'],
422
+ 'I': ['Í', 'Ì', 'Ỉ', 'Ĩ', 'Ị'],
423
+ 'O': ['Ó', 'Ò', 'Ỏ', 'Õ', 'Ọ', 'Ô', 'Ơ'],
424
+ 'U': ['Ú', 'Ù', 'Ủ', 'Ũ', 'Ụ', 'Ư'],
425
+ 'Y': ['Ý', 'Ỳ', 'Ỷ', 'Ỹ', 'Ỵ'],
426
+ 'D': ['Đ']
427
+ }
428
+
429
+ # Chuyển đổi văn bản thành danh sách ký tự để có thể thay thế ký tự
430
+ characters = list(text)
431
+
432
+ # Xác định các ký tự có thể thay thế
433
+ candidate_indices = [i for i, char in enumerate(characters) if char in similar_characters]
434
+
435
+ # Tính toán xác suất thay thế ký tự dựa trên error_rate sử dụng hàm logarit
436
+ augmentation_probability = C / math.log(error_rate + 1, 10)
437
+
438
+ # Thực hiện thay thế ký tự tại các vị trí ngẫu nhiên
439
+ num_errors = int(len(text) * error_rate)
440
+ if num_errors < 1:
441
+ num_errors = 1 if random.random() > 0.5 else 0
442
+
443
+ for _ in range(num_errors):
444
+ if len(candidate_indices) > 0 and random.random() <= augmentation_probability:
445
+ index = random.choice(candidate_indices)
446
+ char = characters[index]
447
+ # Chọn ngẫu nhiên một ký tự tương tự từ từ điển
448
+ similar_char = random.choice(similar_characters[char])
449
+ characters[index] = similar_char
450
+ candidate_indices.remove(index) # Đảm bảo ký tự này không bị thay thế nhiều lần
451
+
452
+ # Ghép các ký tự lại thành chuỗi văn bản
453
+ return ''.join(characters)