💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
Fix Segmentation paragraph and section splits on whitespace-only blank lines (#1441)
Spicy Sharkded862718737
diff --git a/src/python/txtai/pipeline/data/segmentation.py b/src/python/txtai/pipeline/data/segmentation.pyindex 066016c1..cdee0f90 100644--- a/src/python/txtai/pipeline/data/segmentation.py+++ b/src/python/txtai/pipeline/data/segmentation.py@@ -143,9 +143,9 @@ class Segmentation(Pipeline):elif self.lines:content = [self.clean(x) for x in re.split(r"(?:\r?\n)+", text)]elif self.paragraphs:- content = [self.clean(x) for x in re.split(r"(?:\r?\n){2,}", text)]+ content = [self.clean(x) for x in re.split(r"\r?\n(?:[ \t]*\r?\n)+", text)]elif self.sections:- split = r"\f" if "\f" in text else r"(?:\r?\n){3,}"+ split = r"\f" if "\f" in text else r"\r?\n(?:[ \t]*\r?\n){2,}"content = [self.clean(x) for x in re.split(split, text)]else:content = self.clean(text)diff --git a/test/python/testpipeline/testdata/testsegmentation.py b/test/python/testpipeline/testdata/testsegmentation.pyindex 9bdf72f8..cae74c5d 100644--- a/test/python/testpipeline/testdata/testsegmentation.py+++ b/test/python/testpipeline/testdata/testsegmentation.py@@ -22,7 +22,7 @@ class TestSegmentation(unittest.TestCase):def testParagraphs(self):"""- Test paragraph segmentation with both LF and CRLF blank lines+ Test paragraph segmentation with LF, CRLF and whitespace-only blank lines"""segment = Segmentation(paragraphs=True)@@ -31,9 +31,16 @@ class TestSegmentation(unittest.TestCase):# Windows blank lines (\r\n\r\n) must split the same way as LF.self.assertEqual(segment("Para A.\r\n\r\nPara B."), ["Para A.", "Para B."])+ # Blank lines that only contain spaces or tabs must split the same way.+ self.assertEqual(segment("Para A.\n \nPara B."), ["Para A.", "Para B."])+ self.assertEqual(segment("Para A.\r\n\t\r\nPara B."), ["Para A.", "Para B."])++ # Whitespace inside a paragraph is kept.+ self.assertEqual(segment("Line A.\n Line B.\n\nPara B."), ["Line A.\n Line B.", "Para B."])+def testSections(self):"""- Test section segmentation with both LF and CRLF blank lines+ Test section segmentation with LF, CRLF and whitespace-only blank lines"""segment = Segmentation(sections=True)@@ -44,3 +51,6 @@ class TestSegmentation(unittest.TestCase):segment("Sec A.\r\n\r\n\r\nSec B.\r\n\r\n\r\nSec C."),["Sec A.", "Sec B.", "Sec C."],)++ # Blank lines that only contain spaces or tabs must split the same way.+ self.assertEqual(segment("Sec A.\n \n\t\nSec B."), ["Sec A.", "Sec B."])