Penelope v0.5.0 Penelope.NLP.IntentClassifier View Source

The intent classifier transforms a natural language utterance into a named intent and a set of named parameters. It uses an ML classifier to infer the intent name and an entity recognizer to extract named entities as parameters. These components are both represented as ML pipelines.

The intent classifier also maintains a tokenizer pipeline for converting utterances into a list of tokens. This pipeline is executed first, and its results are run through the classifier/recognizer pipelines.

Classification results are returned as a tuple of , where intents is the map of names to classification probabilities, and parameters is a name->value map. Intent names, parameter names and parameter values are all strings. Probabilities are all floats that should sum to 1.

Example: pipeline = %{

tokenizer: [{:ptb_tokenizer, []}],
classifier: [{:count_vectorizer, []},
             {:linear_classifier, [c: 2.0, probability?: true]}],
recognizer: [{:crf_tagger, []}],

} x = [

"you have four pears",
"three hundred apples would be a lot"

] y = [

{"intent_1", ["o", "o", "b_count", "b_fruit"]},
{"intent_2", ["b_count", "i_count", "b_fruit", "o", "o", "o", "o"]}

] classifier = Penelope.NLP.IntentClassifier.fit(%{}, x, y, pipeline)

{intents, params} = Penelope.NLP.IntentClassifier.predict_intent(

classifier,
%{},
"I have three bananas"

)

Link to this section Summary

Functions

imports parameters from a serialized model

exports a runtime model to a serializable data structure

fits the tokenizer, classifier, and recognizer models

predicts an intent and its parameters from an utterance string

Link to this section Types

Link to this type model() View Source
model() :: %{
  tokenizer: [{atom(), any()}],
  detokenizer: [{atom(), any()}],
  classifier: [{atom(), any()}],
  recognizer: [{atom(), any()}]
}

Link to this section Functions

Link to this function compile(params) View Source
compile(params :: map()) :: model()

imports parameters from a serialized model

Link to this function export(model) View Source
export(model :: model()) :: map()

exports a runtime model to a serializable data structure

Link to this function fit(context, x, y, pipelines) View Source
fit(
  context :: map(),
  x :: [utterance :: String.t()],
  y :: [{intent :: String.t(), tags :: [String.t()]}],
  pipelines :: [
    tokenizer: [{String.t() | atom(), any()}],
    classifier: [{String.t() | atom(), any()}],
    recognizer: [{String.t() | atom(), any()}]
  ]
) :: model()

fits the tokenizer, classifier, and recognizer models

Link to this function predict_intent(model, context, x) View Source
predict_intent(model :: model(), context :: map(), x :: String.t()) ::
  {intents :: %{required(intent :: String.t()) => probability :: float()},
   params :: %{required(name :: String.t()) => value :: String.t()}}

predicts an intent and its parameters from an utterance string