|
2 | 2 | import re |
3 | 3 | from pathlib import Path |
4 | 4 |
|
5 | | -from docling_core.types.doc.document import DoclingDocument |
| 5 | +from docling_core.types.doc.document import DoclingDocument, EntityMention |
6 | 6 |
|
7 | 7 | from docling_agent.agent.editor import DoclingEditingAgent |
8 | 8 | from docling_agent.agent.enricher import DoclingEnrichingAgent |
@@ -187,6 +187,43 @@ def _fake_editor_run(self, task: str, document: DoclingDocument | None = None, * |
187 | 187 | print("=" * 70) |
188 | 188 |
|
189 | 189 |
|
| 190 | +def test_make_entity_mention(): |
| 191 | + """Regression test for _make_entity_mention function.""" |
| 192 | + enricher = DoclingEnrichingAgent(backend=_backend(), tools=[]) |
| 193 | + source_text = "International Business Machines is a technology company based in Armonk." |
| 194 | + |
| 195 | + # Basic entity mention |
| 196 | + item = {"text": "International Business Machines"} |
| 197 | + mention = enricher._make_entity_mention(item=item, source_text=source_text) |
| 198 | + assert isinstance(mention, EntityMention) |
| 199 | + assert mention.text == "International Business Machines" |
| 200 | + assert mention.charspan == (0, 31) |
| 201 | + |
| 202 | + # Entity with original and label |
| 203 | + item = {"text": "IBM", "original": "International Business Machines", "label": "ORGANIZATION"} |
| 204 | + mention = enricher._make_entity_mention(item=item, source_text=source_text) |
| 205 | + assert mention.text == "IBM" |
| 206 | + assert mention.orig == "International Business Machines" |
| 207 | + assert mention.label == "ORGANIZATION" |
| 208 | + assert mention.charspan == (0, 31) |
| 209 | + |
| 210 | + # Case-insensitive matching |
| 211 | + item = {"text": "international business machines"} |
| 212 | + mention = enricher._make_entity_mention(item=item, source_text=source_text) |
| 213 | + assert mention.charspan == (0, 31) |
| 214 | + |
| 215 | + # Entity not found |
| 216 | + item = {"text": "Red Hat"} |
| 217 | + mention = enricher._make_entity_mention(item=item, source_text=source_text) |
| 218 | + assert mention.charspan is None |
| 219 | + |
| 220 | + # Original takes precedence in search |
| 221 | + source_abbrev = "The CEO of IBM announced products." |
| 222 | + item = {"text": "International Business Machines", "original": "IBM"} |
| 223 | + mention = enricher._make_entity_mention(item=item, source_text=source_abbrev) |
| 224 | + assert mention.charspan == (11, 14) |
| 225 | + |
| 226 | + |
190 | 227 | if __name__ == "__main__": |
191 | 228 | print("=" * 70) |
192 | 229 | print("TEST 1: Demonstrating the heading levels problem") |
|
0 commit comments