Created
March 23, 2022 15:24
-
-
Save jcabot/cce2c1897235cd5a14db94a1d9c5b9a2 to your computer and use it in GitHub Desktop.
Chatbot intent prediction code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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