Datasets:
Guillaume Raille
commited on
add a function to detetct the toc
Browse files
main.py
CHANGED
|
@@ -1,9 +1,12 @@
|
|
| 1 |
from pathlib import Path
|
| 2 |
|
| 3 |
import httpx
|
|
|
|
| 4 |
|
|
|
|
| 5 |
DATA_DIR = Path.cwd() / "data"
|
| 6 |
|
|
|
|
| 7 |
RAW_PDF_URLS = [
|
| 8 |
"https://www.ebooksgratuits.com/ebooksfrance/moliere-oeuvres_completes_1.pdf",
|
| 9 |
"https://www.ebooksgratuits.com/ebooksfrance/moliere-oeuvres_completes_2.pdf",
|
|
@@ -19,14 +22,53 @@ def download_pdf(url: str, output_path: str) -> None:
|
|
| 19 |
f.write(chunk)
|
| 20 |
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def main():
|
| 23 |
# make sure data directory exists
|
| 24 |
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
| 25 |
|
| 26 |
# download raw PDFs if they don't already exist
|
|
|
|
| 27 |
for url in RAW_PDF_URLS:
|
| 28 |
filename = url.split("/")[-1]
|
| 29 |
output_path = DATA_DIR / filename
|
|
|
|
| 30 |
if not output_path.exists():
|
| 31 |
print(f"Downloading {filename}...")
|
| 32 |
download_pdf(url, output_path)
|
|
@@ -34,6 +76,39 @@ def main():
|
|
| 34 |
print(f"{filename} already exists, skipping download.")
|
| 35 |
|
| 36 |
# fetch from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
# 2. process data into clean tiny-shakespeare like format
|
| 38 |
|
| 39 |
# 3. save processed data to disk
|
|
|
|
| 1 |
from pathlib import Path
|
| 2 |
|
| 3 |
import httpx
|
| 4 |
+
import pymupdf # type: ignore
|
| 5 |
|
| 6 |
+
# where to save / load data
|
| 7 |
DATA_DIR = Path.cwd() / "data"
|
| 8 |
|
| 9 |
+
# URLs of the raw PDFs to download
|
| 10 |
RAW_PDF_URLS = [
|
| 11 |
"https://www.ebooksgratuits.com/ebooksfrance/moliere-oeuvres_completes_1.pdf",
|
| 12 |
"https://www.ebooksgratuits.com/ebooksfrance/moliere-oeuvres_completes_2.pdf",
|
|
|
|
| 22 |
f.write(chunk)
|
| 23 |
|
| 24 |
|
| 25 |
+
def detect_toc(doc: pymupdf.Document) -> tuple[int, int]:
|
| 26 |
+
"""Detect the table of contents in the first quarter of the document.
|
| 27 |
+
|
| 28 |
+
Args:
|
| 29 |
+
doc: A pymupdf Document object representing the PDF document.
|
| 30 |
+
|
| 31 |
+
Returns:
|
| 32 |
+
A tuple containing the start and end page indices of the TOC.
|
| 33 |
+
If no TOC is found, returns (-1, -1).
|
| 34 |
+
"""
|
| 35 |
+
start = -1
|
| 36 |
+
end = -1
|
| 37 |
+
i = 0
|
| 38 |
+
|
| 39 |
+
for page in doc[:len(doc) // 4]:
|
| 40 |
+
if page.get_links():
|
| 41 |
+
start = i
|
| 42 |
+
break
|
| 43 |
+
i += 1
|
| 44 |
+
|
| 45 |
+
if start == -1:
|
| 46 |
+
print("No table of contents found in the first quarter of the document.")
|
| 47 |
+
return -1, -1 # no TOC found
|
| 48 |
+
|
| 49 |
+
for page in doc[start:len(doc) // 4]:
|
| 50 |
+
if not page.get_links():
|
| 51 |
+
end = i
|
| 52 |
+
break
|
| 53 |
+
i += 1
|
| 54 |
+
|
| 55 |
+
if end == -1:
|
| 56 |
+
end = len(doc) // 4
|
| 57 |
+
|
| 58 |
+
print(f"Table of contents detected from page {start} to {end}.")
|
| 59 |
+
return start, end
|
| 60 |
+
|
| 61 |
+
|
| 62 |
def main():
|
| 63 |
# make sure data directory exists
|
| 64 |
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
| 65 |
|
| 66 |
# download raw PDFs if they don't already exist
|
| 67 |
+
paths = []
|
| 68 |
for url in RAW_PDF_URLS:
|
| 69 |
filename = url.split("/")[-1]
|
| 70 |
output_path = DATA_DIR / filename
|
| 71 |
+
paths.append(output_path)
|
| 72 |
if not output_path.exists():
|
| 73 |
print(f"Downloading {filename}...")
|
| 74 |
download_pdf(url, output_path)
|
|
|
|
| 76 |
print(f"{filename} already exists, skipping download.")
|
| 77 |
|
| 78 |
# fetch from
|
| 79 |
+
|
| 80 |
+
for path in paths:
|
| 81 |
+
with open(path, "rb") as f:
|
| 82 |
+
doc = pymupdf.open(f)
|
| 83 |
+
# process the document as needed
|
| 84 |
+
|
| 85 |
+
# detect toc in first quarter of the document
|
| 86 |
+
toc_start, toc_end = detect_toc(doc)
|
| 87 |
+
breakpoint()
|
| 88 |
+
|
| 89 |
+
# read toc
|
| 90 |
+
|
| 91 |
+
# extract info from toc
|
| 92 |
+
data = {
|
| 93 |
+
"title": None,
|
| 94 |
+
"actors": None,
|
| 95 |
+
"acts": [
|
| 96 |
+
{
|
| 97 |
+
"start_page": 0,
|
| 98 |
+
"end_page": 0,
|
| 99 |
+
"title": None,
|
| 100 |
+
"scenes": [
|
| 101 |
+
{
|
| 102 |
+
"start_page": 0,
|
| 103 |
+
"end_page": 0,
|
| 104 |
+
"title": None,
|
| 105 |
+
}
|
| 106 |
+
]
|
| 107 |
+
}
|
| 108 |
+
]
|
| 109 |
+
}
|
| 110 |
+
breakpoint()
|
| 111 |
+
# 1. download raw PDFs from URLs
|
| 112 |
# 2. process data into clean tiny-shakespeare like format
|
| 113 |
|
| 114 |
# 3. save processed data to disk
|