💡 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
Add parameter to disable text cleaning in Segmentation pipeline, closes #823
davidmezzetti7edc7aa18465
diff --git a/src/python/txtai/pipeline/data/segmentation.py b/src/python/txtai/pipeline/data/segmentation.pyindex 91274a44..ec0e1a08 100644--- a/src/python/txtai/pipeline/data/segmentation.py+++ b/src/python/txtai/pipeline/data/segmentation.py@@ -20,7 +20,7 @@ class Segmentation(Pipeline):Segments text into logical units."""- def __init__(self, sentences=False, lines=False, paragraphs=False, minlength=None, join=False, sections=False):+ def __init__(self, sentences=False, lines=False, paragraphs=False, minlength=None, join=False, sections=False, cleantext=True):"""Creates a new Segmentation pipeline.@@ -31,6 +31,7 @@ class Segmentation(Pipeline):minlength: require at least minlength characters per text element, defaults to Nonejoin: joins tokenized sections back together if True, defaults to Falsesections: tokenizes text into sections if True, defaults to False. Splits using section or page breaks, depending on what's available+ cleantext: apply text cleaning rules, defaults to True"""if not NLTK:@@ -42,6 +43,7 @@ class Segmentation(Pipeline):self.sections = sectionsself.minlength = minlengthself.join = join+ self.cleantext = cleantextdef __call__(self, text):"""@@ -127,7 +129,13 @@ class Segmentation(Pipeline):clean text"""+ # Text cleaning disabled, return original text+ if not self.cleantext:+ return text++ # Collapse and remove excess whitespacetext = re.sub(r" +", " ", text)text = text.strip()+ # If minlength enabled, require at least minlength charsreturn text if not self.minlength or len(text) >= self.minlength else Nonediff --git a/src/python/txtai/pipeline/data/textractor.py b/src/python/txtai/pipeline/data/textractor.pyindex db92ddbf..e35358ef 100644--- a/src/python/txtai/pipeline/data/textractor.py+++ b/src/python/txtai/pipeline/data/textractor.py@@ -28,11 +28,13 @@ class Textractor(Segmentation):Extracts text from files."""- def __init__(self, sentences=False, lines=False, paragraphs=False, minlength=None, join=False, tika=True, sections=False, headers=None):+ def __init__(+ self, sentences=False, lines=False, paragraphs=False, minlength=None, join=False, tika=True, sections=False, cleantext=True, headers=None+ ):if not TIKA:raise ImportError('Textractor pipeline is not available - install "pipeline" extra to enable')- super().__init__(sentences, lines, paragraphs, minlength, join, sections)+ super().__init__(sentences, lines, paragraphs, minlength, join, sections, cleantext)# Determine if Apache Tika (default if Java is available) or Beautiful Soup should be used# Beautiful Soup only supports HTML, Tika supports a wide variety of file formats.diff --git a/test/python/testpipeline/testdata/testtextractor.py b/test/python/testpipeline/testdata/testtextractor.pyindex 7280cf74..7333b8ed 100644--- a/test/python/testpipeline/testdata/testtextractor.py+++ b/test/python/testpipeline/testdata/testtextractor.py@@ -23,6 +23,23 @@ class TestTextractor(unittest.TestCase):textractor = Textractor()self.assertFalse(textractor.checkjava("1112444abc"))+ def testClean(self):+ """+ Test text cleaning method+ """++ # Default text cleaning+ textractor = Textractor()+ self.assertEqual(textractor(" a b c "), "a b c")++ # Require text to be minlength+ textractor = Textractor(minlength=10)+ self.assertEqual(textractor(" a b c "), None)++ # Disable text cleaning+ textractor = Textractor(cleantext=False, minlength=10)+ self.assertEqual(textractor(" a b c "), " a b c ")+def testDefault(self):"""Test default text extraction