graphrag-core 0.2.0

Core portable library for GraphRAG - works on native and WASM
Documentation
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
//! Intent classification for ROGRAG system
//!
//! Classifies query intent and determines whether the system should attempt
//! to answer the query or refuse to answer based on confidence and appropriateness.

#[cfg(feature = "rograg")]
use crate::Result;
#[cfg(feature = "rograg")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "rograg")]
use std::collections::HashMap;
#[cfg(feature = "rograg")]
use strum::{Display as StrumDisplay, EnumString};
#[cfg(feature = "rograg")]
use thiserror::Error;

/// Errors that can occur during intent classification.
#[cfg(feature = "rograg")]
#[derive(Error, Debug)]
pub enum IntentClassificationError {
    /// Unable to determine the query's intent from available patterns.
    ///
    /// Occurs when the query doesn't match any known intent patterns and
    /// cannot be reliably classified. Consider query reformulation.
    #[error("Unable to classify query intent: {query}")]
    CannotClassify {
        /// The query text that could not be classified.
        query: String,
    },

    /// Multiple intents detected with similar confidence scores.
    ///
    /// Occurs when the query could be interpreted in multiple ways with
    /// similar likelihood. The query should be clarified or rephrased.
    #[error("Ambiguous intent detected: {intents:?}")]
    AmbiguousIntent {
        /// List of competing intents with similar confidence scores.
        intents: Vec<QueryIntent>,
    },

    /// Classification confidence below acceptable threshold.
    ///
    /// Occurs when the best intent match has low confidence (<0.7 by default).
    /// The system may refuse to answer or suggest query reformulation.
    #[error("Insufficient confidence for classification: {confidence}")]
    InsufficientConfidence {
        /// The confidence score that was below the threshold.
        confidence: f32,
    },
}

/// Classification of query intent types.
///
/// Each intent type represents a different information-seeking goal and may
/// require different retrieval and generation strategies.
#[cfg(feature = "rograg")]
#[derive(Debug, Clone, PartialEq, Eq, Hash, StrumDisplay, EnumString, Serialize, Deserialize)]
pub enum QueryIntent {
    /// Factual information requests about specific attributes or facts.
    ///
    /// Examples: "What is X?", "How many Y are there?"
    Factual,

    /// Requests for definitions, explanations, or conceptual understanding.
    ///
    /// Examples: "Define X", "Explain what Y means"
    Definitional,

    /// Requests about relationships or connections between entities.
    ///
    /// Examples: "How are X and Y related?", "What is the connection between A and B?"
    Relational,

    /// Temporal information requests about timing, sequence, or duration.
    ///
    /// Examples: "When did X happen?", "What year was Y?"
    Temporal,

    /// Causal information requests about reasons, causes, or effects.
    ///
    /// Examples: "Why did X happen?", "What caused Y?"
    Causal,

    /// Comparative analysis requests between two or more entities.
    ///
    /// Examples: "Compare X and Y", "What's the difference between A and B?"
    Comparative,

    /// Exploratory or open-ended questions requiring broader analysis.
    ///
    /// Examples: General questions without clear structure
    Exploratory,

    /// Requests for summaries, overviews, or high-level descriptions.
    ///
    /// Examples: "Summarize X", "Tell me about Y", "Give an overview of Z"
    Summary,

    /// Inappropriate or harmful requests that should be refused.
    ///
    /// Examples: Queries containing hate speech, violence, illegal content
    Inappropriate,

    /// Ambiguous or unclear requests that cannot be reliably interpreted.
    ///
    /// Occurs when intent is unclear or multiple intents have similar confidence.
    Ambiguous,
}

/// Result of classifying a query's intent.
///
/// Contains the primary intent, confidence scores, and guidance on whether
/// the system should refuse to answer.
#[cfg(feature = "rograg")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IntentResult {
    /// The primary classified intent with highest confidence.
    pub primary_intent: QueryIntent,

    /// Secondary intents with their confidence scores.
    ///
    /// Useful for detecting multi-intent queries or ambiguous cases.
    pub secondary_intents: Vec<(QueryIntent, f32)>,

    /// Confidence in the primary intent classification (0.0 to 1.0).
    ///
    /// Higher values indicate more reliable classification.
    pub confidence: f32,

    /// Whether the system should refuse to answer this query.
    ///
    /// Set to `true` for inappropriate content, low confidence, or ambiguity.
    pub should_refuse: bool,

    /// Reason for refusal, if `should_refuse` is true.
    pub refusal_reason: Option<String>,

    /// Suggested query reformulation for better results.
    ///
    /// Provided when confidence is low or intent is ambiguous.
    pub suggested_reformulation: Option<String>,

    /// Query complexity score (0.0 to 1.0).
    ///
    /// Based on length, structure, and linguistic complexity.
    pub complexity_score: f32,
}

#[cfg(feature = "rograg")]
impl Default for IntentResult {
    fn default() -> Self {
        Self {
            primary_intent: QueryIntent::Exploratory,
            secondary_intents: vec![],
            confidence: 0.0,
            should_refuse: false,
            refusal_reason: None,
            suggested_reformulation: None,
            complexity_score: 0.0,
        }
    }
}

/// Configuration for intent classification behavior.
///
/// Controls thresholds, detection features, and reformulation suggestions.
#[cfg(feature = "rograg")]
#[derive(Debug, Clone)]
pub struct IntentClassificationConfig {
    /// Minimum confidence required for classification (0.0 to 1.0).
    ///
    /// Default: 0.7. Classifications below this threshold are considered unreliable.
    pub confidence_threshold: f32,

    /// Threshold for refusing to answer (0.0 to 1.0).
    ///
    /// Default: 0.8. Queries with confidence below this threshold trigger refusal.
    pub refusal_threshold: f32,

    /// Whether to detect and refuse inappropriate content.
    ///
    /// Default: true. When enabled, scans for hate speech, violence, etc.
    pub enable_inappropriate_detection: bool,

    /// Whether to detect ambiguous intent.
    ///
    /// Default: true. When enabled, identifies queries with multiple competing intents.
    pub enable_ambiguity_detection: bool,

    /// Whether to suggest query reformulations.
    ///
    /// Default: true. When enabled, provides suggestions for improving unclear queries.
    pub suggest_reformulations: bool,
}

#[cfg(feature = "rograg")]
impl Default for IntentClassificationConfig {
    fn default() -> Self {
        Self {
            confidence_threshold: 0.7,
            refusal_threshold: 0.8,
            enable_inappropriate_detection: true,
            enable_ambiguity_detection: true,
            suggest_reformulations: true,
        }
    }
}

/// Intent classifier for query analysis and refusal detection.
///
/// Uses pattern matching (keywords and regex) to classify query intent and
/// determine whether queries should be answered or refused. Supports detection
/// of inappropriate content, ambiguous queries, and provides reformulation suggestions.
///
/// # Classification Process
///
/// 1. Check for inappropriate content (if enabled)
/// 2. Score query against each intent pattern
/// 3. Detect ambiguity if secondary intents have high confidence
/// 4. Determine if query should be refused based on thresholds
/// 5. Generate reformulation suggestions if needed
///
/// # Supported Intents
///
/// - Factual, Definitional, Relational
/// - Temporal, Causal, Comparative
/// - Exploratory, Summary
/// - Inappropriate, Ambiguous (special cases)
///
/// # Example
///
/// ```rust,ignore
/// use graphrag_core::rograg::IntentClassifier;
///
/// let classifier = IntentClassifier::new()?;
/// let result = classifier.classify("What is X?")?;
///
/// if result.should_refuse {
///     println!("Refusing: {}", result.refusal_reason.unwrap());
/// } else {
///     println!("Intent: {:?}", result.primary_intent);
/// }
/// ```
#[cfg(feature = "rograg")]
pub struct IntentClassifier {
    config: IntentClassificationConfig,
    intent_patterns: HashMap<QueryIntent, Vec<IntentPattern>>,
    inappropriate_patterns: Vec<regex::Regex>,
}

#[cfg(feature = "rograg")]
#[derive(Debug, Clone)]
struct IntentPattern {
    keywords: Vec<String>,
    patterns: Vec<regex::Regex>,
    weight: f32,
    requires_all: bool, // If true, all keywords must be present
}

#[cfg(feature = "rograg")]
impl IntentClassifier {
    /// Create a new intent classifier with default configuration.
    ///
    /// Initializes pattern databases for all supported intent types.
    ///
    /// # Returns
    ///
    /// Returns an `IntentClassifier` with default thresholds and all detection
    /// features enabled, or an error if pattern initialization fails.
    ///
    /// # Errors
    ///
    /// Returns an error if regex pattern compilation fails during initialization.
    pub fn new() -> Result<Self> {
        let config = IntentClassificationConfig::default();
        let mut classifier = Self {
            config,
            intent_patterns: HashMap::new(),
            inappropriate_patterns: vec![],
        };

        classifier.initialize_patterns()?;
        Ok(classifier)
    }

    /// Create a new intent classifier with custom configuration.
    ///
    /// # Arguments
    ///
    /// * `config` - Custom configuration for classification behavior
    ///
    /// # Returns
    ///
    /// Returns an `IntentClassifier` with the specified configuration, or an
    /// error if pattern initialization fails.
    ///
    /// # Errors
    ///
    /// Returns an error if regex pattern compilation fails during initialization.
    pub fn with_config(config: IntentClassificationConfig) -> Result<Self> {
        let mut classifier = Self {
            config,
            intent_patterns: HashMap::new(),
            inappropriate_patterns: vec![],
        };

        classifier.initialize_patterns()?;
        Ok(classifier)
    }

    /// Initialize intent patterns
    fn initialize_patterns(&mut self) -> Result<()> {
        // Factual intent patterns
        self.add_intent_pattern(
            QueryIntent::Factual,
            IntentPattern {
                keywords: ["what", "which", "how many", "how much"]
                    .iter()
                    .map(|s| s.to_string())
                    .collect(),
                patterns: vec![
                    regex::Regex::new(r"\bwhat (?:is|are|was|were)\b")?,
                    regex::Regex::new(r"\bwhich (?:is|are|was|were)\b")?,
                    regex::Regex::new(r"\bhow many\b")?,
                    regex::Regex::new(r"\bhow much\b")?,
                ],
                weight: 1.0,
                requires_all: false,
            },
        );

        // Definitional intent patterns
        self.add_intent_pattern(
            QueryIntent::Definitional,
            IntentPattern {
                keywords: ["define", "definition", "meaning", "explain", "what is"]
                    .iter()
                    .map(|s| s.to_string())
                    .collect(),
                patterns: vec![
                    regex::Regex::new(r"\bdefine\b")?,
                    regex::Regex::new(r"\bdefinition of\b")?,
                    regex::Regex::new(r"\bmeaning of\b")?,
                    regex::Regex::new(r"\bexplain what\b")?,
                    regex::Regex::new(r"\bwhat (?:is|are) (?:the )?(?:concept|idea|notion) of\b")?,
                ],
                weight: 1.0,
                requires_all: false,
            },
        );

        // Relational intent patterns
        self.add_intent_pattern(
            QueryIntent::Relational,
            IntentPattern {
                keywords: ["relationship", "related", "connection", "between", "and"]
                    .iter()
                    .map(|s| s.to_string())
                    .collect(),
                patterns: vec![
                    regex::Regex::new(r"\brelationship between\b")?,
                    regex::Regex::new(r"\bhow (?:is|are) .+ related to\b")?,
                    regex::Regex::new(r"\bconnection between\b")?,
                    regex::Regex::new(r"\b\w+ and \w+\b")?, // Simple pattern for "X and Y"
                ],
                weight: 1.0,
                requires_all: false,
            },
        );

        // Temporal intent patterns
        self.add_intent_pattern(
            QueryIntent::Temporal,
            IntentPattern {
                keywords: ["when", "time", "date", "year", "before", "after", "during"]
                    .iter()
                    .map(|s| s.to_string())
                    .collect(),
                patterns: vec![
                    regex::Regex::new(r"\bwhen (?:did|was|were|will|is|are)\b")?,
                    regex::Regex::new(r"\bwhat (?:time|date|year)\b")?,
                    regex::Regex::new(r"\bbefore .+ happened\b")?,
                    regex::Regex::new(r"\bafter .+ happened\b")?,
                    regex::Regex::new(r"\bduring .+ period\b")?,
                ],
                weight: 1.0,
                requires_all: false,
            },
        );

        // Causal intent patterns
        self.add_intent_pattern(
            QueryIntent::Causal,
            IntentPattern {
                keywords: ["why", "because", "cause", "reason", "result", "due to"]
                    .iter()
                    .map(|s| s.to_string())
                    .collect(),
                patterns: vec![
                    regex::Regex::new(r"\bwhy (?:did|was|were|is|are|do|does)\b")?,
                    regex::Regex::new(r"\bwhat (?:caused|causes)\b")?,
                    regex::Regex::new(r"\breason for\b")?,
                    regex::Regex::new(r"\bdue to what\b")?,
                    regex::Regex::new(r"\bwhat led to\b")?,
                ],
                weight: 1.0,
                requires_all: false,
            },
        );

        // Comparative intent patterns
        self.add_intent_pattern(
            QueryIntent::Comparative,
            IntentPattern {
                keywords: [
                    "compare",
                    "difference",
                    "versus",
                    "vs",
                    "better",
                    "worse",
                    "similar",
                ]
                .iter()
                .map(|s| s.to_string())
                .collect(),
                patterns: vec![
                    regex::Regex::new(r"\bcompare .+ (?:to|with|and)\b")?,
                    regex::Regex::new(r"\bdifference between\b")?,
                    regex::Regex::new(r"\b.+ (?:versus|vs) .+\b")?,
                    regex::Regex::new(r"\bwhich is (?:better|worse)\b")?,
                    regex::Regex::new(r"\bhow (?:similar|different)\b")?,
                ],
                weight: 1.0,
                requires_all: false,
            },
        );

        // Summary intent patterns
        self.add_intent_pattern(
            QueryIntent::Summary,
            IntentPattern {
                keywords: [
                    "summarize",
                    "overview",
                    "summary",
                    "tell me about",
                    "describe",
                ]
                .iter()
                .map(|s| s.to_string())
                .collect(),
                patterns: vec![
                    regex::Regex::new(r"\bsummarize\b")?,
                    regex::Regex::new(r"\bgive (?:me )?(?:an )?overview\b")?,
                    regex::Regex::new(r"\btell me about\b")?,
                    regex::Regex::new(r"\bdescribe .+\b")?,
                    regex::Regex::new(r"\bwhat (?:can you tell me )?about\b")?,
                ],
                weight: 1.0,
                requires_all: false,
            },
        );

        // Initialize inappropriate content patterns
        if self.config.enable_inappropriate_detection {
            self.inappropriate_patterns = vec![
                regex::Regex::new(r"\b(?:hate|violence|harm|illegal|inappropriate)\b")?,
                // Add more patterns as needed
            ];
        }

        Ok(())
    }

    /// Add an intent pattern
    fn add_intent_pattern(&mut self, intent: QueryIntent, pattern: IntentPattern) {
        self.intent_patterns
            .entry(intent)
            .or_default()
            .push(pattern);
    }

    /// Classify the intent of a query.
    ///
    /// Analyzes the query to determine its primary intent, detect ambiguity,
    /// identify inappropriate content, and decide whether to refuse answering.
    ///
    /// # Arguments
    ///
    /// * `query` - The query text to classify
    ///
    /// # Returns
    ///
    /// Returns an `IntentResult` containing:
    /// - Primary and secondary intents with confidence scores
    /// - Whether to refuse answering
    /// - Refusal reason (if applicable)
    /// - Suggested reformulation (if configured)
    /// - Query complexity score
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let result = classifier.classify("What is entity X?")?;
    /// println!("Intent: {:?}, Confidence: {:.2}",
    ///          result.primary_intent, result.confidence);
    /// ```
    pub fn classify(&self, query: &str) -> Result<IntentResult> {
        let query_lower = query.to_lowercase();

        // Check for inappropriate content first
        if self.config.enable_inappropriate_detection && self.is_inappropriate(&query_lower) {
            return Ok(IntentResult {
                primary_intent: QueryIntent::Inappropriate,
                secondary_intents: vec![],
                confidence: 1.0,
                should_refuse: true,
                refusal_reason: Some("Query contains inappropriate content".to_string()),
                suggested_reformulation: None,
                complexity_score: 0.0,
            });
        }

        // Calculate scores for each intent
        let mut intent_scores: HashMap<QueryIntent, f32> = HashMap::new();

        for (intent, patterns) in &self.intent_patterns {
            let score = self.calculate_intent_score(&query_lower, patterns);
            if score > 0.0 {
                intent_scores.insert(intent.clone(), score);
            }
        }

        // Determine primary and secondary intents
        let mut sorted_intents: Vec<(QueryIntent, f32)> = intent_scores.into_iter().collect();
        sorted_intents.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        if sorted_intents.is_empty() {
            return Ok(IntentResult {
                primary_intent: QueryIntent::Ambiguous,
                secondary_intents: vec![],
                confidence: 0.0,
                should_refuse: true,
                refusal_reason: Some("Unable to understand the query intent".to_string()),
                suggested_reformulation: self.suggest_reformulation(query),
                complexity_score: self.calculate_complexity(&query_lower),
            });
        }

        let (primary_intent, primary_score) = sorted_intents[0].clone();
        let secondary_intents: Vec<(QueryIntent, f32)> =
            sorted_intents.into_iter().skip(1).take(2).collect();

        // Check for ambiguity
        let is_ambiguous = if self.config.enable_ambiguity_detection {
            secondary_intents
                .iter()
                .any(|(_, score)| *score > primary_score * 0.8)
        } else {
            false
        };

        let final_intent = if is_ambiguous {
            QueryIntent::Ambiguous
        } else {
            primary_intent
        };

        let should_refuse = primary_score < self.config.refusal_threshold || is_ambiguous;

        let refusal_reason = if should_refuse {
            if is_ambiguous {
                Some("Query intent is ambiguous - please be more specific".to_string())
            } else {
                Some("Insufficient confidence in understanding the query".to_string())
            }
        } else {
            None
        };

        Ok(IntentResult {
            primary_intent: final_intent,
            secondary_intents,
            confidence: primary_score,
            should_refuse,
            refusal_reason,
            suggested_reformulation: if should_refuse && self.config.suggest_reformulations {
                self.suggest_reformulation(query)
            } else {
                None
            },
            complexity_score: self.calculate_complexity(&query_lower),
        })
    }

    /// Calculate score for a specific intent
    fn calculate_intent_score(&self, query: &str, patterns: &[IntentPattern]) -> f32 {
        let mut total_score = 0.0;

        for pattern in patterns {
            let mut pattern_score = 0.0;

            // Check keyword matches
            let keyword_matches = pattern
                .keywords
                .iter()
                .filter(|keyword| query.contains(&keyword.to_lowercase()))
                .count();

            if pattern.requires_all && keyword_matches != pattern.keywords.len() {
                continue; // Skip if all keywords are required but not all are present
            }

            if keyword_matches > 0 {
                pattern_score += (keyword_matches as f32 / pattern.keywords.len() as f32) * 0.5;
            }

            // Check regex pattern matches
            let regex_matches = pattern
                .patterns
                .iter()
                .filter(|regex| regex.is_match(query))
                .count();

            if regex_matches > 0 {
                pattern_score += (regex_matches as f32 / pattern.patterns.len() as f32) * 0.5;
            }

            total_score += pattern_score * pattern.weight;
        }

        total_score.min(1.0) // Cap at 1.0
    }

    /// Check if query contains inappropriate content
    fn is_inappropriate(&self, query: &str) -> bool {
        self.inappropriate_patterns
            .iter()
            .any(|pattern| pattern.is_match(query))
    }

    /// Calculate query complexity
    fn calculate_complexity(&self, query: &str) -> f32 {
        let word_count = query.split_whitespace().count();
        let sentence_count = query
            .chars()
            .filter(|&c| c == '.' || c == '?' || c == '!')
            .count()
            .max(1);
        let avg_word_length =
            query.chars().filter(|c| c.is_alphabetic()).count() as f32 / word_count.max(1) as f32;

        // Complexity factors
        let length_complexity = (word_count as f32 / 20.0).min(1.0); // Normalize to 20 words
        let sentence_complexity = (sentence_count as f32 / 3.0).min(1.0); // Normalize to 3 sentences
        let word_length_complexity = (avg_word_length / 8.0).min(1.0); // Normalize to 8 chars per word

        // Check for complex constructs
        let has_conjunctions =
            query.contains(" and ") || query.contains(" or ") || query.contains(" but ");
        let has_subordination = query.contains(" because ")
            || query.contains(" since ")
            || query.contains(" although ");
        let construct_complexity = if has_conjunctions || has_subordination {
            0.3
        } else {
            0.0
        };

        (length_complexity * 0.3
            + sentence_complexity * 0.2
            + word_length_complexity * 0.2
            + construct_complexity)
            .min(1.0)
    }

    /// Suggest query reformulation
    fn suggest_reformulation(&self, query: &str) -> Option<String> {
        if !self.config.suggest_reformulations {
            return None;
        }

        let query_lower = query.to_lowercase();

        // Suggest more specific reformulations based on common patterns
        if query_lower.starts_with("tell me about") {
            Some(
                "Try asking a more specific question like 'What is...?' or 'How does...?'"
                    .to_string(),
            )
        } else if query_lower.contains(" and ") {
            Some(
                "Try breaking your question into separate parts or focus on one aspect".to_string(),
            )
        } else if query.split_whitespace().count() > 20 {
            Some("Try using a shorter, more focused question".to_string())
        } else if !query.ends_with('?') && !query.ends_with('.') && !query.ends_with('!') {
            Some("Try phrasing your request as a clear question".to_string())
        } else {
            Some("Try being more specific about what information you're looking for".to_string())
        }
    }

    /// Get configuration
    pub fn get_config(&self) -> &IntentClassificationConfig {
        &self.config
    }

    /// Update configuration
    pub fn update_config(&mut self, config: IntentClassificationConfig) -> Result<()> {
        let old_inappropriate_detection = self.config.enable_inappropriate_detection;
        self.config = config;
        // Re-initialize patterns if needed
        if self.config.enable_inappropriate_detection != old_inappropriate_detection {
            self.initialize_patterns()?;
        }
        Ok(())
    }

    /// Get intent statistics
    pub fn get_statistics(&self) -> IntentClassificationStats {
        let total_patterns = self
            .intent_patterns
            .values()
            .map(|patterns| patterns.len())
            .sum();

        IntentClassificationStats {
            supported_intents: self.intent_patterns.keys().cloned().collect(),
            total_patterns,
            inappropriate_patterns: self.inappropriate_patterns.len(),
            confidence_threshold: self.config.confidence_threshold,
            refusal_threshold: self.config.refusal_threshold,
        }
    }
}

/// Statistics about intent classification capabilities and configuration.
///
/// Provides a snapshot of the classifier's capabilities, thresholds, and
/// pattern coverage for monitoring and debugging purposes.
#[cfg(feature = "rograg")]
#[derive(Debug)]
pub struct IntentClassificationStats {
    /// List of query intents supported by the classifier.
    pub supported_intents: Vec<QueryIntent>,

    /// Total number of patterns across all intents.
    pub total_patterns: usize,

    /// Number of patterns for detecting inappropriate content.
    pub inappropriate_patterns: usize,

    /// Minimum confidence threshold for classification (0.0 to 1.0).
    pub confidence_threshold: f32,

    /// Threshold for refusing to answer queries (0.0 to 1.0).
    pub refusal_threshold: f32,
}

#[cfg(feature = "rograg")]
impl IntentClassificationStats {
    /// Print statistics to the log using structured logging.
    ///
    /// Outputs intent count, pattern counts, and thresholds at INFO level.
    pub fn print(&self) {
        tracing::info!(
            supported_intents = self.supported_intents.len(),
            total_patterns = self.total_patterns,
            inappropriate_patterns = self.inappropriate_patterns,
            confidence_threshold = format!("{:.2}", self.confidence_threshold),
            refusal_threshold = format!("{:.2}", self.refusal_threshold),
            "Intent classification statistics"
        );
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(feature = "rograg")]
    #[test]
    fn test_ambiguous_query() {
        let classifier = IntentClassifier::new().unwrap();
        let result = classifier.classify("something unclear").unwrap();

        // Should either be ambiguous or have low confidence
        assert!(result.primary_intent == QueryIntent::Ambiguous || result.confidence < 0.5);
    }

    #[cfg(feature = "rograg")]
    #[test]
    fn test_complexity_calculation() {
        let classifier = IntentClassifier::new().unwrap();

        let simple_result = classifier.classify("What is Tom?").unwrap();
        let complex_result = classifier.classify("What is the intricate relationship between Entity Name and Second Entity, and how does it evolve throughout their various adventures and escapades?").unwrap();

        assert!(complex_result.complexity_score > simple_result.complexity_score);
    }

    #[cfg(feature = "rograg")]
    #[test]
    fn test_reformulation_suggestions() {
        let config = IntentClassificationConfig {
            suggest_reformulations: true,
            refusal_threshold: 0.9, // High threshold to trigger suggestions
            ..Default::default()
        };

        let classifier = IntentClassifier::with_config(config).unwrap();
        let result = classifier.classify("tell me about stuff").unwrap();

        assert!(result.suggested_reformulation.is_some());
    }
}