-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6abe72
commit 3524442
Showing
4 changed files
with
229 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import json | ||
import os | ||
import shutil | ||
import tempfile | ||
import zipfile | ||
from collections import OrderedDict | ||
from typing import Tuple | ||
|
||
import wget | ||
|
||
from dedoc.attachments_extractors import AbstractAttachmentsExtractor, PDFAttachmentsExtractor | ||
from dedoc.config import get_config | ||
from dedoc.data_structures import AttachedFile | ||
from dedoc.readers import BaseReader, PdfTabbyReader, PdfTxtlayerReader | ||
|
||
|
||
def get_reader_attachments(reader: BaseReader, input_dir: str, attachments_dir: str) -> dict: | ||
os.makedirs(attachments_dir) | ||
result_dict = OrderedDict() | ||
|
||
for file_name in sorted(os.listdir(input_dir)): | ||
if not file_name.endswith("pdf") or file_name == "large.pdf": | ||
continue | ||
|
||
attachment_names = [] | ||
with tempfile.TemporaryDirectory() as tmp_dir: | ||
file_path = os.path.join(tmp_dir, file_name) | ||
shutil.copy(os.path.join(input_dir, file_name), file_path) | ||
document = reader.read(file_path, parameters={"with_attachments": "true"}) | ||
os.remove(file_path) | ||
|
||
file_attachments_dir = os.path.join(attachments_dir, file_name.replace(".", "_")) | ||
os.makedirs(file_attachments_dir) | ||
|
||
png_files, json_files = 0, 0 | ||
for attachment in document.attachments: | ||
if os.path.isfile(attachment.tmp_file_path): | ||
attachment_name, png_files, json_files = _get_attachment_name(attachment, png_files, json_files) | ||
shutil.copy(attachment.tmp_file_path, os.path.join(file_attachments_dir, attachment_name)) | ||
attachment_names.append(attachment_name) | ||
|
||
print(f"{file_name}: {len(attachment_names)} attachments, {len(document.attachments)} in result") | ||
result_dict[file_name] = sorted(attachment_names) | ||
|
||
return result_dict | ||
|
||
|
||
def get_attachments(attachments_extractor: AbstractAttachmentsExtractor, input_dir: str, attachments_dir: str) -> dict: | ||
os.makedirs(attachments_dir) | ||
result_dict = OrderedDict() | ||
|
||
for file_name in sorted(os.listdir(input_dir)): | ||
if not file_name.endswith("pdf"): | ||
continue | ||
|
||
attachment_names = [] | ||
with tempfile.TemporaryDirectory() as tmp_dir: | ||
file_path = os.path.join(tmp_dir, file_name) | ||
shutil.copy(os.path.join(input_dir, file_name), file_path) | ||
attachments = attachments_extractor.get_attachments(tmpdir=tmp_dir, filename=file_name, parameters={}) | ||
os.remove(file_path) | ||
|
||
file_attachments_dir = os.path.join(attachments_dir, file_name.replace(".", "_")) | ||
os.makedirs(file_attachments_dir) | ||
|
||
png_files, json_files = 0, 0 | ||
for attachment in attachments: | ||
if os.path.isfile(attachment.tmp_file_path): | ||
attachment_name, png_files, json_files = _get_attachment_name(attachment, png_files, json_files) | ||
shutil.copy(attachment.tmp_file_path, os.path.join(file_attachments_dir, attachment_name)) | ||
attachment_names.append(attachment_name) | ||
|
||
print(f"{file_name}: {len(attachment_names)} attachments, {len(attachments)} in result") | ||
result_dict[file_name] = sorted(attachment_names) | ||
|
||
return result_dict | ||
|
||
|
||
def _get_attachment_name(attachment: AttachedFile, png_files: int, json_files: int) -> Tuple[str, int, int]: | ||
attachment_name = attachment.original_name | ||
if attachment_name.endswith(".png"): | ||
png_files += 1 | ||
attachment_name = f"{png_files}.png" | ||
if attachment_name.endswith(".json"): | ||
json_files += 1 | ||
attachment_name = f"{json_files}.json" | ||
return attachment_name, png_files, json_files | ||
|
||
|
||
if __name__ == "__main__": | ||
data_url = "https://at.ispras.ru/owncloud/index.php/s/EoczXGwWzai8ztN/download" | ||
data_dir = os.path.join(get_config()["intermediate_data_path"], "benchmark_pdf_attachments") | ||
|
||
if not os.path.isdir(data_dir): | ||
os.makedirs(data_dir) | ||
archive_path = os.path.join(data_dir, "with_attachments.zip") | ||
wget.download(data_url, archive_path) | ||
with zipfile.ZipFile(archive_path, "r") as zip_ref: | ||
zip_ref.extractall(data_dir) | ||
os.remove(archive_path) | ||
|
||
print(f"Benchmark data downloaded to {data_dir}") | ||
else: | ||
print(f"Use cached benchmark data from {data_dir}") | ||
|
||
in_dir = os.path.join(data_dir, "with_attachments") | ||
out_dir = os.path.join(in_dir, "extracted_attachments") | ||
|
||
if os.path.exists(out_dir): | ||
shutil.rmtree(out_dir) | ||
os.makedirs(out_dir) | ||
|
||
benchmarks_dict = {} | ||
|
||
print("Get tabby attachments") | ||
tabby_reader = PdfTabbyReader(config={}) | ||
tabby_out_dir = os.path.join(out_dir, "tabby") | ||
benchmarks_dict["tabby"] = get_reader_attachments(reader=tabby_reader, input_dir=in_dir, attachments_dir=tabby_out_dir) | ||
|
||
print("Get pdfminer attachments") | ||
pdfminer_reader = PdfTxtlayerReader(config={}) | ||
pdfminer_out_dir = os.path.join(out_dir, "pdfminer") | ||
benchmarks_dict["pdfminer"] = get_reader_attachments(reader=pdfminer_reader, input_dir=in_dir, attachments_dir=pdfminer_out_dir) | ||
|
||
print("Get common attachments") | ||
common_out_dir = os.path.join(out_dir, "common") | ||
pdf_attachments_extractor = PDFAttachmentsExtractor(config={}) | ||
benchmarks_dict["common"] = get_attachments(attachments_extractor=pdf_attachments_extractor, input_dir=in_dir, attachments_dir=common_out_dir) | ||
|
||
json_out_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "resources", "benchmarks")) | ||
with open(os.path.join(json_out_dir, "benchmark_pdf_attachments.json"), "w") as f: | ||
json.dump(benchmarks_dict, f, ensure_ascii=False, indent=2) | ||
|
||
print(f"Attachments were extracted to {out_dir}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
{ | ||
"tabby": { | ||
"Document635.pdf": [ | ||
"1.json", | ||
"2.json" | ||
], | ||
"example_with_attachments_depth_1.pdf": [ | ||
"1.json", | ||
"attachment.txt", | ||
"example_with_table4.jpg", | ||
"header_test.pdf", | ||
"header_test.pdf" | ||
], | ||
"example_with_images.xlsx.pdf": [], | ||
"with_attachments_0.docx.pdf": [], | ||
"with_attachments_1.docx.pdf": [], | ||
"with_attachments_1.pptx.pdf": [], | ||
"with_attachments_2.docx.pdf": [], | ||
"with_attachments_2.pptx.pdf": [], | ||
"with_attachments_3.pdf": [] | ||
}, | ||
"pdfminer": { | ||
"Document635.pdf": [ | ||
"1.json", | ||
"1.png", | ||
"2.json", | ||
"2.png" | ||
], | ||
"example_with_attachments_depth_1.pdf": [ | ||
"1.json", | ||
"attachment.txt", | ||
"example_with_table4.jpg", | ||
"header_test.pdf", | ||
"header_test.pdf" | ||
], | ||
"example_with_images.xlsx.pdf": [ | ||
"1.png", | ||
"2.png" | ||
], | ||
"with_attachments_0.docx.pdf": [ | ||
"1.png", | ||
"2.png", | ||
"3.png", | ||
"4.png" | ||
], | ||
"with_attachments_1.docx.pdf": [ | ||
"1.png", | ||
"2.png", | ||
"3.png" | ||
], | ||
"with_attachments_1.pptx.pdf": [ | ||
"1.png", | ||
"2.png", | ||
"3.png" | ||
], | ||
"with_attachments_2.docx.pdf": [ | ||
"1.png", | ||
"2.png" | ||
], | ||
"with_attachments_2.pptx.pdf": [], | ||
"with_attachments_3.pdf": [ | ||
"1.png", | ||
"2.png", | ||
"3.png", | ||
"4.png", | ||
"5.png", | ||
"6.png", | ||
"7.png" | ||
] | ||
}, | ||
"common": { | ||
"Document635.pdf": [ | ||
"1.json", | ||
"2.json" | ||
], | ||
"example_with_attachments_depth_1.pdf": [ | ||
"1.json", | ||
"attachment.txt", | ||
"example_with_table4.jpg", | ||
"header_test.pdf", | ||
"header_test.pdf" | ||
], | ||
"example_with_images.xlsx.pdf": [], | ||
"large.pdf": [], | ||
"with_attachments_0.docx.pdf": [], | ||
"with_attachments_1.docx.pdf": [], | ||
"with_attachments_1.pptx.pdf": [], | ||
"with_attachments_2.docx.pdf": [], | ||
"with_attachments_2.pptx.pdf": [], | ||
"with_attachments_3.pdf": [] | ||
} | ||
} |