This commit is contained in:
Klaudia 2023-04-18 17:42:57 +02:00
parent 11fb2733e5
commit b69d52b5d7

View File

@ -1,7 +1,7 @@
def process_text(text, nazwisko_user, nazwisko_system): def process_text(text, nazwisko_user, nazwisko_system):
lines = text.split('\n') lines = text.split('\n' )
result=[] result=[]
for line in lines: for line in lines :
if nazwisko_system in line: if nazwisko_system in line:
result.append("system " + line.split(nazwisko_system, 1)[1].strip()) result.append("system " + line.split(nazwisko_system, 1)[1].strip())
elif nazwisko_user in line: elif nazwisko_user in line:
@ -9,55 +9,50 @@ def process_text(text, nazwisko_user, nazwisko_system):
else: else:
result.append(line) result.append(line)
return '\n'.join(result) return '\n'.join(result)
def join_lines(text): def join_lines(text ):
lines = text.split("\n") lines = text.split("\n" )
joined_lines = [lines[0]] joined_lines = [lines[0] ]
for line in lines[1:] :
for line in lines[1:]: if line.startswith("[") :
if line.startswith("["): joined_lines.append(line )
joined_lines.append(line) else :
else: joined_lines[-1] += " " + line
joined_lines[-1] += " " + line return "\n".join(joined_lines )
return "\n".join(joined_lines)
def remove_empty_lines(text): def remove_empty_lines(text):
lines = text.split('\n') lines = text.split('\n' )
non_empty_lines = [] non_empty_lines=[]
for line in lines: for line in lines :
if line.strip(): if line.strip():
non_empty_lines.append(line) non_empty_lines.append(line)
return '\n'.join(non_empty_lines) return '\n'.join(non_empty_lines)
def read_tsv(file_path ):
def read_tsv(file_path): with open(file_path, 'r', encoding='utf-8') as tsv_file :
with open(file_path, 'r', encoding='utf-8') as tsv_file: content =tsv_file.read()
content = tsv_file.read() return content
return content def write_tsv(file_path, content ):
with open(file_path, 'w', encoding='utf-8') as tsv_file :
def write_tsv(file_path, content): tsv_file.write(content )
with open(file_path, 'w', encoding='utf-8') as tsv_file: def replace_first_space_with_tab(text) :
tsv_file.write(content) lines = text.split("\n" )
def replace_first_space_with_tab(text): new_lines =[]
lines = text.split("\n") for line in lines :
new_lines = [] first_space_index = line.find(" " )
for line in lines: if first_space_index != -1 :
first_space_index = line.find(" ") new_line = line[:first_space_index] + "\t" + line[first_space_index + 1: ]
if first_space_index != -1: new_lines.append(new_line )
new_line = line[:first_space_index] + "\t" + line[first_space_index + 1:] else :
new_lines.append(new_line) new_lines.append(line )
else: return "\n".join(new_lines )
new_lines.append(line) if __name__ == '__main__' :
return "\n".join(new_lines) # wypelnic przed uruchomieniemm
if __name__ == '__main__': nazwisko_user =""
# wypelnic przed uruchomieniem nazwisko_system= ""
nazwisko_user = "" numer_user =""
nazwisko_system = "" numer_system= ""
numer_user = "" file_path =f"J:\PycharmProjects\systemy_dialogowe\data\dialog-{numer_system}-{numer_user}-01.tsv"
numer_system = "" text= read_tsv(file_path )
file_path = f"J:\PycharmProjects\systemy_dialogowe\data\dialog-{numer_system}-{numer_user}-01.tsv" text= remove_empty_lines(text )
text = read_tsv(file_path ) text= join_lines(text )
text = remove_empty_lines(text ) text= process_text(text, nazwisko_user, nazwisko_system )
text = join_lines(text ) text= replace_first_space_with_tab(text )
text = process_text(text, nazwisko_user, nazwisko_system ) write_tsv(file_path, text )
text = replace_first_space_with_tab(text )
write_tsv(file_path, text )