2020-06-08 11:07:19 +02:00
|
|
|
from pathlib import Path
|
|
|
|
|
2020-08-17 11:21:09 +02:00
|
|
|
import pytest
|
2020-08-17 11:13:52 +02:00
|
|
|
|
2021-02-12 13:38:54 +01:00
|
|
|
from haystack.file_converter.docx import DocxToTextConverter
|
2020-09-16 18:33:23 +02:00
|
|
|
from haystack.file_converter.pdf import PDFToTextConverter
|
|
|
|
from haystack.file_converter.tika import TikaConverter
|
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])
|
2020-09-18 10:42:13 +02:00
|
|
|
def test_convert(Converter, xpdf_fixture):
|
2020-08-17 11:21:09 +02:00
|
|
|
converter = Converter()
|
2020-09-18 10:42:13 +02:00
|
|
|
document = converter.convert(file_path=Path("samples/pdf/sample_pdf_1.pdf"))
|
|
|
|
pages = document["text"].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.
|
|
|
|
|
|
|
|
|
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])
|
|
|
|
def test_table_removal(Converter, xpdf_fixture):
|
|
|
|
converter = Converter(remove_numeric_tables=True)
|
2020-09-18 10:42:13 +02:00
|
|
|
document = converter.convert(file_path=Path("samples/pdf/sample_pdf_1.pdf"))
|
|
|
|
pages = document["text"].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]
|
|
|
|
|
|
|
|
# assert text is retained from the document.
|
2021-02-09 13:42:43 +01:00
|
|
|
# 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])
|
|
|
|
def test_language_validation(Converter, xpdf_fixture, caplog):
|
|
|
|
converter = Converter(valid_languages=["en"])
|
2020-09-18 10:42:13 +02:00
|
|
|
converter.convert(file_path=Path("samples/pdf/sample_pdf_1.pdf"))
|
2020-06-08 11:07:19 +02:00
|
|
|
assert "The language for samples/pdf/sample_pdf_1.pdf is not one of ['en']." not in caplog.text
|
|
|
|
|
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"))
|
2020-06-08 11:07:19 +02:00
|
|
|
assert "The language for samples/pdf/sample_pdf_1.pdf is not one of ['de']." in caplog.text
|
|
|
|
|
|
|
|
|
2021-02-12 13:38:54 +01:00
|
|
|
def test_docx_converter():
|
|
|
|
converter = DocxToTextConverter()
|
|
|
|
document = converter.convert(file_path=Path("samples/docx/sample_docx.docx"))
|
|
|
|
assert document["text"].startswith("Sample Docx File")
|