|
from typing import Any, Dict, List |
|
import spacy |
|
|
|
class EndpointHandler(): |
|
def __init__( |
|
self, |
|
path: str, |
|
): |
|
|
|
self.nlp = spacy.load(".") |
|
|
|
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: |
|
""" |
|
Args: |
|
inputs (:obj:`str`): |
|
a string containing some text |
|
Return: |
|
A :obj:`list`:. The object returned should be like [{"entity_group": "XXX", "word": "some word", "start": 3, "end": 6, "score": 0.82}] containing : |
|
- "entity_group": A string representing what the entity is. |
|
- "word": A substring of the original string that was detected as an entity. |
|
- "start": the offset within `input` leading to `answer`. context[start:stop] == word |
|
- "end": the ending offset within `input` leading to `answer`. context[start:stop] === word |
|
- "score": A score between 0 and 1 describing how confident the model is for this entity. |
|
""" |
|
inputs = data.pop("inputs", data) |
|
|
|
doc=self.nlp(inputs) |
|
|
|
entities = [] |
|
for span in doc.ents: |
|
if len(span.ents) == 0: |
|
continue |
|
current_entity = { |
|
"entity_group": span.label_, |
|
"word": span.text, |
|
"start": span.start_char, |
|
"end": span.end_char, |
|
|
|
} |
|
entities.append(current_entity) |
|
|
|
return entities |
|
|