import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
# Sample training data for intents
train_texts = [
'Set a reminder for meeting at 3pm',
'Remind me to call mom',
'What is the weather today?',
'Tell me a joke',
'Play some music',
'Turn off the lights',
'Schedule a dentist appointment',
'How is the traffic to work?'
]
train_labels = [
'set_reminder',
'set_reminder',
'get_weather',
'tell_joke',
'play_music',
'control_lights',
'set_appointment',
'get_traffic'
]
# Train intent classifier pipeline
intent_clf = make_pipeline(TfidfVectorizer(), LogisticRegression(max_iter=200))
intent_clf.fit(train_texts, train_labels)
# Function to predict intent with confidence
def predict_intent(text):
probs = intent_clf.predict_proba([text])[0]
max_prob = np.max(probs)
intent = intent_clf.classes_[np.argmax(probs)]
if max_prob < 0.6:
return 'clarify', max_prob
return intent, max_prob
# Example usage
user_inputs = [
'Remind me about the meeting',
'Can you play music?',
'Is it going to rain?',
'Turn on the lights please',
'Book a dentist appointment for next week',
'Tell me something funny',
'I want to know traffic conditions'
]
for text in user_inputs:
intent, confidence = predict_intent(text)
if intent == 'clarify':
print(f"I'm not sure what you mean. Could you please clarify?")
else:
print(f"Intent: {intent}, Confidence: {confidence:.2f}")