Spaces:
Build error
Build error
Download src/query_expansion/mod.rs from LuckyMod/intentforge: direct link, hf CLI and curl.
- Browser
- Download file 12.3 kB
-
https://huggingface.co/spaces/LuckyMod/intentforge/resolve/main/src/query_expansion/mod.rs
- Command line
-
hf download hf://spaces/LuckyMod/intentforge/src/query_expansion/mod.rs
-
curl -L -o mod.rs https://huggingface.co/spaces/LuckyMod/intentforge/resolve/main/src/query_expansion/mod.rs
12.3 kB
| //! Query expansion module for IntentForge v2 | |
| //! | |
| //! Provides synonym, acronym, and related term expansion for search queries. | |
| //! Uses curated term lists for technology domains. | |
| use std::collections::HashMap; | |
| use std::sync::LazyLock; | |
| /// Global synonym map for tech/AI domain | |
| static SYNONYM_MAP: LazyLock<HashMap<&'static str, Vec<&'static str>>> = LazyLock::new(|| { | |
| let mut map = HashMap::new(); | |
| // Machine Learning / AI | |
| map.insert("ml", vec!["machine learning", "artificial intelligence"]); | |
| map.insert("ai", vec!["artificial intelligence", "machine learning"]); | |
| map.insert( | |
| "nlp", | |
| vec![ | |
| "natural language processing", | |
| "text analysis", | |
| "language model", | |
| ], | |
| ); | |
| map.insert( | |
| "llm", | |
| vec!["large language model", "generative ai", "chatgpt"], | |
| ); | |
| map.insert( | |
| "deep learning", | |
| vec!["neural network", "deep neural network", "CNN", "RNN"], | |
| ); | |
| map.insert( | |
| "neural network", | |
| vec!["deep learning", "neural net", "perceptron"], | |
| ); | |
| map.insert( | |
| "transformer", | |
| vec!["attention mechanism", "BERT", "GPT", "language model"], | |
| ); | |
| map.insert( | |
| "computer vision", | |
| vec!["image recognition", "object detection", "CNN"], | |
| ); | |
| map.insert( | |
| "reinforcement learning", | |
| vec!["RL", "Q-learning", "policy gradient"], | |
| ); | |
| // Programming Languages | |
| map.insert("js", vec!["javascript", "ECMAScript", "Node.js"]); | |
| map.insert("ts", vec!["typescript", "JavaScript superset"]); | |
| map.insert("py", vec!["python", "Python programming"]); | |
| map.insert("rs", vec!["rust", "Rust programming", "memory safe"]); | |
| map.insert("cpp", vec!["C++", "C plus plus"]); | |
| map.insert("cs", vec!["C#", "C sharp", "dotnet"]); | |
| map.insert("go", vec!["golang", "Go programming"]); | |
| map.insert("rb", vec!["ruby", "Ruby programming"]); | |
| map.insert("java", vec!["JVM", "Java programming"]); | |
| map.insert("kt", vec!["kotlin", "Kotlin programming", "Android"]); | |
| // Web Development | |
| map.insert( | |
| "react", | |
| vec!["React.js", "ReactJS", "frontend framework", "hooks"], | |
| ); | |
| map.insert("vue", vec!["Vue.js", "VueJS", "frontend framework"]); | |
| map.insert( | |
| "angular", | |
| vec![ | |
| "Angular.js", | |
| "AngularJS", | |
| "frontend framework", | |
| "TypeScript", | |
| ], | |
| ); | |
| map.insert( | |
| "svelte", | |
| vec!["Svelte.js", "SvelteJS", "frontend framework"], | |
| ); | |
| map.insert( | |
| "next.js", | |
| vec!["NextJS", "React framework", "SSR", "Vercel"], | |
| ); | |
| map.insert("nuxt", vec!["NuxtJS", "Vue framework", "SSR"]); | |
| map.insert( | |
| "rest", | |
| vec!["REST API", "RESTful", "HTTP API", "web service"], | |
| ); | |
| map.insert("graphql", vec!["GraphQL API", "query language", "Apollo"]); | |
| map.insert("api", vec!["API design", "web service", "endpoint", "REST"]); | |
| map.insert( | |
| "frontend", | |
| vec!["client-side", "UI", "user interface", "browser"], | |
| ); | |
| map.insert("backend", vec!["server-side", "API", "database", "server"]); | |
| map.insert( | |
| "fullstack", | |
| vec!["full stack", "frontend", "backend", "web development"], | |
| ); | |
| // Databases | |
| map.insert( | |
| "sql", | |
| vec!["SQL database", "relational database", "PostgreSQL", "MySQL"], | |
| ); | |
| map.insert( | |
| "nosql", | |
| vec!["NoSQL database", "document store", "MongoDB", "Redis"], | |
| ); | |
| map.insert("postgres", vec!["PostgreSQL", "SQL database", "relational"]); | |
| map.insert("mongo", vec!["MongoDB", "NoSQL", "document database"]); | |
| map.insert( | |
| "redis", | |
| vec!["Redis cache", "in-memory database", "key-value store"], | |
| ); | |
| map.insert( | |
| "elasticsearch", | |
| vec!["Elasticsearch", "search engine", "full-text search"], | |
| ); | |
| map.insert( | |
| "database", | |
| vec!["DB", "data store", "persistence", "SQL", "NoSQL"], | |
| ); | |
| map.insert( | |
| "indexing", | |
| vec!["database index", "query optimization", "performance"], | |
| ); | |
| // DevOps / Cloud | |
| map.insert( | |
| "docker", | |
| vec!["container", "containerization", "Docker Compose"], | |
| ); | |
| map.insert( | |
| "kubernetes", | |
| vec!["k8s", "container orchestration", "K8s cluster"], | |
| ); | |
| map.insert( | |
| "k8s", | |
| vec![ | |
| "kubernetes", | |
| "container orchestration", | |
| "pods", | |
| "deployments", | |
| ], | |
| ); | |
| map.insert( | |
| "ci/cd", | |
| vec![ | |
| "continuous integration", | |
| "continuous deployment", | |
| "pipeline", | |
| "Jenkins", | |
| "GitHub Actions", | |
| ], | |
| ); | |
| map.insert( | |
| "cicd", | |
| vec!["CI/CD", "continuous integration", "continuous deployment"], | |
| ); | |
| map.insert( | |
| "devops", | |
| vec!["CI/CD", "automation", "infrastructure", "deployment"], | |
| ); | |
| map.insert( | |
| "aws", | |
| vec!["Amazon Web Services", "cloud", "EC2", "S3", "Lambda"], | |
| ); | |
| map.insert("azure", vec!["Microsoft Azure", "cloud", "Microsoft cloud"]); | |
| map.insert( | |
| "gcp", | |
| vec!["Google Cloud Platform", "cloud", "Google cloud"], | |
| ); | |
| map.insert( | |
| "cloud", | |
| vec!["AWS", "Azure", "GCP", "cloud computing", "serverless"], | |
| ); | |
| map.insert( | |
| "terraform", | |
| vec!["infrastructure as code", "IaC", "HashiCorp"], | |
| ); | |
| map.insert( | |
| "ansible", | |
| vec!["configuration management", "automation", "DevOps"], | |
| ); | |
| // General Tech | |
| map.insert("tutorial", vec!["guide", "how-to", "learn", "beginner"]); | |
| map.insert("guide", vec!["tutorial", "how-to", "documentation"]); | |
| map.insert("how-to", vec!["tutorial", "guide", "learn", "instructions"]); | |
| map.insert( | |
| "beginner", | |
| vec!["introduction", "getting started", "basics", "fundamentals"], | |
| ); | |
| map.insert( | |
| "advanced", | |
| vec!["expert", "in-depth", "optimization", "best practices"], | |
| ); | |
| map.insert( | |
| "best practices", | |
| vec!["patterns", "guidelines", "recommendations"], | |
| ); | |
| map.insert( | |
| "optimization", | |
| vec!["performance", "speed", "efficiency", "tuning"], | |
| ); | |
| map.insert( | |
| "performance", | |
| vec!["speed", "optimization", "benchmark", "latency"], | |
| ); | |
| map.insert( | |
| "security", | |
| vec!["cybersecurity", "authentication", "encryption", "OAuth"], | |
| ); | |
| map.insert( | |
| "testing", | |
| vec!["unit test", "integration test", "TDD", "quality assurance"], | |
| ); | |
| map.insert( | |
| "debugging", | |
| vec!["troubleshooting", "bug fix", "error handling"], | |
| ); | |
| // Data Science | |
| map.insert( | |
| "data science", | |
| vec!["data analysis", "analytics", "statistics", "ML"], | |
| ); | |
| map.insert( | |
| "data analysis", | |
| vec!["data science", "statistics", "insights"], | |
| ); | |
| map.insert( | |
| "pandas", | |
| vec!["Python data", "DataFrame", "data manipulation"], | |
| ); | |
| map.insert( | |
| "numpy", | |
| vec!["Python numerical", "array", "scientific computing"], | |
| ); | |
| map.insert( | |
| "scikit-learn", | |
| vec!["sklearn", "machine learning", "Python ML"], | |
| ); | |
| map.insert("tensorflow", vec!["TF", "deep learning", "Google ML"]); | |
| map.insert("pytorch", vec!["PyTorch", "deep learning", "Facebook AI"]); | |
| map | |
| }); | |
| /// Configuration for query expansion | |
| pub struct QueryExpansionConfig { | |
| /// Maximum number of synonyms to add per term | |
| pub max_synonyms_per_term: usize, | |
| /// Minimum query length to trigger expansion | |
| pub min_query_length: usize, | |
| /// Maximum expanded query length (words) | |
| pub max_expanded_length: usize, | |
| /// Enable acronym expansion | |
| pub enable_acronyms: bool, | |
| /// Enable related terms | |
| pub enable_related: bool, | |
| } | |
| impl Default for QueryExpansionConfig { | |
| fn default() -> Self { | |
| Self { | |
| max_synonyms_per_term: 2, | |
| min_query_length: 1, | |
| max_expanded_length: 20, | |
| enable_acronyms: true, | |
| enable_related: true, | |
| } | |
| } | |
| } | |
| /// Result of query expansion | |
| pub struct ExpandedQuery { | |
| pub original: String, | |
| pub expanded: String, | |
| pub added_terms: Vec<String>, | |
| } | |
| /// Expand query with synonyms and related terms | |
| pub fn expand_query(query: &str, config: &QueryExpansionConfig) -> ExpandedQuery { | |
| let lower = query.to_lowercase(); | |
| let words: Vec<&str> = lower.split_whitespace().collect(); | |
| let mut added_terms = Vec::new(); | |
| let mut expanded_parts = Vec::new(); | |
| for word in &words { | |
| // Clean word (remove punctuation) | |
| let clean = word.trim_matches(|c: char| !c.is_alphanumeric()); | |
| if clean.is_empty() { | |
| continue; | |
| } | |
| // Check for exact match in synonym map | |
| if let Some(synonyms) = SYNONYM_MAP.get(clean) { | |
| // Add original term | |
| expanded_parts.push(clean.to_string()); | |
| // Add synonyms (limited) | |
| for synonym in synonyms.iter().take(config.max_synonyms_per_term) { | |
| if !query.to_lowercase().contains(synonym) { | |
| added_terms.push(synonym.to_string()); | |
| expanded_parts.push(synonym.to_string()); | |
| } | |
| } | |
| } else { | |
| // Check for multi-word term matches (e.g. "deep learning") | |
| let mut matched = false; | |
| for (term, synonyms) in SYNONYM_MAP.iter() { | |
| // Only match multi-word terms if the query contains the full term | |
| if term.contains(' ') && lower.contains(term) { | |
| if !added_terms.iter().any(|t| t == term) { | |
| for synonym in synonyms.iter().take(config.max_synonyms_per_term) { | |
| if !query.to_lowercase().contains(synonym) { | |
| added_terms.push(synonym.to_string()); | |
| expanded_parts.push(synonym.to_string()); | |
| } | |
| } | |
| } | |
| matched = true; | |
| break; | |
| } | |
| } | |
| // No multi-word match, keep original word | |
| if !matched && !expanded_parts.iter().any(|s| s == clean) { | |
| expanded_parts.push(clean.to_string()); | |
| } | |
| } | |
| } | |
| // Limit expanded query length | |
| expanded_parts.truncate(config.max_expanded_length); | |
| ExpandedQuery { | |
| original: query.to_string(), | |
| expanded: expanded_parts.join(" "), | |
| added_terms, | |
| } | |
| } | |
| mod tests { | |
| use super::*; | |
| fn test_expand_ml_query() { | |
| let result = expand_query("nlp tutorial", &QueryExpansionConfig::default()); | |
| assert!(result.expanded.contains("natural language processing")); | |
| assert!(result.expanded.contains("tutorial")); | |
| assert!(!result.added_terms.is_empty()); | |
| } | |
| fn test_expand_acronym() { | |
| let result = expand_query("kubernetes deployment", &QueryExpansionConfig::default()); | |
| assert!(result.expanded.contains("k8s")); | |
| assert!(result.expanded.contains("container orchestration")); | |
| } | |
| fn test_expand_javascript() { | |
| let result = expand_query("react hooks", &QueryExpansionConfig::default()); | |
| assert!(result.expanded.contains("react")); | |
| // max_synonyms_per_term=2, so only first 2 synonyms are added | |
| assert!(result.added_terms.iter().any(|t| t == "React.js")); | |
| assert!(result.added_terms.iter().any(|t| t == "ReactJS")); | |
| } | |
| fn test_no_expansion_needed() { | |
| let result = expand_query( | |
| "machine learning tutorial", | |
| &QueryExpansionConfig::default(), | |
| ); | |
| // Original terms should be preserved in expanded query | |
| assert!(result.expanded.contains("machine")); | |
| assert!(result.expanded.contains("learning")); | |
| assert!(result.expanded.contains("tutorial")); | |
| } | |
| fn test_short_query() { | |
| let result = expand_query("nlp", &QueryExpansionConfig::default()); | |
| assert!(result.expanded.contains("natural language processing")); | |
| } | |
| } | |