Inference#

class ingredient_parser.inference.AlternativeSequence(labels, scores)[source]#

Methods

count(value, /)

Return number of occurrences of value.

index(value[, start, stop])

Return first index of value.

labels: list[str]#

Alias for field number 0

scores: list[float]#

Alias for field number 1

class ingredient_parser.inference.NumpyCRFInference(model_file: Path, combined_name_labels: bool = False)[source]#

Class to performance inference using trained CRF model for ingredient sentence labelling.

Attributes:
modelNumpyViterbiInference

Implementation of Viterbi for inference.

combined_name_labelsbool

Set to True if there only a single NAME label present in the weights. Set to False otherwise.

Methods

load(path)

Load saved model at given path.

marginal(label, position)

Return the probability of label, label, at position, position, for the most recent sequence passed to predict_sequence.

marginal_with_exclusions(position, ...)

Return the label with the highest marginal that is not one of the excluded labels.

tag_from_features(sentence_features[, ...])

Tag a sentence with labels using model.

load(path: Path) → None[source]#

Load saved model at given path.

Parameters:
pathPath

Path to model to load.

marginal(label: str, position: int) → float[source]#

Return the probability of label, label, at position, position, for the most recent sequence passed to predict_sequence.

Parameters:
labelstr

Label at position.

indexint

Position in sequence.

Returns:
float

Marginal probability of given label at given position.

Raises:
ValueError

Raised if marginals matrix does not exist.

marginal_with_exclusions(position: int, excluded_labels: set[str]) → tuple[str, float][source]#

Return the label with the highest marginal that is not one of the excluded labels.

Parameters:
positionint

Position in sequence.

excluded_labelsset[str]

Set of labels to exclude when finding highest marginal.

Returns:
tuple[str, float]

Label of highest marginal at given position. Value of highest marginal probability at given position.

Raises:
ValueError

Raised if marginals matrix does not exist. Raised if all labels have been excluded. Raised if marginal cannot be found.

tag_from_features(sentence_features: list[dict[str, str | bool]], expect_name_in_output: bool = True, constrain_transitions: bool = True) → list[tuple[str, float]][source]#

Tag a sentence with labels using model.

This function accepts a list of features for each token, rather than calculating the features from the tokens.

If self.combined_name_labels=True, then we cannot apply label transition constraints. In this case, constrain_transitions is forced to False.

Parameters:
sentence_featureslist[FeatureDict]

List of feature dicts for each token.

expect_name_in_outputbool, optional

If True and the model doesn’t label any words in the sentence as the name, fallback to selecting the most likely name from any token even though the model gives it a different label. Note that this does guarantee the output contains a name. Default is True.

constrain_transitionsbool, optional

If True, constrain label transitions to prevent certain invalid label sequences. Default is True.

Returns:
list[tuple[str, float]]

List of (label, confidence) tuples.