2023-01-09 11:08:08 -06:00
|
|
|
import os
|
|
|
|
import pathlib
|
2023-02-27 17:30:54 +01:00
|
|
|
|
2023-01-09 11:08:08 -06:00
|
|
|
import pytest
|
|
|
|
|
2023-02-27 17:30:54 +01:00
|
|
|
from unstructured.documents.elements import Address, ListItem, NarrativeText, Title
|
2023-01-09 11:08:08 -06:00
|
|
|
from unstructured.partition.text import partition_text
|
|
|
|
|
|
|
|
DIRECTORY = pathlib.Path(__file__).parent.resolve()
|
|
|
|
|
|
|
|
EXPECTED_OUTPUT = [
|
|
|
|
NarrativeText(text="This is a test document to use for unit tests."),
|
2023-01-26 10:52:25 -05:00
|
|
|
Address(text="Doylestown, PA 18901"),
|
2023-01-09 11:08:08 -06:00
|
|
|
Title(text="Important points:"),
|
|
|
|
ListItem(text="Hamburgers are delicious"),
|
|
|
|
ListItem(text="Dogs are the best"),
|
|
|
|
ListItem(text="I love fuzzy blankets"),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2023-03-06 15:07:33 -08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("filename", "encoding"),
|
|
|
|
[("fake-text.txt", "utf-8"), ("fake-text.txt", None), ("fake-text-utf-16-be.txt", "utf-16-be")],
|
|
|
|
)
|
|
|
|
def test_partition_text_from_filename(filename, encoding):
|
|
|
|
filename = os.path.join(DIRECTORY, "..", "..", "example-docs", filename)
|
|
|
|
elements = partition_text(filename=filename, encoding=encoding)
|
2023-01-09 11:08:08 -06:00
|
|
|
assert len(elements) > 0
|
|
|
|
assert elements == EXPECTED_OUTPUT
|
|
|
|
|
|
|
|
|
2023-03-06 15:07:33 -08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("filename", "encoding", "error"),
|
|
|
|
[
|
|
|
|
("fake-text.txt", "utf-16", UnicodeDecodeError),
|
|
|
|
("fake-text-utf-16-be.txt", "utf-16", UnicodeError),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_partition_text_from_filename_raises_econding_error(filename, encoding, error):
|
|
|
|
with pytest.raises(error):
|
|
|
|
filename = os.path.join(DIRECTORY, "..", "..", "example-docs", filename)
|
|
|
|
partition_text(filename=filename, encoding=encoding)
|
|
|
|
|
|
|
|
|
2023-01-13 16:39:53 -05:00
|
|
|
def test_partition_text_from_file():
|
2023-01-09 11:08:08 -06:00
|
|
|
filename = os.path.join(DIRECTORY, "..", "..", "example-docs", "fake-text.txt")
|
2023-02-27 17:30:54 +01:00
|
|
|
with open(filename) as f:
|
2023-01-09 11:08:08 -06:00
|
|
|
elements = partition_text(file=f)
|
|
|
|
assert len(elements) > 0
|
|
|
|
assert elements == EXPECTED_OUTPUT
|
|
|
|
|
|
|
|
|
2023-01-13 16:39:53 -05:00
|
|
|
def test_partition_text_from_text():
|
2023-01-09 11:08:08 -06:00
|
|
|
filename = os.path.join(DIRECTORY, "..", "..", "example-docs", "fake-text.txt")
|
2023-02-27 17:30:54 +01:00
|
|
|
with open(filename) as f:
|
2023-01-09 11:08:08 -06:00
|
|
|
text = f.read()
|
|
|
|
elements = partition_text(text=text)
|
|
|
|
assert len(elements) > 0
|
|
|
|
assert elements == EXPECTED_OUTPUT
|
|
|
|
|
|
|
|
|
2023-03-28 17:03:51 -04:00
|
|
|
def test_partition_text_from_text_works_with_empty_string():
|
|
|
|
assert partition_text(text="") == []
|
|
|
|
|
|
|
|
|
2023-01-13 16:39:53 -05:00
|
|
|
def test_partition_text_raises_with_none_specified():
|
2023-01-09 11:08:08 -06:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
partition_text()
|
|
|
|
|
|
|
|
|
2023-01-13 16:39:53 -05:00
|
|
|
def test_partition_text_raises_with_too_many_specified():
|
2023-01-09 11:08:08 -06:00
|
|
|
filename = os.path.join(DIRECTORY, "..", "..", "example-docs", "fake-text.txt")
|
2023-02-27 17:30:54 +01:00
|
|
|
with open(filename) as f:
|
2023-01-09 11:08:08 -06:00
|
|
|
text = f.read()
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
partition_text(filename=filename, text=text)
|
2023-01-26 10:52:25 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_partition_text_captures_everything_even_with_linebreaks():
|
|
|
|
text = """
|
|
|
|
VERY IMPORTANT MEMO
|
|
|
|
DOYLESTOWN, PA 18901
|
|
|
|
"""
|
|
|
|
elements = partition_text(text=text)
|
|
|
|
assert elements == [
|
|
|
|
Title(text="VERY IMPORTANT MEMO"),
|
|
|
|
Address(text="DOYLESTOWN, PA 18901"),
|
|
|
|
]
|