llama-index

Форк
0
43 строки · 1.2 Кб
1
"""Epub parser.
2

3
Contains parsers for epub files.
4
"""
5

6
from pathlib import Path
7
from typing import Dict, List, Optional
8

9
from llama_index.legacy.readers.base import BaseReader
10
from llama_index.legacy.schema import Document
11

12

13
class EpubReader(BaseReader):
14
    """Epub Parser."""
15

16
    def load_data(
17
        self, file: Path, extra_info: Optional[Dict] = None
18
    ) -> List[Document]:
19
        """Parse file."""
20
        try:
21
            import ebooklib
22
            import html2text
23
            from ebooklib import epub
24
        except ImportError:
25
            raise ImportError(
26
                "Please install extra dependencies that are required for "
27
                "the EpubReader: "
28
                "`pip install EbookLib html2text`"
29
            )
30

31
        text_list = []
32
        book = epub.read_epub(file, options={"ignore_ncx": True})
33

34
        # Iterate through all chapters.
35
        for item in book.get_items():
36
            # Chapters are typically located in epub documents items.
37
            if item.get_type() == ebooklib.ITEM_DOCUMENT:
38
                text_list.append(
39
                    html2text.html2text(item.get_content().decode("utf-8"))
40
                )
41

42
        text = "\n".join(text_list)
43
        return [Document(text=text, metadata=extra_info or {})]
44

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.