add space remove
Browse files- __pycache__/utils.cpython-310.pyc +0 -0
- app.py +40 -18
- requirements.txt +0 -0
- utils.py +83 -7
__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
|
@@ -2,32 +2,54 @@ import gradio as gr
|
|
| 2 |
from utils import *
|
| 3 |
|
| 4 |
def final_result(input_text):
|
| 5 |
-
list_text_response = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
##character_replacement
|
| 8 |
-
input_text_process = character_replacement(input_text, error_rate=0.
|
| 9 |
-
list_text_response.append(input_text_process)
|
| 10 |
-
|
| 11 |
##character_insertion
|
| 12 |
-
input_text_process = character_insertion(
|
| 13 |
-
list_text_response.append(input_text_process)
|
| 14 |
##character_deletion
|
| 15 |
-
input_text_process = character_deletion(
|
| 16 |
-
list_text_response.append(input_text_process)
|
| 17 |
##character_transposition
|
| 18 |
-
input_text_process = character_transposition(
|
| 19 |
-
list_text_response.append(input_text_process)
|
| 20 |
##homophone_replacement
|
| 21 |
-
input_text_process = homophone_replacement(
|
| 22 |
-
list_text_response.append(input_text_process)
|
| 23 |
## common_misspelling_replacement
|
| 24 |
-
input_text_process = common_misspelling_replacement(
|
| 25 |
-
list_text_response.append(input_text_process)
|
| 26 |
##similar_character_replacement
|
| 27 |
-
input_text_process = similar_character_replacement(
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
return str(string_text_response)
|
| 32 |
|
| 33 |
demo = gr.Interface(fn=final_result, inputs="textbox", outputs="textbox")
|
|
|
|
| 2 |
from utils import *
|
| 3 |
|
| 4 |
def final_result(input_text):
|
| 5 |
+
# list_text_response = []
|
| 6 |
+
#
|
| 7 |
+
# ##character_replacement
|
| 8 |
+
# input_text_process = character_replacement(input_text, error_rate=0.1)
|
| 9 |
+
# list_text_response.append(input_text_process)
|
| 10 |
+
# ##character_insertion
|
| 11 |
+
# input_text_process = character_insertion(input_text, error_rate=0.01)
|
| 12 |
+
# list_text_response.append(input_text_process)
|
| 13 |
+
# ##character_deletion
|
| 14 |
+
# input_text_process = character_deletion(input_text, error_rate=0.01)
|
| 15 |
+
# list_text_response.append(input_text_process)
|
| 16 |
+
# ##character_transposition
|
| 17 |
+
# input_text_process = character_transposition(input_text, error_rate=0.03)
|
| 18 |
+
# list_text_response.append(input_text_process)
|
| 19 |
+
# ##homophone_replacement
|
| 20 |
+
# input_text_process = homophone_replacement(input_text, error_rate=0.12)
|
| 21 |
+
# list_text_response.append(input_text_process)
|
| 22 |
+
# ## common_misspelling_replacement
|
| 23 |
+
# input_text_process = common_misspelling_replacement(input_text)
|
| 24 |
+
# list_text_response.append(input_text_process)
|
| 25 |
+
# ##similar_character_replacement
|
| 26 |
+
# input_text_process = similar_character_replacement(input_text)
|
| 27 |
+
# list_text_response.append(input_text_process)
|
| 28 |
+
# ##random_space_insertion
|
| 29 |
+
# input_text_process = random_space_insertion(input_text)
|
| 30 |
+
# list_text_response.append(input_text_process)
|
| 31 |
+
# string_text_response = '\n'.join(list_text_response)
|
| 32 |
|
| 33 |
##character_replacement
|
| 34 |
+
input_text_process = character_replacement(input_text, error_rate=0.01)
|
|
|
|
|
|
|
| 35 |
##character_insertion
|
| 36 |
+
input_text_process = character_insertion(input_text_process, error_rate=0.01)
|
|
|
|
| 37 |
##character_deletion
|
| 38 |
+
input_text_process = character_deletion(input_text_process, error_rate=0.01)
|
|
|
|
| 39 |
##character_transposition
|
| 40 |
+
input_text_process = character_transposition(input_text_process, error_rate=0.01)
|
|
|
|
| 41 |
##homophone_replacement
|
| 42 |
+
input_text_process = homophone_replacement(input_text_process, error_rate=0.01)
|
|
|
|
| 43 |
## common_misspelling_replacement
|
| 44 |
+
input_text_process = common_misspelling_replacement(input_text_process, error_rate=0.01)
|
|
|
|
| 45 |
##similar_character_replacement
|
| 46 |
+
input_text_process = similar_character_replacement(input_text_process, error_rate=0.01)
|
| 47 |
+
##random_space_insertion
|
| 48 |
+
input_text_process = random_space_insertion(input_text_process, error_rate=0.01)
|
| 49 |
+
##random_space_removal
|
| 50 |
+
input_text_process = random_space_removal(input_text_process)
|
| 51 |
+
|
| 52 |
+
string_text_response = input_text_process
|
| 53 |
return str(string_text_response)
|
| 54 |
|
| 55 |
demo = gr.Interface(fn=final_result, inputs="textbox", outputs="textbox")
|
requirements.txt
CHANGED
|
Binary files a/requirements.txt and b/requirements.txt differ
|
|
|
utils.py
CHANGED
|
@@ -4,7 +4,14 @@ import numpy as np
|
|
| 4 |
import random
|
| 5 |
import math
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
'''
|
| 9 |
:param text: Gồm 1 câu đúng chính tả
|
| 10 |
:param error_rate: tỷ lệ lỗi sai muốn thêm
|
|
@@ -31,6 +38,7 @@ def character_replacement(text, error_rate=0.03, C=0.01):
|
|
| 31 |
Sử dụng ''.join(text) để ghép danh sách các ký tự lại thành chuỗi văn bản hoàn chỉnh.
|
| 32 |
'''
|
| 33 |
|
|
|
|
| 34 |
# Tính toán xác suất thay thế ký tự dựa trên error_rate sử dụng hàm logarit
|
| 35 |
augmentation_probability = C / math.log(error_rate + 1, 10)
|
| 36 |
|
|
@@ -141,7 +149,7 @@ def character_replacement(text, error_rate=0.03, C=0.01):
|
|
| 141 |
return final_text
|
| 142 |
|
| 143 |
|
| 144 |
-
def character_insertion(text, error_rate=0.03, C=
|
| 145 |
'''
|
| 146 |
:param text: Gồm 1 câu đúng chính tả
|
| 147 |
:param error_rate: tỷ lệ lỗi sai muốn thêm
|
|
@@ -194,7 +202,7 @@ def character_insertion(text, error_rate=0.03, C=0.01):
|
|
| 194 |
return ''.join(text)
|
| 195 |
|
| 196 |
|
| 197 |
-
def character_deletion(text, error_rate=0.03, C=
|
| 198 |
'''
|
| 199 |
:param text: Gồm 1 câu đúng chính tả
|
| 200 |
:param error_rate: tỷ lệ lỗi sai muốn thêm
|
|
@@ -234,7 +242,7 @@ def character_deletion(text, error_rate=0.03, C=0.01):
|
|
| 234 |
# Ghép các ký tự lại thành chuỗi văn bản
|
| 235 |
return ''.join(text)
|
| 236 |
|
| 237 |
-
def character_transposition(text, error_rate=0.03, C=
|
| 238 |
'''
|
| 239 |
:param text: Gồm 1 câu đúng chính tả
|
| 240 |
:param error_rate: tỷ lệ lỗi sai muốn thêm
|
|
@@ -273,7 +281,7 @@ def character_transposition(text, error_rate=0.03, C=0.01):
|
|
| 273 |
# Ghép các ký tự lại thành chuỗi văn bản
|
| 274 |
return ''.join(text)
|
| 275 |
|
| 276 |
-
def homophone_replacement(text, error_rate=0.03, C=
|
| 277 |
'''
|
| 278 |
:param text: Gồm 1 câu đúng chính tả
|
| 279 |
:param error_rate: tỷ lệ lỗi sai muốn thêm
|
|
@@ -340,7 +348,7 @@ def homophone_replacement(text, error_rate=0.03, C=0.01):
|
|
| 340 |
return ' '.join(words)
|
| 341 |
|
| 342 |
|
| 343 |
-
def common_misspelling_replacement(text, error_rate=0.12, C=
|
| 344 |
'''
|
| 345 |
:param text: Gồm 1 câu đúng chính tả
|
| 346 |
:param error_rate: tỷ lệ lỗi sai muốn thêm
|
|
@@ -396,7 +404,7 @@ def common_misspelling_replacement(text, error_rate=0.12, C=0.01):
|
|
| 396 |
return ' '.join(words)
|
| 397 |
|
| 398 |
|
| 399 |
-
def similar_character_replacement(text, error_rate=0.03, C=
|
| 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
|
|
@@ -452,3 +460,71 @@ def similar_character_replacement(text, error_rate=0.03, C=0.01):
|
|
| 452 |
# Ghép các ký tự lại thành chuỗi văn bản
|
| 453 |
return ''.join(characters)
|
| 454 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
|
|
|
| 38 |
Sử dụng ''.join(text) để ghép danh sách các ký tự lại thành chuỗi văn bản hoàn chỉnh.
|
| 39 |
'''
|
| 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 |
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 460 |
# Ghép các ký tự lại thành chuỗi văn bản
|
| 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
|
| 468 |
+
:param C: hằng số để tính toán xác suất chèn khoảng trắng ngẫu nhiên
|
| 469 |
+
:return: Gồm 1 câu sai chính tả
|
| 470 |
+
|
| 471 |
+
Thực hiện chèn khoảng trắng ngẫu nhiên vào giữa từ.
|
| 472 |
+
'''
|
| 473 |
+
|
| 474 |
+
# Chuyển đổi văn bản thành danh sách ký tự để có thể chèn khoảng trắng
|
| 475 |
+
characters = list(text)
|
| 476 |
+
|
| 477 |
+
# Tìm các vị trí có thể chèn khoảng trắng
|
| 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)
|
| 485 |
+
if num_errors < 1:
|
| 486 |
+
num_errors = 1 if random.random() > 0.5 else 0
|
| 487 |
+
|
| 488 |
+
for _ in range(num_errors):
|
| 489 |
+
if len(candidate_indices) > 0 and random.random() <= augmentation_probability:
|
| 490 |
+
index = random.choice(candidate_indices)
|
| 491 |
+
characters.insert(index, ' ')
|
| 492 |
+
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í
|
| 493 |
+
|
| 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
|
| 501 |
+
:param C: hằng số để tính toán xác suất bỏ dấu cách
|
| 502 |
+
:return: Gồm 1 câu bị thiếu dấu cách
|
| 503 |
+
|
| 504 |
+
Thực hiện bỏ ngẫu nhiên một vài dấu cách giữa các từ.
|
| 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()
|
| 512 |
+
candidate_indices = [i for i in range(len(words) - 1)]
|
| 513 |
+
|
| 514 |
+
# Nếu num_errors không được cung cấp, tính toán số lượng dấu cách cần bỏ dựa trên error_rate
|
| 515 |
+
num_errors = int(len(text) * error_rate)
|
| 516 |
+
if num_errors < 1:
|
| 517 |
+
num_errors = 1 if random.random() > 0.5 else 0
|
| 518 |
+
|
| 519 |
+
# Thực hiện bỏ dấu cách tại các vị trí ngẫu nhiên
|
| 520 |
+
selected_indices = random.sample(candidate_indices, min(num_errors, len(candidate_indices)))
|
| 521 |
+
for index in selected_indices:
|
| 522 |
+
if random.random() <= augmentation_probability:
|
| 523 |
+
words[index] = words[index] + words[index + 1]
|
| 524 |
+
words[index + 1] = '' # Xóa từ đã ghép để tránh lặp lại
|
| 525 |
+
|
| 526 |
+
# Ghép các từ lại thành chuỗi văn bản, bỏ qua các từ trống
|
| 527 |
+
return ' '.join([word for word in words if word])
|
| 528 |
+
|
| 529 |
+
|
| 530 |
+
|