From 99bcaddff6e0082785a0f469fded051a15fea9ea Mon Sep 17 00:00:00 2001 From: h Date: Sat, 18 Mar 2023 15:07:27 +0800 Subject: [PATCH] improve mult line in --accumulated_num --- .../translator/chatgptapi_translator.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/book_maker/translator/chatgptapi_translator.py b/book_maker/translator/chatgptapi_translator.py index f7f03a2..9f9678d 100644 --- a/book_maker/translator/chatgptapi_translator.py +++ b/book_maker/translator/chatgptapi_translator.py @@ -198,6 +198,7 @@ The total token is too long and cannot be completely translated\n print(newlist[i], file=f) print(file=f) if i < len(result_list): + print("............................................", file=f) print(result_list[i], file=f) print(file=f) print("=============================", file=f) @@ -208,6 +209,34 @@ The total token is too long and cannot be completely translated\n ) print("continue") + def join_lines(self, text): + lines = text.split("\n") + new_lines = [] + temp_line = [] + + # join + for line in lines: + if line.strip(): + temp_line.append(line.strip()) + else: + if temp_line: + new_lines.append(" ".join(temp_line)) + temp_line = [] + new_lines.append(line) + + if temp_line: + new_lines.append(" ".join(temp_line)) + + text = "\n".join(new_lines) + + # del ^M + text = text.replace("^M", "\r") + lines = text.split("\n") + filtered_lines = [line for line in lines if line.strip() != "\r"] + new_text = "\n".join(filtered_lines) + + return new_text + def translate_list(self, plist): sep = "\n\n\n\n\n" # new_str = sep.join([item.text for item in plist]) @@ -224,6 +253,8 @@ The total token is too long and cannot be completely translated\n if new_str.endswith(sep): new_str = new_str[: -len(sep)] + new_str = self.join_lines(new_str) + plist_len = len(plist) print(f"plist len = {len(plist)}")