Spaces:
Build error
Build error
add calc num_errors algorimth and homophon_replacement def
Browse files- __pycache__/utils.cpython-310.pyc +0 -0
- app.py +3 -1
- utils.py +70 -8
__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
|
@@ -17,7 +17,9 @@ def final_result(input_text):
|
|
| 17 |
##character_transposition
|
| 18 |
input_text_process = character_transposition(input_text, error_rate=0.03)
|
| 19 |
list_text_response.append(input_text_process)
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
##
|
| 22 |
string_text_response = '\n'.join(list_text_response)
|
| 23 |
return str(string_text_response)
|
|
|
|
| 17 |
##character_transposition
|
| 18 |
input_text_process = character_transposition(input_text, error_rate=0.03)
|
| 19 |
list_text_response.append(input_text_process)
|
| 20 |
+
##homophone_replacement
|
| 21 |
+
input_text_process = homophone_replacement(input_text, error_rate=0.12)
|
| 22 |
+
list_text_response.append(input_text_process)
|
| 23 |
##
|
| 24 |
string_text_response = '\n'.join(list_text_response)
|
| 25 |
return str(string_text_response)
|
utils.py
CHANGED
|
@@ -119,7 +119,7 @@ def character_replacement(text, error_rate=0.03, C=0.01):
|
|
| 119 |
text = list(text)
|
| 120 |
|
| 121 |
# Xác định số lượng ký tự cần thay thế dựa trên tỷ lệ lỗi
|
| 122 |
-
num_errors = int(len(text) * error_rate)
|
| 123 |
|
| 124 |
# Chọn ngẫu nhiên các vị trí để thay thế ký tự
|
| 125 |
indices = random.sample(range(len(text)), num_errors)
|
|
@@ -139,7 +139,7 @@ def character_replacement(text, error_rate=0.03, C=0.01):
|
|
| 139 |
return final_text
|
| 140 |
|
| 141 |
|
| 142 |
-
def character_insertion(text, error_rate=0.03, C=0.
|
| 143 |
'''
|
| 144 |
:param text: Gồm 1 câu đúng chính tả
|
| 145 |
:param error_rate: tỷ lệ lỗi sai muốn thêm
|
|
@@ -171,7 +171,7 @@ def character_insertion(text, error_rate=0.03, C=0.5):
|
|
| 171 |
text = list(text)
|
| 172 |
|
| 173 |
# Xác định số lượng ký tự cần thêm dựa trên tỷ lệ lỗi
|
| 174 |
-
num_errors = int(len(text) * error_rate)
|
| 175 |
|
| 176 |
# Tính toán xác suất chèn ký tự dựa trên error_rate sử dụng hàm logarit
|
| 177 |
augmentation_probability = C / math.log(error_rate + 1)
|
|
@@ -190,7 +190,7 @@ def character_insertion(text, error_rate=0.03, C=0.5):
|
|
| 190 |
return ''.join(text)
|
| 191 |
|
| 192 |
|
| 193 |
-
def character_deletion(text, error_rate=0.03, C=0.
|
| 194 |
'''
|
| 195 |
:param text: Gồm 1 câu đúng chính tả
|
| 196 |
:param error_rate: tỷ lệ lỗi sai muốn thêm
|
|
@@ -210,7 +210,7 @@ def character_deletion(text, error_rate=0.03, C=0.5):
|
|
| 210 |
text = list(text)
|
| 211 |
|
| 212 |
# Xác định số lượng ký tự cần xóa dựa trên tỷ lệ lỗi
|
| 213 |
-
num_errors = int(len(text) * error_rate)
|
| 214 |
|
| 215 |
# Tính toán xác suất xóa ký tự dựa trên error_rate sử dụng hàm logarit
|
| 216 |
augmentation_probability = C / math.log(error_rate + 1)
|
|
@@ -227,7 +227,7 @@ def character_deletion(text, error_rate=0.03, C=0.5):
|
|
| 227 |
# Ghép các ký tự lại thành chuỗi văn bản
|
| 228 |
return ''.join(text)
|
| 229 |
|
| 230 |
-
def character_transposition(text, error_rate=0.03, C=0.
|
| 231 |
'''
|
| 232 |
:param text: Gồm 1 câu đúng chính tả
|
| 233 |
:param error_rate: tỷ lệ lỗi sai muốn thêm
|
|
@@ -247,7 +247,7 @@ def character_transposition(text, error_rate=0.03, C=0.5):
|
|
| 247 |
text = list(text)
|
| 248 |
|
| 249 |
# Xác định số lượng lần hoán đổi cần thực hiện dựa trên tỷ lệ lỗi
|
| 250 |
-
num_errors = int(len(text) * error_rate)
|
| 251 |
|
| 252 |
# 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
|
| 253 |
augmentation_probability = C / math.log(error_rate + 1)
|
|
@@ -262,4 +262,66 @@ def character_transposition(text, error_rate=0.03, C=0.5):
|
|
| 262 |
text[index], text[index + 1] = text[index + 1], text[index]
|
| 263 |
|
| 264 |
# Ghép các ký tự lại thành chuỗi văn bản
|
| 265 |
-
return ''.join(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
text = list(text)
|
| 120 |
|
| 121 |
# Xác định số lượng ký tự cần thay thế dựa trên tỷ lệ lỗi
|
| 122 |
+
num_errors = int(len(text) * error_rate) if len(text) * error_rate > 1 else int(random.random())
|
| 123 |
|
| 124 |
# Chọn ngẫu nhiên các vị trí để thay thế ký tự
|
| 125 |
indices = random.sample(range(len(text)), num_errors)
|
|
|
|
| 139 |
return final_text
|
| 140 |
|
| 141 |
|
| 142 |
+
def character_insertion(text, error_rate=0.03, C=0.01):
|
| 143 |
'''
|
| 144 |
:param text: Gồm 1 câu đúng chính tả
|
| 145 |
:param error_rate: tỷ lệ lỗi sai muốn thêm
|
|
|
|
| 171 |
text = list(text)
|
| 172 |
|
| 173 |
# Xác định số lượng ký tự cần thêm dựa trên tỷ lệ lỗi
|
| 174 |
+
num_errors = int(len(text) * error_rate) if len(text) * error_rate > 1 else int(random.random())
|
| 175 |
|
| 176 |
# Tính toán xác suất chèn ký tự dựa trên error_rate sử dụng hàm logarit
|
| 177 |
augmentation_probability = C / math.log(error_rate + 1)
|
|
|
|
| 190 |
return ''.join(text)
|
| 191 |
|
| 192 |
|
| 193 |
+
def character_deletion(text, error_rate=0.03, C=0.01):
|
| 194 |
'''
|
| 195 |
:param text: Gồm 1 câu đúng chính tả
|
| 196 |
:param error_rate: tỷ lệ lỗi sai muốn thêm
|
|
|
|
| 210 |
text = list(text)
|
| 211 |
|
| 212 |
# Xác định số lượng ký tự cần xóa dựa trên tỷ lệ lỗi
|
| 213 |
+
num_errors = int(len(text) * error_rate) if len(text) * error_rate > 1 else int(random.random())
|
| 214 |
|
| 215 |
# Tính toán xác suất xóa ký tự dựa trên error_rate sử dụng hàm logarit
|
| 216 |
augmentation_probability = C / math.log(error_rate + 1)
|
|
|
|
| 227 |
# Ghép các ký tự lại thành chuỗi văn bản
|
| 228 |
return ''.join(text)
|
| 229 |
|
| 230 |
+
def character_transposition(text, error_rate=0.03, C=0.01):
|
| 231 |
'''
|
| 232 |
:param text: Gồm 1 câu đúng chính tả
|
| 233 |
:param error_rate: tỷ lệ lỗi sai muốn thêm
|
|
|
|
| 247 |
text = list(text)
|
| 248 |
|
| 249 |
# Xác định số lượng lần hoán đổi cần thực hiện dựa trên tỷ lệ lỗi
|
| 250 |
+
num_errors = int(len(text) * error_rate) if len(text) * error_rate > 1 else int(random.random())
|
| 251 |
|
| 252 |
# 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
|
| 253 |
augmentation_probability = C / math.log(error_rate + 1)
|
|
|
|
| 262 |
text[index], text[index + 1] = text[index + 1], text[index]
|
| 263 |
|
| 264 |
# Ghép các ký tự lại thành chuỗi văn bản
|
| 265 |
+
return ''.join(text)
|
| 266 |
+
|
| 267 |
+
def homophone_replacement(text, error_rate=0.03, C=0.01):
|
| 268 |
+
'''
|
| 269 |
+
:param text: Gồm 1 câu đúng chính tả
|
| 270 |
+
:param error_rate: tỷ lệ lỗi sai muốn thêm
|
| 271 |
+
:param C: hằng số để tính toán xác suất thay thế từ đồng âm
|
| 272 |
+
:return: Gồm 1 câu sai chính tả
|
| 273 |
+
|
| 274 |
+
Thực hiện thay thế từ đồng âm:
|
| 275 |
+
- Tìm các từ chứa homophones trước
|
| 276 |
+
- Sử dụng augmentation_probability để xác định liệu từ đó có bị thay thế hay không
|
| 277 |
+
Sử dụng ' '.join(words) để ghép danh sách các từ lại thành chuỗi văn bản hoàn chỉnh.
|
| 278 |
+
'''
|
| 279 |
+
|
| 280 |
+
homophones = {
|
| 281 |
+
'gi': 'd',
|
| 282 |
+
'd': 'gi',
|
| 283 |
+
's': 'x',
|
| 284 |
+
'x': 's',
|
| 285 |
+
'tr': 'ch',
|
| 286 |
+
'ch': 'tr',
|
| 287 |
+
'r': 'd',
|
| 288 |
+
'd': 'r',
|
| 289 |
+
'l': 'n',
|
| 290 |
+
'n': 'l',
|
| 291 |
+
'c': 't',
|
| 292 |
+
't': 'c',
|
| 293 |
+
'v': 'b',
|
| 294 |
+
'b': 'v',
|
| 295 |
+
'ng': 'ngh',
|
| 296 |
+
'ngh': 'ng'
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
# Chuyển đổi văn bản thành danh sách từ để có thể thay thế từ
|
| 300 |
+
words = text.split()
|
| 301 |
+
|
| 302 |
+
# Xác định các từ có chứa homophones
|
| 303 |
+
candidate_indices = []
|
| 304 |
+
for i, word in enumerate(words):
|
| 305 |
+
for key in homophones.keys():
|
| 306 |
+
if key in word:
|
| 307 |
+
candidate_indices.append(i)
|
| 308 |
+
break
|
| 309 |
+
|
| 310 |
+
# Tính toán xác suất thay thế từ dựa trên error_rate sử dụng hàm logarit
|
| 311 |
+
augmentation_probability = C / math.log(error_rate + 1)
|
| 312 |
+
|
| 313 |
+
# Thực hiện thay thế từ tại các vị trí ngẫu nhiên
|
| 314 |
+
num_errors = int(len(text) * error_rate) if len(text) * error_rate > 1 else int(random.random())
|
| 315 |
+
for _ in range(num_errors):
|
| 316 |
+
if len(candidate_indices) > 0 and random.random() <= augmentation_probability:
|
| 317 |
+
index = random.choice(candidate_indices)
|
| 318 |
+
word = words[index]
|
| 319 |
+
for key in homophones.keys():
|
| 320 |
+
if key in word:
|
| 321 |
+
word = word.replace(key, homophones[key])
|
| 322 |
+
break # Dừng lại sau khi thay thế lần đầu tiên để tránh thay thế nhiều lần
|
| 323 |
+
words[index] = word
|
| 324 |
+
candidate_indices.remove(index) # Đảm bảo từ này không bị thay thế nhiều lần
|
| 325 |
+
|
| 326 |
+
# Ghép các từ lại thành chuỗi văn bản
|
| 327 |
+
return ' '.join(words)
|