Untitled
unknown
plain_text
2 years ago
4.3 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")
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,c1,c2,c3
text = "Quyền lực của ông trùm xã hội đen (Kỳ 3): Bi kịch gia đình."
print(text)
c4,c1,c2,c3 = convert_string_to_num(text)
print(c4)
print(delete_space(c1))
#########################################################################################################
print('#####################33')
import re
num = '0 0 0 0 0 0 0 0 1 1 1 0 0 2 2 2 9 9 0 0 0 0 20'
text = "Quyền lực của ông trùm xã hội đen mở ngoặc đơn Kỳ 3 đóng ngoặc đơn hai chấm Bi kịch gia đình chấm xuống dòng"
def replace_labels(text, replacement_dict):
for key, value in replacement_dict.items():
text = text.replace(value, key)
text = text.replace('100 1 1', 'chấm xuống dòng')
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")
text = text.replace(" chấm 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
txt1 = convert_to_text(num, text)
print(txt1)
txt2 = word_to_str(txt1, replacement_dict)
print(txt2)Editor is loading...