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

update num_errors fomula

Browse files
Files changed (3) hide show
  1. __pycache__/utils.cpython-310.pyc +0 -0
  2. app.py +3 -0
  3. utils.py +22 -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
@@ -20,6 +20,9 @@ def final_result(input_text):
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)
 
20
  ##homophone_replacement
21
  input_text_process = homophone_replacement(input_text, error_rate=0.12)
22
  list_text_response.append(input_text_process)
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)
utils.py CHANGED
@@ -119,7 +119,9 @@ 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) 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)
@@ -171,7 +173,9 @@ def character_insertion(text, error_rate=0.03, C=0.01):
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)
@@ -210,7 +214,10 @@ def character_deletion(text, error_rate=0.03, C=0.01):
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)
@@ -247,7 +254,9 @@ def character_transposition(text, error_rate=0.03, C=0.01):
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)
@@ -312,7 +321,10 @@ def homophone_replacement(text, error_rate=0.03, C=0.01):
312
  augmentation_probability = C / math.log(error_rate + 1)
313
 
314
  # Thực hiện thay thế từ tại các vị trí ngẫu nhiên
315
- num_errors = int(len(text) * error_rate) if len(text) * error_rate > 1 else int(random.random())
 
 
 
316
  for _ in range(num_errors):
317
  if len(candidate_indices) > 0 and random.random() <= augmentation_probability:
318
  index = random.choice(candidate_indices)
@@ -328,7 +340,7 @@ def homophone_replacement(text, error_rate=0.03, C=0.01):
328
  return ' '.join(words)
329
 
330
 
331
- def common_misspelling_replacement(text, error_rate=0.03, C=0.01):
332
  '''
333
  :param text: Gồm 1 câu đúng chính tả
334
  :param error_rate: tỷ lệ lỗi sai muốn thêm
@@ -367,7 +379,10 @@ def common_misspelling_replacement(text, error_rate=0.03, C=0.01):
367
  augmentation_probability = C / math.log(error_rate + 1)
368
 
369
  # Thực hiện thay thế từ tại các vị trí ngẫu nhiên
370
- num_errors = int(len(text) * error_rate) if len(text) * error_rate > 1 else int(random.random())
 
 
 
371
  for _ in range(num_errors):
372
  if len(candidate_indices) > 0 and random.random() <= augmentation_probability:
373
  index = random.choice(candidate_indices)
 
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
+ if num_errors < 1:
124
+ num_errors = 1 if random.random() > 0.5 else 0
125
 
126
  # Chọn ngẫu nhiên các vị trí để thay thế ký tự
127
  indices = random.sample(range(len(text)), num_errors)
 
173
  text = list(text)
174
 
175
  # Xác định số lượng ký tự cần thêm dựa trên tỷ lệ lỗi
176
+ num_errors = int(len(text) * error_rate)
177
+ if num_errors < 1:
178
+ num_errors = 1 if random.random() > 0.5 else 0
179
 
180
  # Tính toán xác suất chèn ký tự dựa trên error_rate sử dụng hàm logarit
181
  augmentation_probability = C / math.log(error_rate + 1)
 
214
  text = list(text)
215
 
216
  # Xác định số lượng ký tự cần xóa dựa trên tỷ lệ lỗi
217
+ num_errors = int(len(text) * error_rate)
218
+ if num_errors < 1:
219
+ num_errors = 1 if random.random() > 0.5 else 0
220
+
221
 
222
  # Tính toán xác suất xóa ký tự dựa trên error_rate sử dụng hàm logarit
223
  augmentation_probability = C / math.log(error_rate + 1)
 
254
  text = list(text)
255
 
256
  # 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
257
+ num_errors = int(len(text) * error_rate)
258
+ if num_errors < 1:
259
+ num_errors = 1 if random.random() > 0.5 else 0
260
 
261
  # 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
262
  augmentation_probability = C / math.log(error_rate + 1)
 
321
  augmentation_probability = C / math.log(error_rate + 1)
322
 
323
  # Thực hiện thay thế từ tại các vị trí ngẫu nhiên
324
+ num_errors = int(len(text) * error_rate)
325
+ if num_errors < 1:
326
+ num_errors = 1 if random.random() > 0.5 else 0
327
+
328
  for _ in range(num_errors):
329
  if len(candidate_indices) > 0 and random.random() <= augmentation_probability:
330
  index = random.choice(candidate_indices)
 
340
  return ' '.join(words)
341
 
342
 
343
+ def common_misspelling_replacement(text, error_rate=0.12, C=0.01):
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
 
379
  augmentation_probability = C / math.log(error_rate + 1)
380
 
381
  # Thực hiện thay thế từ tại các vị trí ngẫu nhiên
382
+ num_errors = int(len(text) * error_rate)
383
+ if num_errors < 1:
384
+ num_errors = 1 if random.random() > 0.5 else 0
385
+
386
  for _ in range(num_errors):
387
  if len(candidate_indices) > 0 and random.random() <= augmentation_probability:
388
  index = random.choice(candidate_indices)