Untitled
unknown
plain_text
a year ago
4.2 kB
4
Indexable
import re replacement_dict_labels = { " mở ngoặc đơn ": " 1 1 1 ", " đóng ngoặc đơn ": " 2 2 2 ", " mở ngoặc nhọn ": " 3 3 3 ", " đóng ngoặc nhọn ": " 4 4 4 ", " mở ngoặc vuông ": " 5 5 5 ", " đóng ngoặc vuông ": " 6 6 6 ", " gạch ngang trên ": " 7 7 7 ", " gạch ngang dưới ": " 8 8 8 ", " hai chấm ": " 9 9 ", " chấm phẩy ": " 10 10 ", " phẩy ": " 11 ", " lớn hơn ": " 12 12 ", " bé hơn ": " 13 13 ", " chấm hỏi": " 14 14", " chấm than ": " 15 15 ", " a còng ": " 16 16 ", " dấu thăng ": " 17 17 ", " phần trăm ": " 18 18 ", " ba chấm ": " 19 19 ", " chấm": " 20", " bằng ": " 21 ", " xuyệt trái ": " 22 22 ", " xuyệt phải ": " 23 23 ", " xuống dòng ": " 24 24 " } replacement_dict = { r"(": " mở ngoặc đơn ", r")": " đóng ngoặc đơn", r"{": " mở ngoặc nhọn ", r"}": " đóng ngoặc nhọn ", r"[": " mở ngoặc vuông ", r"]": " đóng ngoặc vuông ", r"-": " gạch ngang trên ", r"_": " gạch ngang dưới ", r":": " hai chấm ", r";": " chấm phẩy ", r",": " phẩy ", r">": " lớn hơn ", r"<": " bé hơn ", r"?": " chấm hỏi ", r"!": " chấm than ", r"@": " a còng ", r"#": " dấu thăng ", r"%": " phần trăm ", r"...": " ba chấm ", r".": " chấm", r"=": " bằng ", r"/": " xuyệt trái ", r"\\": " xuyệt phải " } def replace_special_characters(text): for chu, dau in replacement_dict.items(): text = text.replace(chu, dau) text = text.replace(".\n", " chấm xuống dòng") text = text.replace("\n", " xuống dòng") txt = delete_space(text) return txt def word_to_number(text): for chu, so in replacement_dict_labels.items(): text = text.replace(chu, so) text = text.replace(" chấm xuống dòng ", " 100 1 1 ") text = text.replace(" chấm", " 20") return text.strip() def delete_space(text): text_del_sp = text.strip() text_del_sp_inside = re.sub(' +', ' ', text_del_sp) return text_del_sp_inside def lower_string(text): return text.lower() def text_to_0(sen): ls = sen.split() for i in range(len(ls)): if ls[i].isnumeric() is False: ls[i] = '0 ' i+=1 sen = ' '.join(ls) new_sen = re.sub(' +', ' ', sen) return new_sen def num_to_0(text): ls = text.split() for i in range(len(ls)): if ls[i].isnumeric() == True: ls[i] = '0 ' i+=1 sen = ' '.join(ls) new_sen = re.sub(' +', ' ', sen) return new_sen def convert_text_to_num(text): txt1 = replace_special_characters(text) print(txt1) txt2 = lower_string(txt1) print(txt2) txt3 = num_to_0(txt2) print(txt3) txt4 = word_to_number(txt3) print(txt4) txt5 = text_to_0(txt4) print(txt5) return '------------------------------------------------------------\n' text ='Lệ Quyên nói gì về tin đồn kiếm khoảng 68 tỷ đồng một năm.' print(convert_text_to_num(text)) import re def replace_lables_to_work(text): for chu, so in replacement_dict_labels.items(): text = text.replace(so, chu) text = text.replace(" 20", " chấm") txt = delete_space(text) return txt def form_0_to_text(num, text): ls1 = num.split() ls2 = text.split() i = 0 j = 0 for i in range(len(ls1)): if ls1[i] == '0': ls1[i] = ls2[j] i+=1 j+=1 else: i+=1 j+=1 sentence = ' '.join(ls1) return sentence def back_to_origin(text): for chu, dau in replacement_dict.items(): text = text.replace(dau, chu) #text = text.replace(" chấm", ".") return text num = '0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 7 7 7 0 0 2 2 2 20' text = "Vận hành thử nghiệm Trạm thu giá Quốc lộ 18 mở ngoặc đơn đoạn Hạ Long gạch ngang trên Mông Dương đóng ngoặc đơn chấm" t = replace_lables_to_work(num) print(t) t1 = form_0_to_text(t, text) print(t1) t2 = back_to_origin(t1) print(t2)
Editor is loading...
Leave a Comment