2023-08-31 19:55:17 +00:00
|
|
|
import os
|
2024-06-22 10:30:06 +00:00
|
|
|
import re
|
2023-06-01 11:27:38 +00:00
|
|
|
|
2024-10-21 05:25:41 +00:00
|
|
|
import pytest
|
|
|
|
|
2023-11-22 06:11:32 +00:00
|
|
|
from khoj.processor.content.pdf.pdf_to_entries import PdfToEntries
|
2023-08-31 19:55:17 +00:00
|
|
|
from khoj.utils.fs_syncer import get_pdf_files
|
|
|
|
from khoj.utils.rawconfig import TextContentConfig
|
|
|
|
|
2023-06-01 11:27:38 +00:00
|
|
|
|
|
|
|
def test_single_page_pdf_to_jsonl():
|
|
|
|
"Convert single page PDF file to jsonl."
|
|
|
|
# Act
|
|
|
|
# Extract Entries from specified Pdf files
|
2023-08-31 19:55:17 +00:00
|
|
|
# Read singlepage.pdf into memory as bytes
|
|
|
|
with open("tests/data/pdf/singlepage.pdf", "rb") as f:
|
2023-10-17 09:41:16 +00:00
|
|
|
pdf_bytes = f.read()
|
2023-08-31 19:55:17 +00:00
|
|
|
|
|
|
|
data = {"tests/data/pdf/singlepage.pdf": pdf_bytes}
|
2024-02-09 10:34:41 +00:00
|
|
|
entries = PdfToEntries.extract_pdf_entries(pdf_files=data)
|
2023-06-01 11:27:38 +00:00
|
|
|
|
|
|
|
# Assert
|
2024-06-18 14:01:07 +00:00
|
|
|
assert len(entries) == 2
|
|
|
|
assert len(entries[1]) == 1
|
2023-06-01 11:27:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_multi_page_pdf_to_jsonl():
|
|
|
|
"Convert multiple pages from single PDF file to jsonl."
|
|
|
|
# Act
|
|
|
|
# Extract Entries from specified Pdf files
|
2023-08-31 19:55:17 +00:00
|
|
|
with open("tests/data/pdf/multipage.pdf", "rb") as f:
|
2023-10-17 09:41:16 +00:00
|
|
|
pdf_bytes = f.read()
|
2023-08-31 19:55:17 +00:00
|
|
|
|
|
|
|
data = {"tests/data/pdf/multipage.pdf": pdf_bytes}
|
2024-02-09 10:34:41 +00:00
|
|
|
entries = PdfToEntries.extract_pdf_entries(pdf_files=data)
|
2023-06-01 11:27:38 +00:00
|
|
|
|
|
|
|
# Assert
|
2024-06-18 14:01:07 +00:00
|
|
|
assert len(entries) == 2
|
|
|
|
assert len(entries[1]) == 6
|
2023-06-01 11:27:38 +00:00
|
|
|
|
|
|
|
|
2024-10-21 05:25:41 +00:00
|
|
|
@pytest.mark.skip(reason="Temporarily disabled OCR due to performance issues")
|
2023-11-05 23:09:40 +00:00
|
|
|
def test_ocr_page_pdf_to_jsonl():
|
|
|
|
"Convert multiple pages from single PDF file to jsonl."
|
2024-06-22 10:30:06 +00:00
|
|
|
# Arrange
|
|
|
|
expected_str = "playing on a strip of marsh"
|
|
|
|
expected_str_with_variable_spaces = re.compile(expected_str.replace(" ", r"\s*"), re.IGNORECASE)
|
|
|
|
|
2023-11-05 23:09:40 +00:00
|
|
|
# Extract Entries from specified Pdf files
|
|
|
|
with open("tests/data/pdf/ocr_samples.pdf", "rb") as f:
|
|
|
|
pdf_bytes = f.read()
|
|
|
|
data = {"tests/data/pdf/ocr_samples.pdf": pdf_bytes}
|
2024-06-22 10:30:06 +00:00
|
|
|
|
|
|
|
# Act
|
2024-02-09 10:34:41 +00:00
|
|
|
entries = PdfToEntries.extract_pdf_entries(pdf_files=data)
|
2024-06-22 10:30:06 +00:00
|
|
|
raw_entry = entries[1][0].raw
|
|
|
|
|
|
|
|
# Assert
|
2024-06-18 14:01:07 +00:00
|
|
|
assert len(entries) == 2
|
|
|
|
assert len(entries[1]) == 1
|
2024-06-22 10:30:06 +00:00
|
|
|
assert re.search(expected_str_with_variable_spaces, raw_entry) is not None
|
2023-11-05 23:09:40 +00:00
|
|
|
|
|
|
|
|
2023-06-01 11:27:38 +00:00
|
|
|
def test_get_pdf_files(tmp_path):
|
|
|
|
"Ensure Pdf files specified via input-filter, input-files extracted"
|
|
|
|
# Arrange
|
|
|
|
# Include via input-filter globs
|
|
|
|
group1_file1 = create_file(tmp_path, filename="group1-file1.pdf")
|
|
|
|
group1_file2 = create_file(tmp_path, filename="group1-file2.pdf")
|
|
|
|
group2_file1 = create_file(tmp_path, filename="group2-file1.pdf")
|
|
|
|
group2_file2 = create_file(tmp_path, filename="group2-file2.pdf")
|
|
|
|
# Include via input-file field
|
|
|
|
file1 = create_file(tmp_path, filename="document.pdf")
|
|
|
|
# Not included by any filter
|
|
|
|
create_file(tmp_path, filename="not-included-document.pdf")
|
|
|
|
create_file(tmp_path, filename="not-included-text.txt")
|
|
|
|
|
2023-08-31 19:55:17 +00:00
|
|
|
expected_files = set(
|
|
|
|
[os.path.join(tmp_path, file.name) for file in [group1_file1, group1_file2, group2_file1, group2_file2, file1]]
|
|
|
|
)
|
2023-06-01 11:27:38 +00:00
|
|
|
|
|
|
|
# Setup input-files, input-filters
|
|
|
|
input_files = [tmp_path / "document.pdf"]
|
|
|
|
input_filter = [tmp_path / "group1*.pdf", tmp_path / "group2*.pdf"]
|
|
|
|
|
2023-08-31 19:55:17 +00:00
|
|
|
pdf_config = TextContentConfig(
|
|
|
|
input_files=input_files,
|
|
|
|
input_filter=[str(path) for path in input_filter],
|
|
|
|
compressed_jsonl=tmp_path / "test.jsonl",
|
|
|
|
embeddings_file=tmp_path / "test_embeddings.jsonl",
|
|
|
|
)
|
|
|
|
|
2023-06-01 11:27:38 +00:00
|
|
|
# Act
|
2023-08-31 19:55:17 +00:00
|
|
|
extracted_pdf_files = get_pdf_files(pdf_config)
|
2023-06-01 11:27:38 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert len(extracted_pdf_files) == 5
|
2023-08-31 19:55:17 +00:00
|
|
|
assert set(extracted_pdf_files.keys()) == expected_files
|
2023-06-01 11:27:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Helper Functions
|
|
|
|
def create_file(tmp_path, entry=None, filename="document.pdf"):
|
|
|
|
pdf_file = tmp_path / filename
|
|
|
|
pdf_file.touch()
|
|
|
|
if entry:
|
|
|
|
pdf_file.write_text(entry)
|
|
|
|
return pdf_file
|