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.
- 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:
- model
NumpyViterbiInference 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.
- model
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:
- path
Path Path to model to load.
- path
- 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:
- Returns:
floatMarginal probability of given label at given position.
- Raises:
ValueErrorRaised 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:
- Returns:
- Raises:
ValueErrorRaised 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_features
list[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.
- sentence_features
- Returns: