Fix HighlightedText component (#2221)

* entities

* tweak
This commit is contained in:
Abubakar Abid 2022-09-10 00:32:06 -07:00 committed by GitHub
parent 72ee6f8b80
commit 6ee7efa86c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 3 deletions

View File

@ -2904,7 +2904,7 @@ class HighlightedText(Changeable, IOComponent, JSONSerializable):
"""
Displays text that contains spans that are highlighted by category or numerical value.
Preprocessing: this component does *not* accept input.
Postprocessing: expects a {List[Tuple[str, float | str]]]} consisting of spans of text and their associated labels, or a {Dict} with two keys: (1) "text" whose value is the complete text, and "entities", which is a list of dictionaries, each of which have the keys: "entity" (consisting of the entity label), "start" (the character index where the label starts), and "end" (the character index where the label ends).
Postprocessing: expects a {List[Tuple[str, float | str]]]} consisting of spans of text and their associated labels, or a {Dict} with two keys: (1) "text" whose value is the complete text, and "entities", which is a list of dictionaries, each of which have the keys: "entity" (consisting of the entity label), "start" (the character index where the label starts), and "end" (the character index where the label ends). Entities should not overlap.
Demos: diff_texts, text_analysis
Guides: named_entity_recognition
@ -2995,13 +2995,19 @@ class HighlightedText(Changeable, IOComponent, JSONSerializable):
if y is None:
return None
if isinstance(y, dict):
text = y["text"]
entities = y["entities"]
try:
text = y["text"]
entities = y["entities"]
except KeyError:
raise ValueError(
"Expected a dictionary with keys 'text' and 'entities' for the value of the HighlightedText component."
)
if len(entities) == 0:
y = [(text, None)]
else:
list_format = []
index = 0
entities = sorted(entities, key=lambda x: x["start"])
for entity in entities:
list_format.append((text[index : entity["start"]], None))
list_format.append(

View File

@ -1381,6 +1381,14 @@ class TestHighlightedText(unittest.TestCase):
result_ = component.postprocess({"text": text, "entities": entities})
self.assertEqual(result, result_)
text = "Wolfgang lives in Berlin"
entities = [
{"entity": "LOC", "start": 18, "end": 24},
{"entity": "PER", "start": 0, "end": 8},
]
result_ = component.postprocess({"text": text, "entities": entities})
self.assertEqual(result, result_)
text = "I live there"
entities = []
result_ = component.postprocess({"text": text, "entities": entities})