2020-06-08 11:07:19 +02:00
|
|
|
from pathlib import Path
|
2021-11-29 18:44:20 +01:00
|
|
|
import os
|
2020-06-08 11:07:19 +02:00
|
|
|
|
2020-08-17 11:21:09 +02:00
|
|
|
import pytest
|
2020-08-17 11:13:52 +02:00
|
|
|
|
2021-11-29 18:44:20 +01:00
|
|
|
from haystack.nodes import MarkdownConverter, DocxToTextConverter, PDFToTextConverter, PDFToTextOCRConverter, \
|
|
|
|
TikaConverter, AzureConverter
|
2020-06-08 11:07:19 +02:00
|
|
|
|
|
|
|
|
2020-10-30 18:06:02 +01:00
|
|
|
@pytest.mark.tika
|
2021-09-01 16:42:25 +02:00
|
|
|
@pytest.mark.parametrize(
|
2021-10-13 14:23:23 +02:00
|
|
|
# "Converter", [PDFToTextConverter, TikaConverter, PDFToTextOCRConverter]
|
|
|
|
"Converter", [PDFToTextOCRConverter]
|
2021-09-01 16:42:25 +02:00
|
|
|
)
|
2021-10-29 13:52:28 +05:30
|
|
|
def test_convert(Converter):
|
2020-08-17 11:21:09 +02:00
|
|
|
converter = Converter()
|
2021-11-29 18:44:20 +01:00
|
|
|
document = converter.convert(file_path=Path("samples/pdf/sample_pdf_1.pdf"))[0]
|
2021-10-13 14:23:23 +02:00
|
|
|
pages = document["content"].split("\f")
|
2020-06-08 11:07:19 +02:00
|
|
|
assert len(pages) == 4 # the sample PDF file has four pages.
|
|
|
|
assert pages[0] != "" # the page 1 of PDF contains text.
|
|
|
|
assert pages[2] == "" # the page 3 of PDF file is empty.
|
2021-09-01 16:42:25 +02:00
|
|
|
# assert text is retained from the document.
|
|
|
|
# As whitespace can differ (\n," ", etc.), we standardize all to simple whitespace
|
|
|
|
page_standard_whitespace = " ".join(pages[0].split())
|
|
|
|
assert (
|
|
|
|
"Adobe Systems made the PDF specification available free of charge in 1993."
|
|
|
|
in page_standard_whitespace
|
|
|
|
)
|
2020-06-08 11:07:19 +02:00
|
|
|
|
|
|
|
|
2020-10-30 18:06:02 +01:00
|
|
|
@pytest.mark.tika
|
2020-08-17 11:21:09 +02:00
|
|
|
@pytest.mark.parametrize("Converter", [PDFToTextConverter, TikaConverter])
|
2021-10-29 13:52:28 +05:30
|
|
|
def test_table_removal(Converter):
|
2020-08-17 11:21:09 +02:00
|
|
|
converter = Converter(remove_numeric_tables=True)
|
2021-11-29 18:44:20 +01:00
|
|
|
document = converter.convert(file_path=Path("samples/pdf/sample_pdf_1.pdf"))[0]
|
2021-10-13 14:23:23 +02:00
|
|
|
pages = document["content"].split("\f")
|
2020-06-08 11:07:19 +02:00
|
|
|
# assert numeric rows are removed from the table.
|
|
|
|
assert "324" not in pages[0]
|
|
|
|
assert "54x growth" not in pages[0]
|
|
|
|
|
|
|
|
|
2020-10-30 18:06:02 +01:00
|
|
|
@pytest.mark.tika
|
2020-08-17 11:21:09 +02:00
|
|
|
@pytest.mark.parametrize("Converter", [PDFToTextConverter, TikaConverter])
|
2021-10-29 13:52:28 +05:30
|
|
|
def test_language_validation(Converter, caplog):
|
2020-08-17 11:21:09 +02:00
|
|
|
converter = Converter(valid_languages=["en"])
|
2020-09-18 10:42:13 +02:00
|
|
|
converter.convert(file_path=Path("samples/pdf/sample_pdf_1.pdf"))
|
2021-09-01 16:42:25 +02:00
|
|
|
assert (
|
|
|
|
"The language for samples/pdf/sample_pdf_1.pdf is not one of ['en']."
|
|
|
|
not in caplog.text
|
|
|
|
)
|
2020-06-08 11:07:19 +02:00
|
|
|
|
2020-08-17 11:21:09 +02:00
|
|
|
converter = Converter(valid_languages=["de"])
|
2020-09-18 10:42:13 +02:00
|
|
|
converter.convert(file_path=Path("samples/pdf/sample_pdf_1.pdf"))
|
2021-09-01 16:42:25 +02:00
|
|
|
assert (
|
|
|
|
"The language for samples/pdf/sample_pdf_1.pdf is not one of ['de']."
|
|
|
|
in caplog.text
|
|
|
|
)
|
2020-06-08 11:07:19 +02:00
|
|
|
|
|
|
|
|
2021-02-12 13:38:54 +01:00
|
|
|
def test_docx_converter():
|
|
|
|
converter = DocxToTextConverter()
|
2021-11-29 18:44:20 +01:00
|
|
|
document = converter.convert(file_path=Path("samples/docx/sample_docx.docx"))[0]
|
2021-10-13 14:23:23 +02:00
|
|
|
assert document["content"].startswith("Sample Docx File")
|
2021-03-23 16:31:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_markdown_converter():
|
|
|
|
converter = MarkdownConverter()
|
2021-11-29 18:44:20 +01:00
|
|
|
document = converter.convert(file_path=Path("samples/markdown/sample.md"))[0]
|
2021-10-13 14:23:23 +02:00
|
|
|
assert document["content"].startswith("What to build with Haystack")
|
2021-11-29 18:44:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_azure_converter():
|
|
|
|
# Check if Form Recognizer endpoint and credential key in environment variables
|
|
|
|
if "AZURE_FORMRECOGNIZER_ENDPOINT" in os.environ and "AZURE_FORMRECOGNIZER_KEY" in os.environ:
|
|
|
|
converter = AzureConverter(endpoint=os.environ["AZURE_FORMRECOGNIZER_ENDPOINT"],
|
|
|
|
credential_key=os.environ["AZURE_FORMRECOGNIZER_KEY"],
|
|
|
|
save_json=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
docs = converter.convert(file_path="samples/pdf/sample_pdf_1.pdf")
|
|
|
|
assert len(docs) == 2
|
|
|
|
assert docs[0]["content_type"] == "table"
|
|
|
|
assert len(docs[0]["content"]) == 5 # number of rows
|
|
|
|
assert len(docs[0]["content"][0]) == 5 # number of columns, Form Recognizer assumes there are 5 columns
|
|
|
|
assert docs[0]["content"][0] == ['', 'Column 1', '', 'Column 2', 'Column 3']
|
|
|
|
assert docs[0]["content"][4] == ['D', '$54.35', '', '$6345.', '']
|
|
|
|
|
|
|
|
assert docs[1]["content_type"] == "text"
|
|
|
|
assert docs[1]["content"].startswith("A sample PDF file")
|