|
|
import pandas as pd |
|
|
import numpy as np |
|
|
|
|
|
import random |
|
|
import math |
|
|
|
|
|
import fomula |
|
|
|
|
|
|
|
|
SER_want = 0.15 |
|
|
SER_max = 0.3 |
|
|
k_sigmoid = fomula.get_k_sigmoid(x=SER_max - SER_want) |
|
|
print("k_sigmoid", k_sigmoid) |
|
|
def character_replacement(text, SER, error_rate=0.03): |
|
|
''' |
|
|
:param text: Gồm 1 câu đúng chính tả |
|
|
:param error_rate: tỷ lệ lỗi sai muốn thêm |
|
|
:param C: hằng số để tính toán xác suất thay thế ký tự |
|
|
:SER: Tỷ lệ lỗi đã sửa / tổng lỗi |
|
|
:return: Gồm 1 câu sai chính tả |
|
|
|
|
|
Kết hợp phương pháp error_rate và augmentation_probability để tạo lỗi chính tả. |
|
|
|
|
|
Bàn phím QWERTY tiếng Việt: |
|
|
|
|
|
Tạo một từ điển (keyboard) chứa các ký tự và các ký tự gần đó trên bàn phím. Ví dụ, ký tự 'a' có thể được thay thế bằng 'â', 'ă', 'á',... |
|
|
Chuyển đổi văn bản thành danh sách ký tự: |
|
|
|
|
|
Sử dụng list(text) để chuyển chuỗi văn bản thành danh sách các ký tự riêng lẻ. Điều này giúp dễ dàng thay thế từng ký tự. |
|
|
Chọn ngẫu nhiên các vị trí để thay thế ký tự: |
|
|
|
|
|
Sử dụng random.sample để chọn ngẫu nhiên các vị trí dựa trên tỷ lệ lỗi. Điều này giúp tạo ra các lỗi chính tả ngẫu nhiên và đa dạng. |
|
|
Thực hiện thay thế ký tự: |
|
|
|
|
|
Với mỗi vị trí đã chọn, kiểm tra nếu ký tự đó có trong từ điển keyboard. Nếu có, chọn ngẫu nhiên một ký tự từ các ký tự gần đó trên bàn phím. |
|
|
Giữ nguyên kiểu chữ hoa hoặc chữ thường của ký tự gốc. |
|
|
Ghép lại thành chuỗi văn bản: |
|
|
|
|
|
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. |
|
|
''' |
|
|
|
|
|
|
|
|
|
|
|
augmentation_probability = fomula.AP_fomula(error_rate=error_rate) |
|
|
|
|
|
|
|
|
keyboard = { |
|
|
'a': 'âăáàạảãấầậẩẫắằặẳẵ', |
|
|
'â': 'aăáàạảãấầậẩẫắằặẳẵ', |
|
|
'ă': 'aâáàạảãấầậẩẫắằặẳẵ', |
|
|
'á': 'aâăàạảãấầậẩẫắằặẳẵ', |
|
|
'à': 'aâăáạảãấầậẩẫắằặẳẵ', |
|
|
'ạ': 'aâăáàảãấầậẩẫắằặẳẵ', |
|
|
'ả': 'aâăáàạãấầậẩẫắằặẳẵ', |
|
|
'ã': 'aâăáàạảấầậẩẫắằặẳẵ', |
|
|
'â': 'aăáàạảãấầậẩẫắằặẳẵ', |
|
|
'b': 'vn', |
|
|
'c': 'xv', |
|
|
'd': 'đ', |
|
|
'đ': 'd', |
|
|
'e': 'êéèẹẻẽếềệểễ', |
|
|
'ê': 'eéèẹẻẽếềệểễ', |
|
|
'é': 'eêèẹẻẽếềệểễ', |
|
|
'è': 'eêéẹẻẽếềệểễ', |
|
|
'ẹ': 'eêéèẻẽếềệểễ', |
|
|
'ẻ': 'eêéèẹẽếềệểễ', |
|
|
'ẽ': 'eêéèẹẻếềệểễ', |
|
|
'ế': 'eêéèẹẻẽềệểễ', |
|
|
'ề': 'eêéèẹẻẽếệểễ', |
|
|
'ệ': 'eêéèẹẻẽếềểễ', |
|
|
'ể': 'eêéèẹẻẽếềệễ', |
|
|
'ễ': 'eêéèẹẻẽếềệ', |
|
|
'f': 'r', |
|
|
'g': 'hf', |
|
|
'h': 'g', |
|
|
'i': 'ìíỉĩị', |
|
|
'í': 'iìỉĩị', |
|
|
'ì': 'iíỉĩị', |
|
|
'ỉ': 'iíìĩị', |
|
|
'ĩ': 'iíìỉị', |
|
|
'ị': 'iíìỉĩ', |
|
|
'k': 'l', |
|
|
'l': 'k;', |
|
|
'm': 'n', |
|
|
'n': 'm', |
|
|
'o': 'ôơóòọỏõốồộổỗớờợởỡ', |
|
|
'ô': 'oơóòọỏõốồộổỗớờợởỡ', |
|
|
'ơ': 'oôóòọỏõốồộổỗớờợởỡ', |
|
|
'ó': 'oôơòọỏõốồộổỗớờợởỡ', |
|
|
'ò': 'oôơóọỏõốồộổỗớờợởỡ', |
|
|
'ọ': 'oôơóòỏõốồộổỗớờợởỡ', |
|
|
'ỏ': 'oôơóòọõốồộổỗớờợởỡ', |
|
|
'õ': 'oôơóòọỏốồộổỗớờợởỡ', |
|
|
'ố': 'oôơóòọỏõồộổỗớờợởỡ', |
|
|
'ồ': 'oôơóòọỏõốộổỗớờợởỡ', |
|
|
'ộ': 'oôơóòọỏõốồổỗớờợởỡ', |
|
|
'ổ': 'oôơóòọỏõốồộỗớờợởỡ', |
|
|
'ỗ': 'oôơóòọỏõốồộổỗớờợởỡ', |
|
|
'ớ': 'oôơóòọỏõốồộổỗờợởỡ', |
|
|
'ờ': 'oôơóòọỏõốồộổỗớờợởỡ', |
|
|
'ợ': 'oôơóòọỏõốồộổỗớờợởỡ', |
|
|
'ở': 'oôơóòọỏõốồộổỗớờợỡ', |
|
|
'ỡ': 'oôơóòọỏõốồộổỗớờợở', |
|
|
'p': 'q', |
|
|
'q': 'p', |
|
|
'r': 'f', |
|
|
's': 'd', |
|
|
't': 'y', |
|
|
'u': 'ưúùụủũứừựửữ', |
|
|
'ư': 'uúùụủũứừựửữ', |
|
|
'ú': 'uưùụủũứừựửữ', |
|
|
'ù': 'uưúụủũứừựửữ', |
|
|
'ụ': 'uưúùủũứừựửữ', |
|
|
'ủ': 'uưúùụũứừựửữ', |
|
|
'ũ': 'uưúùụủứừựửữ', |
|
|
'ứ': 'uưúùụủũừựửữ', |
|
|
'ừ': 'uưúùụủũứựửữ', |
|
|
'ự': 'uưúùụủũứừửữ', |
|
|
'ử': 'uưúùụủũứừựữ', |
|
|
'ữ': 'uưúùụủũứừựử', |
|
|
'v': 'b', |
|
|
'x': 'c', |
|
|
'y': 't', |
|
|
'z': 's' |
|
|
} |
|
|
|
|
|
|
|
|
text = list(text) |
|
|
|
|
|
|
|
|
num_errors = int(len(text) * error_rate) |
|
|
if num_errors < 1: |
|
|
num_errors = 1 if random.random() > 0.5 else 0 |
|
|
|
|
|
|
|
|
indices = random.sample(range(len(text)), num_errors) |
|
|
|
|
|
num_of_change = 0 |
|
|
|
|
|
sigmoid = 1 if SER <= SER_want else (1 - (1 / (1 + pow(math.e, - (SER - SER_want) * k_sigmoid)))) |
|
|
print(sigmoid) |
|
|
|
|
|
|
|
|
for index in indices: |
|
|
if text[index].lower() in keyboard and random.random() <= augmentation_probability * sigmoid: |
|
|
num_of_change += 1 |
|
|
|
|
|
replacement_char = random.choice(keyboard[text[index].lower()]) |
|
|
|
|
|
if text[index].isupper(): |
|
|
replacement_char = replacement_char.upper() |
|
|
text[index] = replacement_char |
|
|
|
|
|
|
|
|
final_text = ''.join(text) |
|
|
|
|
|
print("character_replacement: ", num_of_change) |
|
|
|
|
|
return final_text, num_of_change |
|
|
|
|
|
|
|
|
def character_insertion(text, SER, error_rate=0.03): |
|
|
''' |
|
|
:param text: Gồm 1 câu đúng chính tả |
|
|
:param error_rate: tỷ lệ lỗi sai muốn thêm |
|
|
:param C: hằng số để tính toán xác suất chèn ký tự |
|
|
:return: Gồm 1 câu sai chính tả |
|
|
|
|
|
Bàn phím QWERTY tiếng Việt: |
|
|
|
|
|
Một chuỗi ký tự bao gồm tất cả các ký tự chữ cái tiếng Việt và các ký tự dấu (â, ă, ê, ô, ơ, ư, đ). |
|
|
Chuyển đổi văn bản thành danh sách ký tự: |
|
|
|
|
|
Sử dụng list(text) để chuyển chuỗi văn bản thành danh sách các ký tự riêng lẻ. Điều này giúp dễ dàng thêm ký tự vào bất kỳ vị trí nào trong văn bản. |
|
|
Thực hiện thêm ký tự: |
|
|
|
|
|
Sử dụng vòng lặp for _ in range(num_errors) để thêm một số lượng ký tự ngẫu nhiên xác định (num_errors). |
|
|
Mỗi lần trong vòng lặp: |
|
|
Sử dụng random.randint(0, len(text)) để chọn ngẫu nhiên một vị trí trong văn bản nơi ký tự sẽ được thêm vào. Vị trí này có thể nằm ở bất kỳ đâu trong văn bản, bao gồm cả đầu và cuối văn bản. |
|
|
Sử dụng random.choice(keyboard) để chọn ngẫu nhiên một ký tự từ chuỗi keyboard. |
|
|
Sử dụng text.insert(index, char_to_insert) để thêm ký tự đã chọn vào vị trí đã chọn trong danh sách ký tự. |
|
|
Ghép lại thành chuỗi văn bản: |
|
|
|
|
|
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. |
|
|
''' |
|
|
|
|
|
|
|
|
keyboard = 'abcdefghijklmnopqrstuvwxyzâăáàạảãấầậẩẫắằặẳẵêéèẹẻẽếềệểễìíỉĩịôơóòọỏõốồộổỗớờợởỡưúùụủũứừựửữđ' |
|
|
|
|
|
|
|
|
text = list(text) |
|
|
|
|
|
|
|
|
num_errors = int(len(text) * error_rate) |
|
|
if num_errors < 1: |
|
|
num_errors = 1 if random.random() > 0.5 else 0 |
|
|
|
|
|
|
|
|
augmentation_probability = fomula.AP_fomula(error_rate=error_rate) |
|
|
|
|
|
num_of_change = 0 |
|
|
|
|
|
|
|
|
sigmoid = 1 if SER <= SER_want else (1 - (1 / (1 + pow(math.e, -(SER - SER_want) * k_sigmoid)))) |
|
|
print(sigmoid) |
|
|
|
|
|
|
|
|
for _ in range(num_errors): |
|
|
|
|
|
if random.random() <= augmentation_probability * sigmoid: |
|
|
num_of_change += 1 |
|
|
index = random.randint(0, len(text)) |
|
|
|
|
|
char_to_insert = random.choice(keyboard) |
|
|
|
|
|
text.insert(index, char_to_insert) |
|
|
|
|
|
|
|
|
print("character_insertion: ", num_of_change) |
|
|
return ''.join(text), num_of_change |
|
|
|
|
|
|
|
|
def character_deletion(text, SER, error_rate=0.03): |
|
|
''' |
|
|
:param text: Gồm 1 câu đúng chính tả |
|
|
:param error_rate: tỷ lệ lỗi sai muốn thêm |
|
|
:param C: hằng số để tính toán xác suất xóa ký tự |
|
|
:return: Gồm 1 câu sai chính tả |
|
|
|
|
|
Thực hiện xóa ký tự: |
|
|
Sử dụng vòng lặp for _ in range(num_errors) để xóa một số lượng ký tự ngẫu nhiên xác định (num_errors). |
|
|
Mỗi lần trong vòng lặp: |
|
|
Sử dụng random.randint(0, len(text)-1) để chọn ngẫu nhiên một vị trí trong văn bản nơi ký tự sẽ bị xóa. |
|
|
Sử dụng text.pop(index) để xóa ký tự tại vị trí đã chọn. |
|
|
Ghép lại thành chuỗi văn bản: |
|
|
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. |
|
|
''' |
|
|
|
|
|
|
|
|
text = list(text) |
|
|
|
|
|
|
|
|
num_errors = int(len(text) * error_rate) |
|
|
if num_errors < 1: |
|
|
num_errors = 1 if random.random() > 0.5 else 0 |
|
|
|
|
|
|
|
|
|
|
|
augmentation_probability = fomula.AP_fomula(error_rate=error_rate) |
|
|
|
|
|
num_of_change = 0 |
|
|
|
|
|
sigmoid = 1 if SER <= SER_want else (1 - (1 / (1 + pow(math.e, -(SER - SER_want) * k_sigmoid)))) |
|
|
print(sigmoid) |
|
|
|
|
|
|
|
|
for _ in range(num_errors): |
|
|
|
|
|
if random.random() <= augmentation_probability * sigmoid: |
|
|
if len(text) > 0: |
|
|
num_of_change += 1 |
|
|
index = random.randint(0, len(text) - 1) |
|
|
|
|
|
text.pop(index) |
|
|
|
|
|
|
|
|
print("character_deletion: ", num_of_change) |
|
|
return ''.join(text), num_of_change |
|
|
|
|
|
def character_transposition(text, SER, error_rate=0.03): |
|
|
''' |
|
|
:param text: Gồm 1 câu đúng chính tả |
|
|
:param error_rate: tỷ lệ lỗi sai muốn thêm |
|
|
:param C: hằng số để tính toán xác suất hoán đổi ký tự |
|
|
:return: Gồm 1 câu sai chính tả |
|
|
|
|
|
Thực hiện hoán đổi ký tự: |
|
|
Sử dụng vòng lặp for _ in range(num_errors) để hoán đổi một số lượng ký tự ngẫu nhiên xác định (num_errors). |
|
|
Mỗi lần trong vòng lặp: |
|
|
Sử dụng random.randint(0, len(text)-2) để chọn ngẫu nhiên một vị trí trong văn bản nơi ký tự sẽ bị hoán đổi. |
|
|
Hoán đổi hai ký tự liên tiếp tại vị trí đã chọn. |
|
|
Ghép lại thành chuỗi văn bản: |
|
|
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. |
|
|
''' |
|
|
|
|
|
|
|
|
text = list(text) |
|
|
|
|
|
|
|
|
num_errors = int(len(text) * error_rate) |
|
|
if num_errors < 1: |
|
|
num_errors = 1 if random.random() > 0.5 else 0 |
|
|
|
|
|
|
|
|
augmentation_probability = fomula.AP_fomula(error_rate=error_rate) |
|
|
num_of_change = 0 |
|
|
|
|
|
sigmoid = 1 if SER <= SER_want else (1 - (1 / (1 + pow(math.e, -(SER - SER_want) * k_sigmoid)))) |
|
|
print(sigmoid) |
|
|
|
|
|
|
|
|
for _ in range(num_errors): |
|
|
|
|
|
if random.random() <= augmentation_probability * sigmoid: |
|
|
if len(text) > 1: |
|
|
num_of_change += 2 |
|
|
index = random.randint(0, len(text) - 2) |
|
|
|
|
|
text[index], text[index + 1] = text[index + 1], text[index] |
|
|
|
|
|
|
|
|
print("character_transposition: ", num_of_change) |
|
|
return ''.join(text), num_of_change |
|
|
|
|
|
def homophone_replacement(text, SER, error_rate=0.03): |
|
|
''' |
|
|
:param text: Gồm 1 câu đúng chính tả |
|
|
:param error_rate: tỷ lệ lỗi sai muốn thêm |
|
|
:param C: hằng số để tính toán xác suất thay thế từ đồng âm |
|
|
:return: Gồm 1 câu sai chính tả |
|
|
|
|
|
Thực hiện thay thế từ đồng âm: |
|
|
- Tìm các từ chứa homophones trước |
|
|
- Sử dụng augmentation_probability để xác định liệu từ đó có bị thay thế hay không |
|
|
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. |
|
|
''' |
|
|
|
|
|
homophones = { |
|
|
'gi': 'd', |
|
|
'd': 'gi', |
|
|
's': 'x', |
|
|
'x': 's', |
|
|
'tr': 'ch', |
|
|
'ch': 'tr', |
|
|
'r': 'd', |
|
|
'd': 'r', |
|
|
'l': 'n', |
|
|
'n': 'l', |
|
|
'c': 't', |
|
|
't': 'c', |
|
|
'v': 'b', |
|
|
'b': 'v', |
|
|
'ng': 'ngh', |
|
|
'ngh': 'ng', |
|
|
'v': 'z' |
|
|
} |
|
|
|
|
|
|
|
|
words = text.split() |
|
|
|
|
|
|
|
|
candidate_indices = [] |
|
|
for i, word in enumerate(words): |
|
|
for key in homophones.keys(): |
|
|
if key in word: |
|
|
candidate_indices.append(i) |
|
|
break |
|
|
|
|
|
|
|
|
augmentation_probability = fomula.AP_fomula(error_rate=error_rate) |
|
|
num_of_change = 0 |
|
|
|
|
|
num_errors = int(len(text) * error_rate) |
|
|
if num_errors < 1: |
|
|
num_errors = 1 if random.random() > 0.5 else 0 |
|
|
|
|
|
sigmoid = 1 if SER <= SER_want else (1 - (1 / (1 + pow(math.e, -(SER - SER_want) * k_sigmoid)))) |
|
|
print(sigmoid) |
|
|
|
|
|
for _ in range(num_errors): |
|
|
if len(candidate_indices) > 0 and random.random() <= augmentation_probability * sigmoid: |
|
|
index = random.choice(candidate_indices) |
|
|
word = words[index] |
|
|
for key in homophones.keys(): |
|
|
if key in word: |
|
|
word = word.replace(key, homophones[key]) |
|
|
num_of_change += np.abs(len(key) - len(homophones[key])) |
|
|
break |
|
|
words[index] = word |
|
|
candidate_indices.remove(index) |
|
|
|
|
|
|
|
|
print("homophone_replacement: ", num_of_change) |
|
|
return ' '.join(words), num_of_change |
|
|
|
|
|
|
|
|
def common_misspelling_replacement(text, SER, error_rate=0.12): |
|
|
''' |
|
|
:param text: Gồm 1 câu đúng chính tả |
|
|
:param error_rate: tỷ lệ lỗi sai muốn thêm |
|
|
:param C: hằng số để tính toán xác suất thay thế từ sai chính tả phổ biến |
|
|
:return: Gồm 1 câu sai chính tả |
|
|
|
|
|
Thực hiện thay thế từ bằng các phiên bản sai chính tả phổ biến. |
|
|
- Tìm các từ có thể thay thế trước |
|
|
- Sử dụng augmentation_probability để xác định liệu từ đó có bị thay thế hay không |
|
|
''' |
|
|
|
|
|
|
|
|
common_misspellings = { |
|
|
'những': ['nhửng', 'nhừng'], |
|
|
'của': ['cuả', 'cũa'], |
|
|
'cái': ['cái', 'cã'], |
|
|
'giải': ['giãi', 'giải'], |
|
|
'pháp': ['pháp', 'fáp'], |
|
|
'đúng': ['dúng', 'đúng'], |
|
|
'rất': ['rất', 'rậc'], |
|
|
'sáng': ['sán', 'sáng'], |
|
|
'tạo': ['tạo', 'tạu'] |
|
|
} |
|
|
|
|
|
|
|
|
words = text.split() |
|
|
|
|
|
|
|
|
candidate_indices = [i for i, word in enumerate(words) if word in common_misspellings] |
|
|
|
|
|
|
|
|
if len(candidate_indices) == 0: |
|
|
return ' '.join(words), 0 |
|
|
|
|
|
|
|
|
augmentation_probability = fomula.AP_fomula(error_rate=error_rate) |
|
|
num_of_change = 0 |
|
|
|
|
|
num_errors = int(len(text) * error_rate) |
|
|
if num_errors < 1: |
|
|
num_errors = 1 if random.random() > 0.5 else 0 |
|
|
|
|
|
sigmoid = 1 if SER <= SER_want else (1 - (1 / (1 + pow(math.e, -(SER - SER_want) * k_sigmoid)))) |
|
|
print(sigmoid) |
|
|
|
|
|
for _ in range(num_errors): |
|
|
if len(candidate_indices) > 0 and random.random() <= augmentation_probability * sigmoid: |
|
|
index = random.choice(candidate_indices) |
|
|
word = words[index] |
|
|
|
|
|
misspelled_word = random.choice(common_misspellings[word]) |
|
|
words[index] = misspelled_word |
|
|
num_of_change += np.abs(len(misspelled_word) - len(word)) |
|
|
candidate_indices.remove(index) |
|
|
|
|
|
|
|
|
print("common_misspelling_replacement: ", num_of_change) |
|
|
return ' '.join(words), num_of_change |
|
|
|
|
|
|
|
|
def similar_character_replacement(text, SER, error_rate=0.03): |
|
|
''' |
|
|
:param text: Gồm 1 câu đúng chính tả |
|
|
:param error_rate: tỷ lệ lỗi sai muốn thêm |
|
|
:param C: hằng số để tính toán xác suất thay thế ký tự tương tự |
|
|
:return: Gồm 1 câu sai chính tả |
|
|
|
|
|
Thực hiện thay thế ký tự bằng ký tự tương tự trong bộ mã Unicode. |
|
|
- Tìm các ký tự có thể thay thế trước |
|
|
- Sử dụng augmentation_probability để xác định liệu ký tự đó có bị thay thế hay không |
|
|
''' |
|
|
|
|
|
|
|
|
similar_characters = { |
|
|
'a': ['á', 'à', 'ả', 'ã', 'ạ', 'â', 'ă'], |
|
|
'e': ['é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê'], |
|
|
'i': ['í', 'ì', 'ỉ', 'ĩ', 'ị'], |
|
|
'o': ['ó', 'ò', 'ỏ', 'õ', 'ọ', 'ô', 'ơ'], |
|
|
'u': ['ú', 'ù', 'ủ', 'ũ', 'ụ', 'ư'], |
|
|
'y': ['ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ'], |
|
|
'd': ['đ'], |
|
|
'A': ['Á', 'À', 'Ả', 'Ã', 'Ạ', 'Â', 'Ă'], |
|
|
'E': ['É', 'È', 'Ẻ', 'Ẽ', 'Ẹ', 'Ê'], |
|
|
'I': ['Í', 'Ì', 'Ỉ', 'Ĩ', 'Ị'], |
|
|
'O': ['Ó', 'Ò', 'Ỏ', 'Õ', 'Ọ', 'Ô', 'Ơ'], |
|
|
'U': ['Ú', 'Ù', 'Ủ', 'Ũ', 'Ụ', 'Ư'], |
|
|
'Y': ['Ý', 'Ỳ', 'Ỷ', 'Ỹ', 'Ỵ'], |
|
|
'D': ['Đ'] |
|
|
} |
|
|
|
|
|
|
|
|
characters = list(text) |
|
|
|
|
|
|
|
|
candidate_indices = [i for i, char in enumerate(characters) if char in similar_characters] |
|
|
|
|
|
|
|
|
augmentation_probability = fomula.AP_fomula(error_rate=error_rate) |
|
|
num_of_change = 0 |
|
|
|
|
|
num_errors = int(len(text) * error_rate) |
|
|
if num_errors < 1: |
|
|
num_errors = 1 if random.random() > 0.5 else 0 |
|
|
|
|
|
sigmoid = 1 if SER <= SER_want else (1 - (1 / (1 + pow(math.e, -(SER - SER_want) * k_sigmoid)))) |
|
|
print(sigmoid) |
|
|
|
|
|
for _ in range(num_errors): |
|
|
if len(candidate_indices) > 0 and random.random() <= augmentation_probability * sigmoid: |
|
|
num_of_change += 1 |
|
|
index = random.choice(candidate_indices) |
|
|
char = characters[index] |
|
|
|
|
|
similar_char = random.choice(similar_characters[char]) |
|
|
characters[index] = similar_char |
|
|
candidate_indices.remove(index) |
|
|
|
|
|
|
|
|
print("similar_character_replacement: ", num_of_change) |
|
|
return ''.join(characters), num_of_change |
|
|
|
|
|
|
|
|
def random_space_insertion(text, SER, error_rate=0.025): |
|
|
''' |
|
|
:param text: Gồm 1 câu đúng chính tả |
|
|
:param error_rate: tỷ lệ lỗi sai muốn thêm |
|
|
:param C: hằng số để tính toán xác suất chèn khoảng trắng ngẫu nhiên |
|
|
:return: Gồm 1 câu sai chính tả |
|
|
|
|
|
Thực hiện chèn khoảng trắng ngẫu nhiên vào giữa từ. |
|
|
''' |
|
|
|
|
|
|
|
|
characters = list(text) |
|
|
|
|
|
|
|
|
candidate_indices = [i for i in range(1, len(characters))] |
|
|
|
|
|
|
|
|
augmentation_probability = fomula.AP_fomula(error_rate=error_rate) |
|
|
num_of_change = 0 |
|
|
|
|
|
num_errors = int(len(text) * error_rate) |
|
|
if num_errors < 1: |
|
|
num_errors = 1 if random.random() > 0.5 else 0 |
|
|
|
|
|
sigmoid = 1 if SER <= SER_want else (1 - (1 / (1 + pow(math.e, -(SER - SER_want) * k_sigmoid)))) |
|
|
print(sigmoid) |
|
|
|
|
|
for _ in range(num_errors): |
|
|
if len(candidate_indices) > 0 and random.random() <= augmentation_probability * sigmoid: |
|
|
num_of_change += 1 |
|
|
index = random.choice(candidate_indices) |
|
|
characters.insert(index, ' ') |
|
|
candidate_indices.remove(index) |
|
|
|
|
|
|
|
|
print("random_space_insertion: ", num_of_change) |
|
|
return ''.join(characters), num_of_change |
|
|
|
|
|
def random_space_removal(text, SER, error_rate=0.01): |
|
|
''' |
|
|
:param text: Gồm 1 câu đúng chính tả |
|
|
:param error_rate: tỷ lệ lỗi sai muốn thêm |
|
|
:param C: hằng số để tính toán xác suất bỏ dấu cách |
|
|
:return: Gồm 1 câu bị thiếu dấu cách |
|
|
|
|
|
Thực hiện bỏ ngẫu nhiên một vài dấu cách giữa các từ. |
|
|
''' |
|
|
|
|
|
|
|
|
augmentation_probability = fomula.AP_fomula(error_rate=error_rate) |
|
|
num_of_change = 0 |
|
|
|
|
|
words = text.split() |
|
|
candidate_indices = [i for i in range(len(words) - 1)] |
|
|
|
|
|
|
|
|
num_errors = int(len(text) * error_rate) |
|
|
if num_errors < 1: |
|
|
num_errors = 1 if random.random() > 0.5 else 0 |
|
|
|
|
|
sigmoid = 1 if SER <= SER_want else (1 - (1 / (1 + pow(math.e, -(SER - SER_want) * k_sigmoid)))) |
|
|
print(sigmoid) |
|
|
|
|
|
|
|
|
selected_indices = random.sample(candidate_indices, min(num_errors, len(candidate_indices))) |
|
|
for index in selected_indices: |
|
|
if random.random() <= augmentation_probability * sigmoid: |
|
|
num_of_change += 1 |
|
|
words[index] = words[index] + words[index + 1] |
|
|
words[index + 1] = '' |
|
|
|
|
|
|
|
|
print("random_space_removal: ", num_of_change) |
|
|
return ' '.join([word for word in words if word]), num_of_change |
|
|
|
|
|
|
|
|
|
|
|
import unidecode |
|
|
|
|
|
def remove_vietnamese_accents(text, SER, error_rate=0.05): |
|
|
''' |
|
|
:param text: Gồm 1 câu có dấu tiếng Việt |
|
|
:param error_rate: tỷ lệ lỗi sai muốn thêm |
|
|
:param C: hằng số để tính toán xác suất chuyển đổi ký tự |
|
|
:param num_errors: số lượng lỗi sai muốn thêm (tuỳ chọn) |
|
|
:return: Gồm 1 câu không dấu tiếng Việt với một tỷ lệ lỗi nhất định |
|
|
|
|
|
Thực hiện chuyển đổi các ký tự có dấu thành các ký tự không dấu tương ứng với xác suất nhất định. |
|
|
''' |
|
|
|
|
|
|
|
|
augmentation_probability = fomula.AP_fomula(error_rate=error_rate) |
|
|
|
|
|
|
|
|
characters = list(text) |
|
|
|
|
|
num_of_change = 0 |
|
|
num_errors = int(len(text) * error_rate) |
|
|
if num_errors < 1: |
|
|
num_errors = 1 if random.random() > 0.5 else 0 |
|
|
|
|
|
|
|
|
candidate_indices = [i for i, char in enumerate(characters) if unidecode.unidecode(char) != char] |
|
|
|
|
|
sigmoid = 1 if SER <= SER_want else (1 - (1 / (1 + pow(math.e, -(SER - SER_want) * k_sigmoid)))) |
|
|
print(sigmoid) |
|
|
|
|
|
|
|
|
selected_indices = random.sample(candidate_indices, min(num_errors, len(candidate_indices))) |
|
|
for index in selected_indices: |
|
|
if random.random() <= augmentation_probability * sigmoid: |
|
|
num_of_change += 1 |
|
|
characters[index] = unidecode.unidecode(characters[index]) |
|
|
|
|
|
print("remove_vietnamese_accents: ", num_of_change) |
|
|
|
|
|
return ''.join(characters), num_of_change |