Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Respect numeric zero entity flattening thresholds (#1405)

HuaTNAaba97cf16c14
diff --git a/src/python/txtai/pipeline/text/entity.py b/src/python/txtai/pipeline/text/entity.py
index 57564a18..c48f4ea1 100644
--- a/src/python/txtai/pipeline/text/entity.py
+++ b/src/python/txtai/pipeline/text/entity.py
@@ -64,7 +64,7 @@ class Entity(HFPipeline):
# Extract entities if flatten set, otherwise extract (entity, entity type, score) tuples
outputs = []
for result in results:
- if flatten:
+ if flatten is not None and flatten is not False:
output = [r["word"] for r in result if self.accept(r["entity_group"], labels) and r["score"] >= threshold]
outputs.append(" ".join(output) if join else output)
else:
diff --git a/test/python/testpipeline/testtext/testentity.py b/test/python/testpipeline/testtext/testentity.py
index 9868f7ed..ef5d1533 100644
--- a/test/python/testpipeline/testtext/testentity.py
+++ b/test/python/testpipeline/testtext/testentity.py
@@ -44,6 +44,20 @@ class TestEntity(unittest.TestCase):
)
self.assertEqual(entities, "Canada Manhattan")
+ def testEntityZeroThreshold(self):
+ """
+ Test zero-threshold flattened and joined entity outputs
+ """
+
+ text = "Canada's last fully intact ice shelf has suddenly collapsed, forming a Manhattan-sized iceberg"
+ self.assertEqual(self.entity(text, flatten=0.0), ["Canada", "Manhattan"])
+ self.assertEqual(self.entity([text], flatten=0.0), [["Canada", "Manhattan"]])
+ self.assertEqual(self.entity(text, flatten=0.0, join=True), "Canada Manhattan")
+ self.assertEqual(self.entity(text, labels=["LOC"], flatten=0.0), ["Canada"])
+
+ # Boolean False keeps scored tuples, even when joining is requested.
+ self.assertEqual(self.entity(text, flatten=False, join=True), self.entity(text))
+
def testEntityTypes(self):
"""
Test entity type filtering