Skip to content

Instantly share code, notes, and snippets.

@jcabot
Created March 23, 2022 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcabot/cce2c1897235cd5a14db94a1d9c5b9a2 to your computer and use it in GitHub Desktop.
Save jcabot/cce2c1897235cd5a14db94a1d9c5b9a2 to your computer and use it in GitHub Desktop.
Chatbot intent prediction code
def predict(context: NLUContext, sentence: str, configuration: NlpConfiguration) -> numpy.ndarray:
prediction: numpy.ndarray
sentences = [preprocess_prediction_sentence(sentence, configuration)]
sequences = context.tokenizer.texts_to_sequences(sentences)
if configuration.discard_oov_sentences and all(i==1 for i in sequences[0]):
# the sentence to predict consists of only out of focabulary tokens so we can automatically assign a zero probability to all classes
prediction = numpy.zeros(len(context.intents))
else:
padded = tf.keras.preprocessing.sequence.pad_sequences(sequences, padding='post', maxlen=configuration.input_max_num_tokens, truncating='post')
full_prediction = context.nlp_model.predict(padded)
prediction = full_prediction[0] # We return just the a single array with the predictions as we predict for just one sentence
return prediction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment