Skip to content

Commit b5cde03

Browse files
committed
feat: add clean_newline utility for hyphenated line breaks (#2513)
1 parent 3ac4443 commit b5cde03

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

test_unstructured/cleaners/test_core.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,21 @@ def test_clean_extra_whitespace(text, expected):
147147
assert core.clean(text=text, extra_whitespace=True) == expected
148148

149149

150+
@pytest.mark.parametrize(
151+
("text", "expected"),
152+
[
153+
("re- \nsearch", "research"),
154+
(
155+
"This is an example of text contin-\nuing to next line",
156+
"This is an example of text continuing to next line",
157+
),
158+
("pre-\n processing", "preprocessing"),
159+
],
160+
)
161+
def test_clean_newline(text, expected):
162+
assert core.clean_newline(text) == expected
163+
164+
150165
@pytest.mark.parametrize(
151166
("text", "expected"),
152167
[

unstructured/cleaners/core.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,16 @@ def clean_extra_whitespace(text: str) -> str:
334334
return cleaned_text.strip()
335335

336336

337+
def clean_newline(text: str) -> str:
338+
"""Concatenates hyphenated words split across newlines or significant whitespace.
339+
340+
Example
341+
-------
342+
re- \nsearch -> research
343+
"""
344+
return re.sub(r"(\w+)-\s+(\w+)", r"\1\2", text)
345+
346+
337347
def clean_dashes(text: str) -> str:
338348
"""Cleans dash characters in text.
339349

0 commit comments

Comments
 (0)