Classify the intent of a user query Args: query: User's natural language query Returns: Tuple of (intent, confidence_score)
(self, query: str)
| 57 | } |
| 58 | |
| 59 | def classify_intent(self, query: str) -> Tuple[str, float]: |
| 60 | """ |
| 61 | Classify the intent of a user query |
| 62 | |
| 63 | Args: |
| 64 | query: User's natural language query |
| 65 | |
| 66 | Returns: |
| 67 | Tuple of (intent, confidence_score) |
| 68 | """ |
| 69 | query_lower = query.lower() |
| 70 | |
| 71 | intent_scores = {} |
| 72 | |
| 73 | # Calculate score for each intent |
| 74 | for intent, patterns in self.INTENT_PATTERNS.items(): |
| 75 | score = 0 |
| 76 | matches = 0 |
| 77 | |
| 78 | for pattern in patterns: |
| 79 | if re.search(pattern, query_lower): |
| 80 | matches += 1 |
| 81 | score += 1 |
| 82 | |
| 83 | if matches > 0: |
| 84 | # Normalize by number of patterns |
| 85 | intent_scores[intent] = score / len(patterns) |
| 86 | |
| 87 | # If no matches, default to general_query |
| 88 | if not intent_scores: |
| 89 | return ('general_query', 0.5) |
| 90 | |
| 91 | # Get intent with highest score |
| 92 | best_intent = max(intent_scores.items(), key=lambda x: x[1]) |
| 93 | |
| 94 | logger.info(f"Classified intent: {best_intent[0]} (confidence: {best_intent[1]:.2f})") |
| 95 | |
| 96 | return best_intent |
| 97 | |
| 98 | def extract_entities(self, query: str) -> Dict[str, List[str]]: |
| 99 | """ |