From 363e2ebca3bd49004d7880f3f45398125fd9323d Mon Sep 17 00:00:00 2001 From: Klaudia Date: Tue, 18 Apr 2023 17:44:37 +0200 Subject: [PATCH] fix --- utils/help.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 utils/help.py diff --git a/utils/help.py b/utils/help.py new file mode 100644 index 0000000..f85555d --- /dev/null +++ b/utils/help.py @@ -0,0 +1,58 @@ +def process_text(text, nazwisko_user, nazwisko_system): + lines = text.split('\n' ) + result=[] + for line in lines : + if nazwisko_system in line: + result.append("system " + line.split(nazwisko_system, 1)[1].strip()) + elif nazwisko_user in line: + result.append("user " + line.split(nazwisko_user, 1)[1].strip()) + else: + result.append(line) + return '\n'.join(result) +def join_lines(text ): + lines = text.split("\n" ) + joined_lines = [lines[0] ] + for line in lines[1:] : + if line.startswith("[") : + joined_lines.append(line ) + else : + joined_lines[-1] += " " + line + return "\n".join(joined_lines ) +def remove_empty_lines(text): + lines = text.split('\n' ) + non_empty_lines=[] + for line in lines : + if line.strip(): + non_empty_lines.append(line) + return '\n'.join(non_empty_lines) +def read_tsv(file_path ): + with open(file_path, 'r', encoding='utf-8') as tsv_file : + content =tsv_file.read() + return content +def write_tsv(file_path, content ): + with open(file_path, 'w', encoding='utf-8') as tsv_file : + tsv_file.write(content ) +def replace_first_space_with_tab(text) : + lines = text.split("\n" ) + new_lines =[] + for line in lines : + first_space_index = line.find(" " ) + if first_space_index != -1 : + new_line = line[:first_space_index] + "\t" + line[first_space_index + 1: ] + new_lines.append(new_line ) + else : + new_lines.append(line ) + return "\n".join(new_lines ) +if __name__ == '__main__' : + # wypelnic przed uruchomieniemm + nazwisko_user ="" + nazwisko_system= "" + numer_user ="" + numer_system= "" + file_path =f"J:\PycharmProjects\systemy_dialogowe\data\dialog-{numer_system}-{numer_user}-01.tsv" + text= read_tsv(file_path ) + text= remove_empty_lines(text ) + text= join_lines(text ) + text= process_text(text, nazwisko_user, nazwisko_system ) + text= replace_first_space_with_tab(text ) + write_tsv(file_path, text )