💡 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
Keep text wrapped in inline elements inside article paragraphs (#1409)
George Tonmoy Royd2792460a6c2
diff --git a/src/python/txtai/pipeline/data/htmltomd.py b/src/python/txtai/pipeline/data/htmltomd.pyindex 7f09025e..fbcd93b8 100644--- a/src/python/txtai/pipeline/data/htmltomd.py+++ b/src/python/txtai/pipeline/data/htmltomd.py@@ -123,6 +123,9 @@ class HTMLToMarkdown(Pipeline):if self.iscontainer(node, children):texts = [self.process(node, article) for node in children]text = "\n".join(text for text in texts if text or not article)++ # Apply paragraph formatting to article paragraphs that only have element children+ text = self.articletext(node, text) if article and node.name == "p" else textelse:text = self.text(node, article)@@ -410,8 +413,8 @@ class HTMLToMarkdown(Pipeline):# List of valid text nodesvalid = ("p", "th", "td", "li", "a", "b", "strong", "i", "em")- # Check if this node is valid or it's part of a table cell- valid = node.name in valid or (node.parent and node.parent.name in ("th", "td"))+ # Check if this node is valid or it's part of a paragraph or table cell+ valid = node.name in valid or node.find_parent(("p", "th", "td")) is not None# Check if text is valid article texttext = text if (valid or self.isheader(node)) and not self.islink(node) else ""diff --git a/test/python/testpipeline/testdata/testtextractor.py b/test/python/testpipeline/testdata/testtextractor.pyindex 440f33de..450aabb8 100644--- a/test/python/testpipeline/testdata/testtextractor.py+++ b/test/python/testpipeline/testdata/testtextractor.py@@ -175,6 +175,34 @@ class TestTextractor(unittest.TestCase):"|Header1|Header2|\n|---|---|\n|Test1 Test2|Test3|",)+ def testHTMLArticleSpan(self):+ """+ Test article paragraphs with text wrapped in inline elements+ """++ textractor = Textractor()++ for section in ["article", "main"]:+ with self.subTest(section=section):+ html = f"<html><body><{section}><p><span>This is a test</span></p></{section}></body></html>"+ self.assertEqual(textractor(html), "This is a test")++ # Mixed paragraphs, nested spans and formatting inside a span+ html = "<html><body><article><p>Test1</p><p><span><span>Test2</span></span></p><p><span>Test <b>3</b></span></p></article></body></html>"+ self.assertEqual(textractor(html), "Test1\n\nTest2\n\nTest **3**")++ # Link only paragraphs and asides are still skipped+ html = (+ "<html><body><article><p><span>This is a test</span></p><p><span><a href='link'>Home</a></span></p>"+ "<aside><p>Skip</p></aside></article></body></html>"+ )+ self.assertEqual(textractor(html), "This is a test")++ # Each paragraph is kept separate+ textractor = Textractor(paragraphs=True)+ html = "<html><body><article><p><span>Test1</span></p><p><span>Test2</span></p></article></body></html>"+ self.assertEqual(textractor(html), ["Test1", "Test2"])+def testHTMLListStart(self):"""Test ordered lists retain their starting number