Repository: yihong0618/bilingual_book_maker
File: book_maker/translator/chatgptapi_translator.py (Python)

Code before:
                )
                or (
                    len(result_list) < len(best_result_list)
                    and len(best_result_list) > plist_len
                )
            ):
                best_result_list = result_list
            retry_count += 1
            end_time = time.time()

        result_list = best_result_list

        state = "fail" if len(result_list) != plist_len else "success"

        if retry_count > 0:
            print(f"retry {state}")
            with open("buglog.txt", "a") as f:
                print(
                    f"retry {state}, count = {retry_count}, time = {(end_time-start_time):.1f}s",
                    file=f,
                )

        if len(result_list) != plist_len:
            # todo: select best
            newlist = new_str.split(sep)
            with open("buglog.txt", "a") as f:
                print(f"problem size: {plist_len - len(result_list)}", file=f)
                for i in range(len(newlist)):
                    print(newlist[i], file=f)
                    print(file=f)
                    if i < len(result_list):
                        print(result_list[i], file=f)
                        print(file=f)
                    print("=============================", file=f)
            print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")

            print(
                f"bug: {plist_len} paragraphs of text translated into {len(result_list)} paragraphs"
            )
            print("continue")

        return result_list


Review comment (suggestion):
 Create a `log` directory and add this directory to .gitignore.I think that would make the structure clearer:D

Code after:

    def log_translation_mismatch(
        self, plist_len, result_list, new_str, sep, log_path="log/buglog.txt"
    ):
        if len(result_list) == plist_len:
            return
        newlist = new_str.split(sep)
        with open(log_path, "a") as f:
            print(f"problem size: {plist_len - len(result_list)}", file=f)
            for i in range(len(newlist)):
                print(newlist[i], file=f)
                print(file=f)
                if i < len(result_list):
                    print(result_list[i], file=f)
                    print(file=f)
                print("=============================", file=f)
        print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")

        print(
            f"bug: {plist_len} paragraphs of text translated into {len(result_list)} paragraphs"
        )
        print("continue")

    def translate_list(self, plist):
        sep = "\n\n\n\n\n"
        # new_str = sep.join([item.text for item in plist])

        new_str = ""
        i = 1
        for p in plist:
            temp_p = copy(p)
            for sup in temp_p.find_all("sup"):
                sup.extract()
            new_str += f"({i}) " + temp_p.get_text().strip() + sep
            i = i + 1

        if new_str.endswith(sep):
            new_str = new_str[: -len(sep)]

        plist_len = len(plist)

        print(f"plist len = {len(plist)}")

        result_list = self.translate_and_split_lines(new_str)

        start_time = time.time()

        result_list, retry_count = self.get_best_result_list(
            plist_len, new_str, 6, result_list
        )