#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IntentClass {
Conversational,
Research,
Implementation,
}
const ACTION_VERBS: &[&str] = &[
"write",
"create",
"build",
"run",
"fix",
"implement",
"add",
"update",
"delete",
"test",
"deploy",
"generate",
"show",
"list",
"find",
"search",
"refactor",
"remove",
"rename",
"install",
"compile",
"debug",
"check",
];
const RESEARCH_VERBS: &[&str] = &[
"explain",
"analyze",
"analyse",
"investigate",
"review",
"examine",
"explore",
"describe",
"summarize",
"summarise",
"understand",
"diagnose",
"audit",
"assess",
"evaluate",
"compare",
];
const QUESTION_WORDS: &[&str] = &[
"what", "why", "how", "when", "where", "which", "who", "whose", "whom", "does", "is", "are",
"can", "could", "would", "should",
];
const GREETINGS: &[&str] = &[
"hello",
"hi",
"hey",
"howdy",
"greetings",
"sup",
"yo",
"good morning",
"good afternoon",
"good evening",
"hey there",
"hi there",
"hello there",
];
const CLOSINGS: &[&str] = &[
"bye",
"goodbye",
"thanks",
"thank you",
"cheers",
"later",
"see ya",
"see you",
"ok thanks",
"thx",
"ty",
];
const SELF_QUESTIONS: &[&str] = &[
"how are you",
"what are you",
"who are you",
"what can you do",
"what is trusty-code",
"what is trusty code",
"what do you do",
"what's your name",
"whats your name",
"are you there",
"you there",
];
fn normalize(input: &str) -> String {
let mut out = String::with_capacity(input.len());
for ch in input.chars() {
if ch.is_alphanumeric() || ch == '\'' || ch == '-' || ch == '_' || ch.is_whitespace() {
for low in ch.to_lowercase() {
out.push(low);
}
} else {
out.push(' ');
}
}
out.split_whitespace().collect::<Vec<_>>().join(" ")
}
pub fn classify_intent(input: &str) -> IntentClass {
let trimmed = input.trim();
if trimmed.is_empty() {
return IntentClass::Conversational;
}
if trimmed.starts_with('/') {
return IntentClass::Implementation;
}
let normalized = normalize(trimmed);
if normalized.is_empty() {
return IntentClass::Conversational;
}
if GREETINGS.iter().any(|g| &normalized == g) {
return IntentClass::Conversational;
}
if CLOSINGS.iter().any(|c| &normalized == c) {
return IntentClass::Conversational;
}
if SELF_QUESTIONS.iter().any(|q| &normalized == q) {
return IntentClass::Conversational;
}
let words: Vec<&str> = normalized.split_whitespace().collect();
let word_count = words.len();
let has_action_verb = words.iter().any(|w| ACTION_VERBS.contains(w));
let has_research_verb = words.iter().any(|w| RESEARCH_VERBS.contains(w));
let starts_with_question_word = words
.first()
.map(|w| QUESTION_WORDS.contains(w))
.unwrap_or(false);
let ends_with_question_mark = trimmed.ends_with('?');
if !has_action_verb && !has_research_verb && !starts_with_question_word {
for g in GREETINGS {
if normalized.starts_with(g)
&& (normalized.len() == g.len()
|| normalized.as_bytes().get(g.len()) == Some(&b' '))
&& word_count <= 6
{
return IntentClass::Conversational;
}
}
for c in CLOSINGS {
if normalized.starts_with(c)
&& (normalized.len() == c.len()
|| normalized.as_bytes().get(c.len()) == Some(&b' '))
&& word_count <= 6
{
return IntentClass::Conversational;
}
}
}
if has_action_verb {
return IntentClass::Implementation;
}
if starts_with_question_word || has_research_verb {
return IntentClass::Research;
}
if ends_with_question_mark && word_count <= 15 {
return IntentClass::Research;
}
if normalized.starts_with("help me ") || normalized == "help me" {
return IntentClass::Implementation;
}
if word_count <= 4 {
return IntentClass::Conversational;
}
if word_count > 10 {
return IntentClass::Implementation;
}
IntentClass::Conversational
}
#[cfg(test)]
#[path = "classifier_tests.rs"]
mod classifier_tests;
#[cfg(test)]
#[path = "classifier_tests_2.rs"]
mod classifier_tests_2;
#[cfg(test)]
#[path = "classifier_property_tests.rs"]
mod classifier_property_tests;
#[cfg(test)]
#[path = "unit_tests.rs"]
mod tests;