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

Code before:
    @staticmethod
    def _is_special_text(text):
        return text.isdigit() or text.isspace() or isLink(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 make_bilingual_book(self):
        def deal_new(p, wait_p_list):
            ret = deal_old(wait_p_list)
            new_p = copy(p)
            new_p.string = self.translate_model.translate(p.text)
            p.insert_after(new_p)
            return ret

        def deal_old(wait_p_list):
            if len(wait_p_list) == 0:
                return []

            result_txt_list = self.translate_model.translate_list(wait_p_list)

            for i in range(0, len(wait_p_list)):
                if i < len(result_txt_list):
                    p = wait_p_list[i]
                    new_p = copy(p)
                    new_p.string = result_txt_list[i]
                    p.insert_after(new_p)

            return []

        new_book = self._make_new_book(self.origin_book)
        all_items = list(self.origin_book.get_items())
        trans_taglist = self.translate_tags.split(",")
        all_p_length = sum(
            0
            if i.get_type() != ITEM_DOCUMENT
            else len(bs(i.content, "html.parser").findAll(trans_taglist))
            for i in all_items
        )
        all_p_length += self.allow_navigable_strings * sum(
            0
            if i.get_type() != ITEM_DOCUMENT
            else len(bs(i.content, "html.parser").findAll(text=True))
            for i in all_items
        )
        pbar = tqdm(total=self.test_num) if self.is_test else tqdm(total=all_p_length)
        index = 0

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

Code after:
        r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+"
    )
    return bool(url_pattern.match(text.strip()))


def is_tail_Link(text, num=100):
    text = text.strip()
    url_pattern = re.compile(
        r".*http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+$"
    )
    return bool(url_pattern.match(text)) and len(text) < num


def is_source(text):
    return text.strip().startswith("Source: ")


def is_list(text, num=80):
    text = text.strip()
    return re.match(r"^Listing\s*\d+", text) and len(text) < num


def is_figure(text, num=80):
    text = text.strip()
    return re.match(r"^Figure\s*\d+", text) and len(text) < num


class EPUBBookLoader(BaseBookLoader):
    def __init__(
        self,
        epub_name,
        model,
        key,
        resume,
        language,
        batch_size,
        model_api_base=None,
        is_test=False,
        test_num=5,
        translate_tags="p",
        allow_navigable_strings=False,
        accumulated_num=1,
        prompt_template=None,
        prompt_config=None,
    ):
        self.epub_name = epub_name
        self.new_epub = epub.EpubBook()
        self.translate_model = model(
            key,
            language,
            api_base=model_api_base,