Untitled
unknown
python
a month ago
4.2 kB
2
Indexable
Never
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") return text.strip() 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") 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_string_to_num(text): c1 = replace_special_characters(text) c2 = num_to_0(c1) c3 = word_to_number(c2) c4 = text_to_0(c3) return c4 text = "Từ 12/2/2018: Người bán, tặng xe ô tô không phải thông báo với công an. Pham Quang Vinh\n" print(text) print(convert_string_to_num(text)) ######################################################################################################### import re num = '0 0 22 22 0 22 22 0 9 9 0 0 11 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 24 24' text = "Từ 12 xuyệt trái 2 xuyệt trái 2018 hai chấm Người bán phẩy tặng xe ô tô không phải thông báo với công an chấm Pham Quang Vinh xuống dòng" def replace_labels(text, replacement_dict): for key, value in replacement_dict.items(): text = text.replace(value, key) return text def word_to_str(text, dic): for pattern, replacement in dic.items(): text = re.sub(replacement, pattern, text) text = text.replace(" xuống dòng", "\n") while ' ' in text: text = text.replace(' ', ' ') return text def convert_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 elif ls1[i]!='0': i+=1 j+=1 sen = ' '.join(ls1) sen1 = re.sub(' + ', ' ', sen) sen2 = replace_labels(sen1, replacement_dict_labels) return sen2 def back_to_origin(text): txt1 = convert_to_text(num, text) txt2 = word_to_str(txt1, replacement_dict) return txt2 print(back_to_origin(text))