Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion test_unstructured/cleaners/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,23 @@ def test_get_indexed_match_raises_with_bad_index():


def test_get_indexed_match_raises_with_index_too_high():
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="The largest index was 2"):
extract._get_indexed_match("BLAH BLAH BLAH", "BLAH", 4)


def test_get_indexed_match_raises_with_no_matches():
with pytest.raises(ValueError, match="No matches were found"):
extract._get_indexed_match("BLAH BLAH BLAH", "XYZ", 0)


def test_extract_text_before_and_after_with_absent_pattern():
with pytest.raises(ValueError, match="No matches were found"):
extract.extract_text_before("hello world", "xyz")

with pytest.raises(ValueError, match="No matches were found"):
extract.extract_text_after("hello world", "xyz")


def test_extract_text_before():
text = "Teacher: BLAH BLAH BLAH; Student: BLAH BLAH BLAH!"
assert extract.extract_text_before(text, "BLAH", 1) == "Teacher: BLAH"
Expand Down
3 changes: 3 additions & 0 deletions unstructured/cleaners/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ def _get_indexed_match(text: str, pattern: str, index: int = 0) -> re.Match:
raise ValueError(f"The index is {index}. Index must be a non-negative integer.")

regex_match = None
i = -1
for i, result in enumerate(re.finditer(pattern, text)):
if i == index:
regex_match = result

if regex_match is None:
if i < 0:
raise ValueError(f"Result with index {index} was not found. No matches were found.")
raise ValueError(f"Result with index {index} was not found. The largest index was {i}.")

return regex_match
Expand Down