Spaces:
Sleeping
Sleeping
Update ner_tool.py
Browse files- ner_tool.py +9 -6
ner_tool.py
CHANGED
@@ -7,7 +7,7 @@ class NamedEntityRecognitionTool(Tool):
|
|
7 |
name = "ner_tool"
|
8 |
description = "Identifies and labels entities such as persons, organizations, and locations in a given text."
|
9 |
inputs = ["text"]
|
10 |
-
outputs = ["
|
11 |
|
12 |
def __call__(self, text: str):
|
13 |
# Initialize the named entity recognition pipeline
|
@@ -16,10 +16,13 @@ class NamedEntityRecognitionTool(Tool):
|
|
16 |
# Perform named entity recognition on the input text
|
17 |
entities = ner_analyzer(text)
|
18 |
|
19 |
-
#
|
20 |
-
|
|
|
|
|
|
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
|
25 |
-
return {"entities":
|
|
|
7 |
name = "ner_tool"
|
8 |
description = "Identifies and labels entities such as persons, organizations, and locations in a given text."
|
9 |
inputs = ["text"]
|
10 |
+
outputs = ["entities"]
|
11 |
|
12 |
def __call__(self, text: str):
|
13 |
# Initialize the named entity recognition pipeline
|
|
|
16 |
# Perform named entity recognition on the input text
|
17 |
entities = ner_analyzer(text)
|
18 |
|
19 |
+
# Extract relevant information for each identified entity
|
20 |
+
entity_info = [{"entity": entity.get("entity", "UNKNOWN"), "word": entity.get("word", ""), "start": entity.get("start", -1), "end": entity.get("end", -1)} for entity in entities]
|
21 |
+
|
22 |
+
# Extract the actual text span for each identified location entity
|
23 |
+
location_entities = [text[start:end] for entity in entity_info if entity["entity"] == "I-LOC" for start, end in [(entity["start"], entity["end"])]]
|
24 |
|
25 |
+
# Print the identified entities
|
26 |
+
print(f"Identified Location Entities: {location_entities}")
|
27 |
|
28 |
+
return {"entities": location_entities} # Return a dictionary with the specified output component
|