Datasets:
Guillaume Raille
commited on
cleanup and create tiny-moliere.txt
Browse files- data/tiny-moliere.txt +0 -0
- main.py +21 -36
data/tiny-moliere.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
main.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
from pathlib import Path
|
| 2 |
|
| 3 |
import httpx
|
| 4 |
-
import pymupdf
|
| 5 |
|
| 6 |
# where to save / load data
|
| 7 |
DATA_DIR = Path.cwd() / "data"
|
|
@@ -24,7 +24,7 @@ def download_pdf(url: str, output_path: str) -> None:
|
|
| 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 |
|
|
@@ -36,7 +36,7 @@ def detect_toc(doc: pymupdf.Document) -> tuple[int, int]:
|
|
| 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
|
|
@@ -46,7 +46,7 @@ def detect_toc(doc: pymupdf.Document) -> tuple[int, int]:
|
|
| 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
|
|
@@ -75,44 +75,29 @@ def main():
|
|
| 75 |
else:
|
| 76 |
print(f"{filename} already exists, skipping download.")
|
| 77 |
|
| 78 |
-
#
|
|
|
|
|
|
|
| 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 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 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
|
| 115 |
-
print("Hello from tiny-moliere!")
|
| 116 |
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
|
|
|
| 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"
|
|
|
|
| 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 |
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 75 |
else:
|
| 76 |
print(f"{filename} already exists, skipping download.")
|
| 77 |
|
| 78 |
+
# overwrite tiny-moliere.txt if it exists
|
| 79 |
+
if (DATA_DIR / "tiny-moliere.txt").exists():
|
| 80 |
+
(DATA_DIR / "tiny-moliere.txt").unlink()
|
| 81 |
|
| 82 |
+
# process each PDF and write into a .txt file
|
| 83 |
for path in paths:
|
| 84 |
with open(path, "rb") as f:
|
| 85 |
doc = pymupdf.open(f)
|
|
|
|
| 86 |
|
| 87 |
# detect toc in first quarter of the document
|
| 88 |
+
_, toc_end = detect_toc(doc)
|
| 89 |
+
|
| 90 |
+
# compute a clip to remove headers / footers on every pages
|
| 91 |
+
rect = doc[0].bound()
|
| 92 |
+
new_rect = pymupdf.Rect(
|
| 93 |
+
rect.x0 + 60, rect.y0 + 60, rect.x1 - 60, rect.y1 - 60
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
# we ignore the last page
|
| 97 |
+
for page in doc[toc_end:-1]:
|
| 98 |
+
page_text = page.get_text(clip=new_rect)
|
| 99 |
+
with open(DATA_DIR / "tiny-moliere.txt", "a") as f:
|
| 100 |
+
f.write(page_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|