💡 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 dropping paragraph/section splits on CRLF line endings (#1152)
Loi Nguyen2389b5946c72
diff --git a/src/python/txtai/pipeline/data/segmentation.py b/src/python/txtai/pipeline/data/segmentation.pyindex ddda16ed..066016c1 100644--- a/src/python/txtai/pipeline/data/segmentation.py+++ b/src/python/txtai/pipeline/data/segmentation.py@@ -141,11 +141,11 @@ class Segmentation(Pipeline):elif self.sentences:content = [self.clean(x) for x in sent_tokenize(text)]elif self.lines:- content = [self.clean(x) for x in re.split(r"\n{1,}", text)]+ 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"\n{2,}", text)]+ content = [self.clean(x) for x in re.split(r"(?:\r?\n){2,}", text)]elif self.sections:- split = r"\f" if "\f" in text else r"\n{3,}"+ split = r"\f" if "\f" in text else r"(?:\r?\n){3,}"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.pynew file mode 100644index 00000000..264c0d1e--- /dev/null+++ b/test/python/testpipeline/testdata/testsegmentation.py@@ -0,0 +1,40 @@+"""+Segmentation module tests+"""++import unittest++from txtai.pipeline import Segmentation+++class TestSegmentation(unittest.TestCase):+ """+ Segmentation tests.+ """++ def testParagraphs(self):+ """+ Test paragraph segmentation with both LF and CRLF blank lines.+ """+ segment = Segmentation(paragraphs=True)+ self.assertEqual(segment("Para A.\n\nPara B."), ["Para A.", "Para B."])+ # 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."])++ def testSections(self):+ """+ Test section segmentation with both LF and CRLF blank lines.+ """+ segment = Segmentation(sections=True)+ self.assertEqual(segment("Sec A.\n\n\nSec B."), ["Sec A.", "Sec B."])+ self.assertEqual(+ segment("Sec A.\r\n\r\n\r\nSec B.\r\n\r\n\r\nSec C."),+ ["Sec A.", "Sec B.", "Sec C."],+ )++ def testLines(self):+ """+ Test line segmentation tolerates CRLF.+ """+ segment = Segmentation(lines=True)+ self.assertEqual(segment("Line A.\r\nLine B."), ["Line A.", "Line B."])