medkit.text.metrics.ner#

This package needs extra-dependencies not installed as core dependencies of medkit. To install them, use pip install medkit-lib[metrics-ner].

Classes:

SeqEvalEvaluator([tagging_scheme, ...])

Evaluator to compute the performance of labeling tasks such as named entity recognition.

SeqEvalMetricsComputer(id_to_label[, ...])

An implementation of MetricsComputer using seqeval to compute metrics in the training of named-entity recognition components.

class SeqEvalEvaluator(tagging_scheme='bilou', return_metrics_by_label=True, average='macro', tokenizer=None, labels_remapping=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 (str, default="bilou") – Scheme for tagging the tokens, it can be bilou or iob2

  • return_metrics_by_label (bool, default=True) – If True, return the metrics by label in the output dictionary. If False, only global metrics are returned

  • average (str, default="macro") – Type of average to be performed in metrics. - macro, unweighted mean (default) - weighted, weighted average by support (number of true instances by label)

  • tokenizer (Any, optional) – Optional Fast Tokenizer to convert text into tokens. If not provided, the text is tokenized by character.

  • labels_remapping (dict of str to str, optional) – Optional remapping of labels, useful when there is a mismatch between the predicted labels and the reference labels to evaluate against. If a label (of a reference of predicted entity) is found in this dict, the corresponding value will be used as label instead.

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 of TextDocuments) – Text documents containing entities of reference

  • predicted_entities (list of list of Entity) – List of predicted entities by document

Return type:

dict[str, float]

Returns:

dict of str to float – A dictionary with average 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, average='macro')[source]#

An implementation of MetricsComputer using seqeval to compute metrics in the training of named-entity recognition components.

The metrics computer can be used with a Trainer

Parameters:
  • id_to_label (dict of int to str) – Mapping integer value to label, it should be the same used in preprocess

  • tagging_scheme (str, default="bilou") – Scheme used for tagging the tokens, it can be bilou or iob2

  • return_metrics_by_label (bool, default=True) – If True, return the metrics by label in the output dictionary. If False, only return average metrics

  • average (str, default="macro") – Type of average to be performed in metrics. - macro, unweighted mean (default) - weighted, weighted average by support (number of true instances by attr value)

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:
  • model_output (BatchData) – A batch data including the logits predicted by the model

  • input_batch (BatchData) – A batch data including the labels of reference

Return type:

dict[str, list[list[str]]]

Returns:

dict of str to list of list of 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 of str to list of Any) – A dictionary with the true and predicted tags collected by batches

Return type:

dict[str, float]

Returns:

dict of str to float – A dictionary with average and per label metrics if required. The metrics included are : accuracy, precision, recall and F1 score.