medkit.text.metrics.ner
medkit.text.metrics.ner#
Classes:
|
Evaluator to compute the performance of labeling tasks such as named entity recognition. |
|
An implementation of |
- class SeqEvalEvaluator(tagging_scheme='bilou', return_metrics_by_label=True, tokenizer=None)[source]#
Evaluator to compute the performance of labeling tasks such as named entity recognition. This evaluator compares TextDocuments of reference with its predicted annotations and returns a dictionary of metrics.
The evaluator converts the set of entities and documents to tags before compute the metric. It supports two schemes, IOB2 (a BIO scheme) and BILOU. The IOB2 scheme tags the Beginning, the Inside and the Outside text of a entity. The BILOU scheme tags the Beginning, the Inside and the Last tokens of multi-token entity as well as Unit-length entity.
For more information about IOB schemes, refer to the Wikipedia page
Hint
If tokenizer is not defined, the evaluator tokenizes the text by character. This may generate a lot of tokens with large documents and may affect execution time. You can use a fast tokenizer from HuggingFace, i.e. : bert tokenizer
>>> from transformers import AutoTokenizer >>> tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased", use_fast=True)
- Parameters
tagging_scheme (
Literal['bilou','iob2']) – Scheme for tagging the tokens, it can be bilou or iob2return_metrics_by_label (
bool) – If True, return the metrics by label in the output dictionary. If False, only return overall metricstokenizer (
Optional[Any]) – Optional Fast Tokenizer to convert text into tokens. If not provided, the text is tokenized by character.
Methods:
compute(documents, predicted_entities)Compute metrics of entity matching giving predictions.
- compute(documents, predicted_entities)[source]#
Compute metrics of entity matching giving predictions.
- Parameters
documents (
List[TextDocument]) – Text documents containing entities of referencepredicted_entities (
List[List[Entity]]) – List of predicted entities by document
- Return type
Dict[str,float]- Returns
Dict[str, float] – A dictionary with overall and per type metrics if required. The metrics included are: accuracy, precision, recall and F1 score.
- class SeqEvalMetricsComputer(id_to_label, tagging_scheme='bilou', return_metrics_by_label=True)[source]#
An implementation of
MetricsComputerusing seqeval to compute metrics in the training of named-entity recognition components.The metrics computer can be used with a
Trainer- id_to_label:
Mapping integer value to label, it should be the same used in preprocess
- tagging_scheme:
Scheme used for tagging the tokens, it can be bilou or iob2
- return_metrics_by_label:
If True, return the metrics by label in the output dictionary. If False, only return overall metrics
Methods:
compute(all_data)Compute metrics using the tag representation collected by batches during the training/evaluation loop.
prepare_batch(model_output, input_batch)Prepare a batch of tensors to compute the metric
- prepare_batch(model_output, input_batch)[source]#
Prepare a batch of tensors to compute the metric
- Parameters
- Return type
Dict[str,List[List[str]]]- Returns
Dict[str, List[List[str]]] – A dictionary with the true and predicted tags representation of a batch data
- compute(all_data)[source]#
Compute metrics using the tag representation collected by batches during the training/evaluation loop.
- Parameters
all_data (
Dict[str,List[Any]]) – A dictionary with the true and predicted tags collected by batches- Return type
Dict[str,float]- Returns
Dict[str, float] – A dictionary with overall and per label metrics if required. The metrics included are : accuracy, precision, recall and F1 score.