Repository: yihong0618/bilingual_book_maker
File: book_maker/loader/epub_loader.py (Python)

Code before:
        )
        pbar = tqdm(total=self.test_num) if self.is_test else tqdm(total=all_p_length)
        index = 0
        p_to_save_len = len(self.p_to_save)
        try:
            # Add the things that don't need to be translated first, so that you can see the img after the interruption
            for item in self.origin_book.get_items():
                if item.get_type() != ITEM_DOCUMENT:
                    new_book.add_item(item)

            for item in self.origin_book.get_items_of_type(ITEM_DOCUMENT):
                # if item.file_name != "OEBPS/ch01.xhtml":
                #     continue

                soup = bs(item.content, "html.parser")
                p_list = soup.findAll(trans_taglist)
                if self.allow_navigable_strings:
                    p_list.extend(soup.findAll(text=True))

                send_num = self.accumulated_num
                if send_num > 1:
                    print("------------------------------------------------------")
                    print(f"dealing {item.file_name} ...")
                    count = 0
                    wait_p_list = []
                    for i in range(0, len(p_list)):
                        p = p_list[i]
                        temp_p = copy(p)
                        for sup in temp_p.find_all("sup"):
                            sup.extract()
                        if (
                            not p.text
                            or self._is_special_text(temp_p.text)
                            or isSourceLink(temp_p.text)
                        ):
                            continue
                        length = len(p.text)
                        if length > send_num:
                            wait_p_list = deal_new(p, wait_p_list)
                            continue
                        if i == len(p_list) - 1:
                            if count + length < send_num:
                                wait_p_list.append(p)
                                wait_p_list = deal_old(wait_p_list)
                            else:
                                wait_p_list = deal_new(p, wait_p_list)
                            break
                        if count + length < send_num:
                            count += length
                            wait_p_list.append(p)
                        else:

Review comment (suggestion):
```suggestion
                    for i in range(len(p_list)):
```

Code after:
            key,
            language,
            api_base=model_api_base,
            **prompt_config_to_kwargs(prompt_config),
        )
        self.is_test = is_test
        self.test_num = test_num
        self.translate_tags = translate_tags
        self.allow_navigable_strings = allow_navigable_strings
        self.accumulated_num = accumulated_num
        self.helper = EPUBBookLoaderHelper(self.translate_model, self.accumulated_num)

        try:
            self.origin_book = epub.read_epub(self.epub_name)
        except Exception:
            # tricky for #71 if you don't know why please check the issue and ignore this
            # when upstream change will TODO fix this
            def _load_spine(self):
                spine = self.container.find(
                    "{%s}%s" % (epub.NAMESPACES["OPF"], "spine")
                )

                self.book.spine = [
                    (t.get("idref"), t.get("linear", "yes")) for t in spine
                ]
                self.book.set_direction(spine.get("page-progression-direction", None))

            epub.EpubReader._load_spine = _load_spine
            self.origin_book = epub.read_epub(self.epub_name)

        self.p_to_save = []
        self.resume = resume
        self.bin_path = f"{Path(epub_name).parent}/.{Path(epub_name).stem}.temp.bin"
        if self.resume:
            self.load_state()

    @staticmethod
    def _is_special_text(text):
        return text.isdigit() or text.isspace() or is_link(text)

    def _make_new_book(self, book):
        new_book = epub.EpubBook()
        new_book.metadata = book.metadata
        new_book.spine = book.spine
        new_book.toc = book.toc
        return new_book

    def _process_paragraph(self, p, index, p_to_save_len):
        if not p.text or self._is_special_text(p.text):
            return index