oxirs-chat 0.2.4

RAG chat API with LLM integration and natural language to SPARQL translation
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
//! Core Consciousness Model
//!
//! Main consciousness model with neural-inspired architecture for
//! context-aware response generation.

use anyhow::{Context, Result};
use chrono::Utc;
use std::collections::VecDeque;
use tracing::{debug, info, warn};

use super::super::consciousness_types::*;
use super::super::*;
use super::attention::AttentionMechanism;
use super::config::ConsciousnessModelConfig;
use super::emotional::AdvancedEmotionalState;
use super::memory::MultiLayerMemorySystem;
use super::metacognitive::EnhancedMetacognitiveLayer;
use super::metrics::{ConsciousnessMetrics, ConsciousnessSnapshot};
use super::neural::NeuralCorrelates;
use super::responses::AdvancedConsciousResponse;
use super::stream::ConsciousnessStream;

/// Advanced consciousness model with neural-inspired architecture
#[derive(Debug, Clone)]
pub struct ConsciousnessModel {
    // Core consciousness components
    pub awareness_level: f64,
    pub attention_mechanism: AttentionMechanism,
    pub multi_layer_memory: MultiLayerMemorySystem,
    pub emotional_state: AdvancedEmotionalState,
    pub metacognitive_layer: EnhancedMetacognitiveLayer,

    // Neural consciousness simulation
    pub neural_correlates: NeuralCorrelates,
    pub consciousness_stream: ConsciousnessStream,

    // Performance monitoring
    pub consciousness_metrics: ConsciousnessMetrics,
    pub state_history: VecDeque<ConsciousnessSnapshot>,

    // Configuration
    pub config: ConsciousnessModelConfig,
}

impl ConsciousnessModel {
    pub fn new() -> Result<Self> {
        Ok(Self {
            awareness_level: 0.8,
            attention_mechanism: AttentionMechanism::new()?,
            multi_layer_memory: MultiLayerMemorySystem::new()?,
            emotional_state: AdvancedEmotionalState::neutral(),
            metacognitive_layer: EnhancedMetacognitiveLayer::new()?,
            neural_correlates: NeuralCorrelates::new()?,
            consciousness_stream: ConsciousnessStream::new(),
            consciousness_metrics: ConsciousnessMetrics::new(),
            state_history: VecDeque::with_capacity(100),
            config: ConsciousnessModelConfig::default(),
        })
    }

    pub fn with_config(config: ConsciousnessModelConfig) -> Result<Self> {
        let mut model = Self::new()?;
        model.config = config;
        Ok(model)
    }

    /// Advanced consciousness processing with comprehensive error handling
    pub fn conscious_query_processing(
        &mut self,
        query: &str,
        context: &AssembledContext,
    ) -> Result<AdvancedConsciousResponse> {
        let processing_start = std::time::Instant::now();

        // Input validation
        if query.is_empty() {
            return Err(anyhow::anyhow!(
                "Empty query provided to consciousness processing"
            ));
        }

        // Create consciousness snapshot before processing
        self.capture_consciousness_snapshot()?;

        // 1. Neural consciousness simulation
        debug!(
            "Starting neural consciousness simulation for query: {}",
            query.chars().take(50).collect::<String>()
        );
        let neural_activation = self
            .neural_correlates
            .process_input(query, context)
            .context("Failed to process neural consciousness simulation")?;

        // 2. Update consciousness stream
        self.consciousness_stream
            .add_experience(query, context, &neural_activation)
            .context("Failed to update consciousness stream")?;

        // 3. Advanced awareness computation
        let awareness_update = self
            .compute_advanced_awareness(query, context)
            .context("Failed to compute advanced awareness")?;
        self.awareness_level =
            (self.awareness_level * 0.7 + awareness_update * 0.3).clamp(0.0, 1.0);

        // 4. Sophisticated attention mechanism
        let attention_allocation = self
            .attention_mechanism
            .allocate_attention(query, context, &neural_activation)
            .context("Failed to allocate attention")?;

        // 5. Multi-layer memory processing
        let memory_integration = self
            .multi_layer_memory
            .process_and_integrate(query, context, &attention_allocation)
            .context("Failed to process multi-layer memory")?;

        // 6. Advanced emotional processing
        let emotional_response = self
            .emotional_state
            .process_emotional_content(query, context)
            .context("Failed to process emotional content")?;

        // 7. Enhanced metacognitive assessment
        let metacognitive_result = self
            .metacognitive_layer
            .comprehensive_assessment(query, context, &neural_activation)
            .context("Failed to perform metacognitive assessment")?;

        // 8. Generate sophisticated insights
        let enhanced_insights = self
            .generate_advanced_insights(query, context, &neural_activation, &memory_integration)
            .context("Failed to generate advanced insights")?;

        // 9. Update consciousness metrics
        self.consciousness_metrics.update(
            self.awareness_level,
            &attention_allocation,
            &memory_integration,
            &emotional_response,
            processing_start.elapsed(),
        )?;

        // 10. Construct advanced response
        let response = AdvancedConsciousResponse {
            base_response: context.clone(),
            consciousness_metadata: AdvancedConsciousnessMetadata {
                awareness_level: self.awareness_level,
                neural_activation: neural_activation.clone(),
                attention_allocation: attention_allocation.clone(),
                memory_integration: memory_integration.clone(),
                emotional_response: emotional_response.clone(),
                metacognitive_result: metacognitive_result.clone(),
                processing_time: processing_start.elapsed(),
                consciousness_health_score: self.calculate_consciousness_health()?,
            },
            enhanced_insights,
            consciousness_stream_context: self.consciousness_stream.get_recent_context(5),
        };

        info!(
            "Consciousness processing completed successfully in {:?}",
            processing_start.elapsed()
        );
        Ok(response)
    }

    /// Advanced awareness computation with multiple cognitive factors
    fn compute_advanced_awareness(&self, query: &str, context: &AssembledContext) -> Result<f64> {
        let query_complexity = self.calculate_enhanced_query_complexity(query)?;
        let context_richness = self.calculate_context_richness(context)?;
        let information_density = self.calculate_information_density(query, context)?;
        let cognitive_load = self.calculate_cognitive_load(query, context)?;

        // Sophisticated awareness calculation using multiple factors
        let raw_awareness = (
            query_complexity * 0.25
                + context_richness * 0.25
                + information_density * 0.25
                + (1.0 - cognitive_load) * 0.25
            // Inverse cognitive load
        )
        .clamp(0.0, 1.0);

        // Apply consciousness stream influence
        let stream_influence = self.consciousness_stream.get_awareness_influence()?;
        let final_awareness = (raw_awareness * 0.8 + stream_influence * 0.2).clamp(0.0, 1.0);

        debug!("Advanced awareness computed: complexity={:.3}, richness={:.3}, density={:.3}, load={:.3}, final={:.3}", 
               query_complexity, context_richness, information_density, cognitive_load, final_awareness);

        Ok(final_awareness)
    }

    /// Enhanced query complexity analysis with linguistic and semantic factors
    fn calculate_enhanced_query_complexity(&self, query: &str) -> Result<f64> {
        if query.is_empty() {
            return Ok(0.0);
        }

        let words: Vec<&str> = query.split_whitespace().collect();
        let word_count = words.len();
        let unique_words = words.iter().collect::<HashSet<_>>().len();
        let avg_word_length =
            words.iter().map(|w| w.len()).sum::<usize>() as f64 / word_count.max(1) as f64;

        // Linguistic complexity factors
        let syntactic_complexity = self.calculate_syntactic_complexity(query)?;
        let semantic_depth = self.calculate_semantic_depth(query)?;
        let question_complexity = self.analyze_question_complexity(query)?;

        let complexity = ((word_count as f64 * 0.02).min(1.0) * 0.2
            + (unique_words as f64 / word_count.max(1) as f64) * 0.2
            + (avg_word_length / 10.0).min(1.0) * 0.15
            + syntactic_complexity * 0.2
            + semantic_depth * 0.15
            + question_complexity * 0.1)
            .clamp(0.0, 1.0);

        Ok(complexity)
    }

    fn calculate_syntactic_complexity(&self, query: &str) -> Result<f64> {
        let clause_markers = [
            "because", "although", "while", "since", "unless", "if", "when",
        ];
        let subordination_score = clause_markers
            .iter()
            .filter(|marker| query.to_lowercase().contains(*marker))
            .count() as f64
            * 0.2;

        let punctuation_complexity = query
            .chars()
            .filter(|c| ['?', '!', ';', ':', ','].contains(c))
            .count() as f64
            * 0.1;

        Ok((subordination_score + punctuation_complexity).min(1.0))
    }

    fn calculate_semantic_depth(&self, query: &str) -> Result<f64> {
        let abstract_concepts = [
            "concept",
            "idea",
            "theory",
            "principle",
            "philosophy",
            "meaning",
            "essence",
        ];
        let depth_indicators = [
            "analyze",
            "compare",
            "evaluate",
            "synthesize",
            "critique",
            "explain",
        ];

        let abstract_score = abstract_concepts
            .iter()
            .filter(|concept| query.to_lowercase().contains(*concept))
            .count() as f64
            * 0.15;

        let depth_score = depth_indicators
            .iter()
            .filter(|indicator| query.to_lowercase().contains(*indicator))
            .count() as f64
            * 0.2;

        Ok((abstract_score + depth_score).min(1.0))
    }

    fn analyze_question_complexity(&self, query: &str) -> Result<f64> {
        let simple_patterns = ["what is", "who is", "when did", "where is"];
        let complex_patterns = ["how does", "why might", "what if", "compare"];
        let very_complex_patterns = [
            "analyze the relationship",
            "evaluate the impact",
            "synthesize",
        ];

        let query_lower = query.to_lowercase();

        if very_complex_patterns
            .iter()
            .any(|pattern| query_lower.contains(pattern))
        {
            Ok(1.0)
        } else if complex_patterns
            .iter()
            .any(|pattern| query_lower.contains(pattern))
        {
            Ok(0.7)
        } else if simple_patterns
            .iter()
            .any(|pattern| query_lower.contains(pattern))
        {
            Ok(0.3)
        } else {
            Ok(0.5) // Default moderate complexity
        }
    }

    fn calculate_context_richness(&self, context: &AssembledContext) -> Result<f64> {
        let mut richness = 0.0;
        let mut components = 0;

        // Semantic results richness
        if !context.semantic_results.is_empty() {
            let avg_score = context
                .semantic_results
                .iter()
                .map(|r| r.score as f64)
                .sum::<f64>()
                / context.semantic_results.len() as f64;
            richness += avg_score * 0.3;
            components += 1;
        }

        // Graph results richness
        if !context.graph_results.is_empty() {
            let graph_diversity = context.graph_results.len().min(10) as f64 / 10.0;
            richness += graph_diversity * 0.2;
            components += 1;
        }

        // Entity extraction richness
        if !context.extracted_entities.is_empty() {
            let entity_confidence = context
                .extracted_entities
                .iter()
                .map(|e| e.confidence as f64)
                .sum::<f64>()
                / context.extracted_entities.len() as f64;
            richness += entity_confidence * 0.3;
            components += 1;
        }

        // Quantum results (if available)
        if let Some(ref quantum_results) = context.quantum_results {
            if !quantum_results.is_empty() {
                richness += 0.2;
                components += 1;
            }
        }

        if components > 0 {
            Ok(richness / components as f64)
        } else {
            Ok(0.0)
        }
    }

    fn calculate_information_density(
        &self,
        query: &str,
        context: &AssembledContext,
    ) -> Result<f64> {
        let query_entropy = self.calculate_entropy(query)?;
        let context_information = context.semantic_results.len() + context.graph_results.len();
        let information_ratio = (context_information as f64 / query.len().max(1) as f64).min(1.0);

        Ok((query_entropy + information_ratio) / 2.0)
    }

    fn calculate_entropy(&self, text: &str) -> Result<f64> {
        if text.is_empty() {
            return Ok(0.0);
        }

        let mut char_counts = HashMap::new();
        let total_chars = text.chars().count() as f64;

        for ch in text.chars() {
            *char_counts.entry(ch).or_insert(0) += 1;
        }

        let entropy = char_counts
            .values()
            .map(|&count| {
                let probability = count as f64 / total_chars;
                -probability * probability.log2()
            })
            .sum::<f64>();

        Ok(entropy / 8.0) // Normalize to 0-1 range
    }

    fn calculate_cognitive_load(&self, query: &str, context: &AssembledContext) -> Result<f64> {
        let query_load = (query.len() as f64 / 1000.0).min(1.0);
        let context_load = ((context.semantic_results.len() + context.graph_results.len()) as f64
            / 100.0)
            .min(1.0);
        let entity_load = (context.extracted_entities.len() as f64 / 50.0).min(1.0);

        Ok((query_load + context_load + entity_load) / 3.0)
    }

    fn capture_consciousness_snapshot(&mut self) -> Result<()> {
        let snapshot = ConsciousnessSnapshot {
            timestamp: Utc::now(),
            awareness_level: self.awareness_level,
            attention_weights: self.attention_mechanism.get_current_weights()?,
            emotional_state: self.emotional_state.get_current_state()?,
            memory_pressure: self.multi_layer_memory.get_memory_pressure()?,
            neural_activity: self.neural_correlates.get_activity_summary()?,
        };

        self.state_history.push_back(snapshot);

        // Maintain history limit
        if self.state_history.len() > 100 {
            self.state_history.pop_front();
        }

        Ok(())
    }

    fn calculate_consciousness_health(&self) -> Result<f64> {
        let awareness_health = if self.awareness_level >= 0.7 {
            1.0
        } else {
            self.awareness_level / 0.7
        };
        let attention_health = self.attention_mechanism.get_health_score()?;
        let memory_health = self.multi_layer_memory.get_health_score()?;
        let emotional_health = self.emotional_state.get_stability_score()?;

        Ok((awareness_health + attention_health + memory_health + emotional_health) / 4.0)
    }

    fn generate_advanced_insights(
        &self,
        _query: &str,
        _context: &AssembledContext,
        neural_activation: &NeuralActivation,
        memory_integration: &MemoryIntegrationResult,
    ) -> Result<Vec<AdvancedConsciousInsight>> {
        let mut insights = Vec::new();

        // Neural pattern insight
        if neural_activation.overall_activation > 0.7 {
            insights.push(AdvancedConsciousInsight {
                insight_type: AdvancedInsightType::NeuralPattern,
                content: format!(
                    "High neural activation detected: {:.3}",
                    neural_activation.overall_activation
                ),
                confidence: neural_activation.confidence,
                implications: vec![
                    "Query activates multiple cognitive networks".to_string(),
                    "Suggests complex conceptual processing".to_string(),
                ],
                supporting_evidence: neural_activation.get_evidence(),
                consciousness_correlation: neural_activation.consciousness_relevance,
            });
        }

        // Memory integration insight
        if memory_integration.integration_strength > 0.6 {
            insights.push(AdvancedConsciousInsight {
                insight_type: AdvancedInsightType::MemoryIntegration,
                content: format!(
                    "Strong memory integration: {:.3}",
                    memory_integration.integration_strength
                ),
                confidence: memory_integration.confidence,
                implications: vec![
                    "Leveraging rich episodic memory traces".to_string(),
                    "Cross-temporal pattern recognition active".to_string(),
                ],
                supporting_evidence: memory_integration.get_evidence(),
                consciousness_correlation: memory_integration.consciousness_relevance,
            });
        }

        // Attention distribution insight
        let attention_entropy = self.attention_mechanism.calculate_attention_entropy()?;
        if attention_entropy < 0.3 {
            insights.push(AdvancedConsciousInsight {
                insight_type: AdvancedInsightType::AttentionFocus,
                content: format!("Highly focused attention: entropy={attention_entropy:.3}"),
                confidence: 0.8,
                implications: vec![
                    "Concentrated cognitive resources".to_string(),
                    "Single-domain expertise engagement".to_string(),
                ],
                supporting_evidence: vec![format!("Attention entropy: {:.3}", attention_entropy)],
                consciousness_correlation: 0.9 - attention_entropy,
            });
        }

        // Consciousness stream insight
        let stream_coherence = self.consciousness_stream.calculate_coherence()?;
        if stream_coherence > 0.8 {
            insights.push(AdvancedConsciousInsight {
                insight_type: AdvancedInsightType::StreamCoherence,
                content: format!("High consciousness stream coherence: {stream_coherence:.3}"),
                confidence: stream_coherence,
                implications: vec![
                    "Stable consciousness state maintained".to_string(),
                    "Consistent cognitive processing patterns".to_string(),
                ],
                supporting_evidence: vec![format!("Stream coherence: {:.3}", stream_coherence)],
                consciousness_correlation: stream_coherence,
            });
        }

        debug!(
            "Generated {} advanced consciousness insights",
            insights.len()
        );
        Ok(insights)
    }

    fn update_attention_focus(&mut self, query: &str, context: &AssembledContext) -> Result<()> {
        // Use the attention mechanism to update focus
        let _ = self.attention_mechanism.allocate_attention(
            query,
            context,
            &NeuralActivation::default(),
        )?;
        Ok(())
    }

    fn create_memory_trace(&mut self, query: &str, context: &AssembledContext) {
        let trace = MemoryTrace {
            id: Uuid::new_v4().to_string(),
            query: query.to_string(),
            context_summary: format!(
                "Context with {} triples",
                context.retrieved_triples.as_ref().map_or(0, |t| t.len())
            ),
            timestamp: Utc::now(),
            importance_score: self.calculate_importance(query, context),
            emotional_valence: self.emotional_state.valence,
        };

        // Store trace in multi-layer memory system
        if let Err(e) = self
            .multi_layer_memory
            .store_episodic_memory(&trace.query, &trace.context_summary)
        {
            warn!("Failed to store memory trace: {}", e);
        }
    }

    fn calculate_memory_integration(&self) -> f64 {
        // Simplified implementation - would need public accessor methods on EpisodicMemory
        // to properly calculate memory integration based on episode importance scores
        0.5 // Default memory integration score
    }

    fn generate_conscious_insights(
        &self,
        query: &str,
        _context: &AssembledContext,
    ) -> Vec<ConsciousInsight> {
        let mut insights = Vec::new();

        // Pattern recognition insight
        if let Some(pattern) = self.detect_query_pattern(query) {
            insights.push(ConsciousInsight {
                insight_type: InsightType::PatternRecognition,
                content: format!("Detected pattern: {pattern}"),
                confidence: 0.8,
                implications: vec!["This suggests a systematic approach to analysis".to_string()],
            });
        }

        // Emotional resonance insight
        let emotional_resonance = self.emotional_state.calculate_resonance(query);
        if emotional_resonance > 0.5 {
            insights.push(ConsciousInsight {
                insight_type: InsightType::EmotionalResonance,
                content: format!("High emotional resonance detected: {emotional_resonance:.2}"),
                confidence: emotional_resonance,
                implications: vec!["Consider emotional context in response".to_string()],
            });
        }

        // Memory integration insight
        let memory_integration = self.calculate_memory_integration();
        if memory_integration > 0.7 {
            insights.push(ConsciousInsight {
                insight_type: InsightType::MemoryIntegration,
                content: format!("Strong memory integration: {memory_integration:.2}"),
                confidence: memory_integration,
                implications: vec!["Can leverage previous interaction patterns".to_string()],
            });
        }

        insights
    }

    fn calculate_query_complexity(&self, query: &str) -> f64 {
        let word_count = query.split_whitespace().count();
        let unique_words = query.split_whitespace().collect::<HashSet<_>>().len();
        let avg_word_length = query.split_whitespace().map(|w| w.len()).sum::<usize>() as f64
            / word_count.max(1) as f64;

        (word_count as f64 * 0.1 + unique_words as f64 * 0.2 + avg_word_length * 0.1).min(1.0)
    }

    fn calculate_importance(&self, query: &str, context: &AssembledContext) -> f64 {
        let query_complexity = self.calculate_query_complexity(query);
        let context_richness = context
            .retrieved_triples
            .as_ref()
            .map_or(0.0, |t| t.len() as f64 * 0.1);

        (query_complexity + context_richness).min(1.0)
    }

    fn detect_query_pattern(&self, query: &str) -> Option<String> {
        let query_lower = query.to_lowercase();

        if query_lower.contains("how") && query_lower.contains("many") {
            Some("Quantitative Analysis".to_string())
        } else if query_lower.contains("what") && query_lower.contains("is") {
            Some("Definitional Query".to_string())
        } else if query_lower.contains("why") {
            Some("Causal Reasoning".to_string())
        } else if query_lower.contains("compare") || query_lower.contains("difference") {
            Some("Comparative Analysis".to_string())
        } else {
            None
        }
    }
}

#[derive(Debug, Clone)]
pub struct MemoryTrace {
    pub id: String,
    pub query: String,
    pub context_summary: String,
    pub timestamp: DateTime<Utc>,
    pub importance_score: f64,
    pub emotional_valence: f64,
}

#[derive(Debug, Clone)]
pub struct EmotionalState {
    pub valence: f64,   // -1.0 (negative) to 1.0 (positive)
    pub arousal: f64,   // 0.0 (calm) to 1.0 (excited)
    pub dominance: f64, // 0.0 (submissive) to 1.0 (dominant)
}

impl EmotionalState {
    pub fn neutral() -> Self {
        Self {
            valence: 0.0,
            arousal: 0.5,
            dominance: 0.5,
        }
    }

    pub fn calculate_resonance(&self, query: &str) -> f64 {
        let emotional_words = [
            "excited",
            "happy",
            "sad",
            "angry",
            "frustrated",
            "pleased",
            "worried",
            "confident",
        ];

        let query_lower = query.to_lowercase();
        let emotional_content = emotional_words
            .iter()
            .filter(|word| query_lower.contains(*word))
            .count() as f64
            / emotional_words.len() as f64;

        (self.valence.abs() + self.arousal + emotional_content) / 3.0
    }
}