|
|
|
|
|
from transformers import pipeline |
|
from transformers import Tool |
|
|
|
class NamedEntityRecognitionTool(Tool): |
|
name = "ner_tool" |
|
description = "Identifies and labels entities such as persons, organizations, and locations in a given text." |
|
inputs = ["text"] |
|
outputs = ["entities"] |
|
|
|
def __call__(self, text: str): |
|
|
|
ner_analyzer = pipeline("ner") |
|
|
|
|
|
entities = ner_analyzer(text) |
|
|
|
|
|
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] |
|
|
|
|
|
location_entities = [text[start:end] for entity in entity_info if entity["entity"] == "I-LOC" for start, end in [(entity["start"], entity["end"])]] |
|
|
|
|
|
print(f"Identified Location Entities: {location_entities}") |
|
|
|
return {"entities": location_entities} |
|
|