Chris4K commited on
Commit
fb510e6
1 Parent(s): f866660

Update ner_tool.py

Browse files
Files changed (1) hide show
  1. 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 = ["text"]
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
- # Print the identified entities
20
- print(f"Identified Entities: {entities}")
 
 
 
21
 
22
- # Extract entity labels and return as a list
23
- entity_labels = [entity.get("entity", "UNKNOWN") for entity in entities]
24
 
25
- return {"entities": entity_labels} # Return a dictionary with the specified output component
 
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