medcat.utils.ner.model

Module Contents

Classes

NerModel

The NER model.

class medcat.utils.ner.model.NerModel(cat)

The NER model.

This wraps a CAT instance and simplifies its use as a NER model.

It provies methods for creating one from a TransformersNER as well as loading from a model pack (along with some validation).

It also exposes some useful parts of the CAT it wraps such as the config and the concept database.

Parameters:

cat (medcat.cat.CAT) –

property config: medcat.config.Config
Return type:

medcat.config.Config

property cdb: medcat.cdb.CDB
Return type:

medcat.cdb.CDB

__init__(cat)
Parameters:

cat (medcat.cat.CAT) –

Return type:

None

train(json_path, train_nr=0, *args, **kwargs)

Train the underlying transformers NER model.

All the extra arguments are passed to the TransformersNER train method.

Parameters:
  • json_path (Union[str, list, None]) – The JSON file path to read the training data from.

  • train_nr (int) – The number of the NER object in cat._addl_train to train. Defaults to 0.

  • *args – Additional arguments for TransformersNER.train .

  • **kwargs – Additional keyword arguments for TransformersNER.train .

Returns:

Tuple[Any, Any, Any] – df, examples, dataset

Return type:

Tuple[Any, Any, Any]

__call__(text, *args, **kwargs)

Get the annotated document for text.

Undefined arguments and keyword arguments get passed on to the equivalent CAT method.

Parameters:
  • text (Optional[str]) – The input text.

  • *args – Additional arguments for cat.__call__ .

  • **kwargs – Additional keyword arguments for cat.__call__ .

Returns:

Optional[Doc] – The annotated document.

Return type:

Optional[spacy.tokens.Doc]

get_entities(text, *args, **kwargs)

Gets the entities recognized within a given text.

The output format is identical to CAT.get_entities.

Undefined arguments and keyword arguments get passed on to CAT.get_entities.

Parameters:
  • text (str) – The input text.

  • *args – Additional arguments for cat.get_entities .

  • **kwargs – Additional keyword arguments for cat.get_entities .

Returns:

dict – The output entities.

Return type:

dict

classmethod create(ner)

Create a NER model with a TransformersNER

Parameters:

ner (Union[TransformersNER, List[TransformersNER]]) – The TransformersNER instance(s).

Returns:

NerModel – The resulting model

Return type:

NerModel

classmethod load_model_pack(model_pack_path, config=None)

Load NER model from model pack.

The method first wraps the loaded CAT instance.

Parameters:
  • config (Optional[Dict]) – Config for DeId model pack (primarily for stride of overlap window)

  • model_pack_path (str) – The model pack path.

Returns:

NerModel – The resulting DeI model.

Return type:

NerModel