File size: 4,126 Bytes
3740bd7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | """
Intent Classification module for XENO Bot
Handles classification of user intents (greetings, thanks, goodbye, queries)
"""
import random
import re
from typing import List, Tuple
class IntentClassifier:
"""Classifies user intents and provides appropriate responses"""
def __init__(self):
self.intent_patterns = {
"greeting": {
"patterns": [
r"\b(hi|hello|hey|good morning|good afternoon|good evening|greetings)\b",
r"^(hi|hello|hey)[\s!.]*$",
r"\b(how are you|how do you do)\b",
],
"responses": [
"Hello! I'm XENO Assistant. How can I help you with XENO financial services today?",
"Hi there! I'm here to assist you with any questions about XENO services. What can I help you with?",
"Good day! Welcome to XENO Support. How may I assist you today?",
],
},
"thanks": {
"patterns": [
r"\b(thank you|thanks|thank u|thx|appreciate|grateful)\b",
r"^(thanks|thank you)[\s!.]*$",
r"\b(much appreciated|thanks a lot|thank you so much)\b",
],
"responses": [
"You're welcome! Is there anything else I can help you with regarding XENO services?",
"Happy to help! Feel free to ask if you have any other questions about XENO.",
"Glad I could assist you! Let me know if you need help with anything else.",
],
},
"goodbye": {
"patterns": [
r"\b(bye|goodbye|see you|farewell|take care|have a good day)\b",
r"^(bye|goodbye)[\s!.]*$",
r"\b(talk to you later|see you later|until next time)\b",
],
"responses": [
"Goodbye! Thank you for using XENO services. Have a great day!",
"Take care! Feel free to return anytime you need help with XENO services.",
"Have a wonderful day! Don't hesitate to reach out if you need assistance with XENO.",
],
},
}
def classify_intent(self, message: str, timer=None) -> Tuple[str, str]:
"""
Classify the intent of a user message
Args:
message: User's message
timer: Optional timer object for tracking
Returns:
Tuple of (intent_name, response_text)
"""
if timer:
with timer.time_step("intent_classification"):
return self._classify_intent_impl(message)
else:
return self._classify_intent_impl(message)
def _classify_intent_impl(self, message: str) -> Tuple[str, str]:
"""Internal implementation of intent classification"""
message_lower = message.lower().strip()
for intent_name, intent_data in self.intent_patterns.items():
for pattern in intent_data["patterns"]:
if re.search(pattern, message_lower, re.IGNORECASE):
response = random.choice(intent_data["responses"])
return intent_name, response
return "query", ""
def is_simple_intent(self, intent: str) -> bool:
"""
Check if the intent is a simple one that doesn't require RAG
Args:
intent: Intent name
Returns:
True if simple intent, False otherwise
"""
simple_intents = ["greeting", "thanks"]
return intent in simple_intents
def add_intent(self, intent_name: str, patterns: List[str], responses: List[str]):
"""
Add a new intent to the classifier
Args:
intent_name: Name of the intent
patterns: List of regex patterns to match
responses: List of possible responses
"""
self.intent_patterns[intent_name] = {
"patterns": patterns,
"responses": responses,
}
|