torsh-text 0.1.2

Natural language processing utilities for ToRSh deep learning framework
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
//! Semantic Context Analysis Module
//!
//! This module provides comprehensive contextual analysis capabilities for semantic
//! fluency evaluation. It focuses on how semantic meaning evolves and maintains
//! coherence within different contextual frameworks.

use super::config::ContextAnalysisConfig;
use super::results::{
    ContextEvolutionPattern, ContextualAdaptation, ContextualCoherence, ContextualSemanticMetrics,
    ContextualTransition, SemanticContext,
};
use crate::error::TextAnalysisError;
use serde::{Deserialize, Serialize};
use std::collections::hash_map::DefaultHasher;
use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
use std::hash::{Hash, Hasher};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum ContextAnalysisError {
    #[error("Invalid context analysis configuration: {0}")]
    ConfigError(String),
    #[error("Context calculation failed: {0}")]
    CalculationError(String),
    #[error("Semantic context analysis error: {0}")]
    AnalysisError(String),
}

pub type ContextResult<T> = Result<T, ContextAnalysisError>;

/// Advanced semantic context analyzer for contextual fluency evaluation
#[derive(Debug, Clone)]
pub struct SemanticContextAnalyzer {
    config: ContextAnalysisConfig,
    context_windows: VecDeque<SemanticContext>,
    contextual_mappings: HashMap<String, Vec<SemanticContext>>,
    transition_patterns: Vec<ContextualTransition>,
    context_cache: HashMap<u64, ContextualSemanticMetrics>,
    adaptation_tracker: BTreeMap<usize, ContextualAdaptation>,
    evolution_patterns: Vec<ContextEvolutionPattern>,
    coherence_tracker: HashMap<String, f64>,
}

impl SemanticContextAnalyzer {
    /// Create new context analyzer with configuration
    pub fn new(config: ContextAnalysisConfig) -> ContextResult<Self> {
        Self::validate_config(&config)?;

        Ok(Self {
            config,
            context_windows: VecDeque::new(),
            contextual_mappings: HashMap::new(),
            transition_patterns: Vec::new(),
            context_cache: HashMap::new(),
            adaptation_tracker: BTreeMap::new(),
            evolution_patterns: Vec::new(),
            coherence_tracker: HashMap::new(),
        })
    }

    /// Analyze contextual semantic metrics for text
    pub fn analyze_contextual_semantics(
        &mut self,
        text: &str,
        context_metadata: Option<&HashMap<String, String>>,
    ) -> ContextResult<ContextualSemanticMetrics> {
        let cache_key = self.generate_cache_key(text, context_metadata);
        if let Some(cached) = self.context_cache.get(&cache_key) {
            return Ok(cached.clone());
        }

        let sentences = self.extract_sentences(text);
        let mut metrics = ContextualSemanticMetrics::default();

        // Build semantic contexts for analysis
        let contexts = self.build_semantic_contexts(&sentences, context_metadata)?;

        // Core contextual analysis
        metrics.overall_contextual_score = self.calculate_overall_contextual_score(&contexts)?;
        metrics.context_preservation_score = self.analyze_context_preservation(&contexts)?;
        metrics.contextual_adaptation_score = self.evaluate_contextual_adaptation(&contexts)?;
        metrics.semantic_context_coherence = self.calculate_contextual_coherence(&contexts)?;

        // Advanced contextual features
        if self.config.analyze_advanced_context {
            metrics.contextual_transitions = self.analyze_contextual_transitions(&contexts)?;
            metrics.adaptation_patterns = self.identify_adaptation_patterns(&contexts)?;
            metrics.evolution_patterns = self.detect_evolution_patterns(&contexts)?;
            metrics.contextual_coherence_analysis = self.perform_coherence_analysis(&contexts)?;
        }

        // Multi-scale context analysis
        if self.config.multi_scale_analysis {
            metrics.local_context_metrics = self.analyze_local_context(&contexts)?;
            metrics.global_context_metrics = self.analyze_global_context(&contexts)?;
            metrics.hierarchical_context_metrics = self.analyze_hierarchical_context(&contexts)?;
        }

        // Context sensitivity analysis
        if self.config.analyze_context_sensitivity {
            metrics.sensitivity_measures = self.calculate_context_sensitivity(&contexts)?;
            metrics.adaptation_flexibility = self.measure_adaptation_flexibility(&contexts)?;
        }

        // Cache results for performance
        self.context_cache.insert(cache_key, metrics.clone());

        Ok(metrics)
    }

    /// Build semantic contexts from sentences
    fn build_semantic_contexts(
        &mut self,
        sentences: &[String],
        metadata: Option<&HashMap<String, String>>,
    ) -> ContextResult<Vec<SemanticContext>> {
        let mut contexts = Vec::new();
        let window_size = self.config.context_window_size;

        for (i, sentence) in sentences.iter().enumerate() {
            // Determine context window boundaries
            let start_idx = i.saturating_sub(window_size / 2);
            let end_idx = (i + window_size / 2 + 1).min(sentences.len());

            // Build context from surrounding sentences
            let preceding_context = if start_idx < i {
                sentences[start_idx..i].to_vec()
            } else {
                vec![]
            };

            let following_context = if i + 1 < end_idx {
                sentences[i + 1..end_idx].to_vec()
            } else {
                vec![]
            };

            // Extract semantic features from context
            let context = SemanticContext {
                sentence_index: i,
                focal_sentence: sentence.clone(),
                preceding_context,
                following_context,
                semantic_features: self
                    .extract_contextual_features(sentence, &sentences[start_idx..end_idx])?,
                context_strength: self.calculate_context_strength(i, sentences)?,
                contextual_keywords: self
                    .identify_contextual_keywords(sentence, &sentences[start_idx..end_idx])?,
                semantic_roles: self
                    .analyze_semantic_roles(sentence, &sentences[start_idx..end_idx])?,
                contextual_coherence: self
                    .measure_local_coherence(sentence, &sentences[start_idx..end_idx])?,
                adaptation_indicators: self
                    .identify_adaptation_indicators(sentence, &sentences[start_idx..end_idx])?,
                metadata: metadata.cloned().unwrap_or_default(),
            };

            contexts.push(context);
        }

        // Update internal tracking
        self.update_context_tracking(&contexts)?;

        Ok(contexts)
    }

    /// Calculate overall contextual score
    fn calculate_overall_contextual_score(
        &self,
        contexts: &[SemanticContext],
    ) -> ContextResult<f64> {
        if contexts.is_empty() {
            return Ok(0.0);
        }

        let mut scores = Vec::new();

        for context in contexts {
            let context_score = self.evaluate_single_context(context)?;
            scores.push(context_score);
        }

        // Weight by context strength if enabled
        let weighted_score = if self.config.weight_by_context_strength {
            self.calculate_strength_weighted_score(&scores, contexts)?
        } else {
            scores.iter().sum::<f64>() / scores.len() as f64
        };

        Ok(weighted_score.max(0.0).min(1.0))
    }

    /// Analyze context preservation across text
    fn analyze_context_preservation(&self, contexts: &[SemanticContext]) -> ContextResult<f64> {
        if contexts.len() < 2 {
            return Ok(1.0);
        }

        let mut preservation_scores = Vec::new();

        for i in 1..contexts.len() {
            let preservation =
                self.calculate_context_preservation(&contexts[i - 1], &contexts[i])?;
            preservation_scores.push(preservation);
        }

        Ok(preservation_scores.iter().sum::<f64>() / preservation_scores.len() as f64)
    }

    /// Evaluate contextual adaptation quality
    fn evaluate_contextual_adaptation(
        &mut self,
        contexts: &[SemanticContext],
    ) -> ContextResult<f64> {
        let mut adaptation_scores = Vec::new();

        for (i, context) in contexts.iter().enumerate() {
            let adaptation_quality = self.assess_adaptation_quality(context, i)?;
            adaptation_scores.push(adaptation_quality);

            // Track adaptation patterns
            if adaptation_quality != 0.0 {
                let adaptation = ContextualAdaptation {
                    position: i,
                    adaptation_type: self.classify_adaptation_type(context)?,
                    strength: adaptation_quality,
                    triggers: self.identify_adaptation_triggers(context)?,
                    semantic_shift: self.measure_semantic_shift(context)?,
                };

                self.adaptation_tracker.insert(i, adaptation);
            }
        }

        Ok(adaptation_scores.iter().sum::<f64>() / adaptation_scores.len() as f64)
    }

    /// Calculate contextual coherence score
    fn calculate_contextual_coherence(
        &mut self,
        contexts: &[SemanticContext],
    ) -> ContextResult<f64> {
        let mut coherence_measures = Vec::new();

        // Local coherence analysis
        for context in contexts {
            let local_coherence = self.measure_local_contextual_coherence(context)?;
            coherence_measures.push(local_coherence);

            // Update coherence tracking
            let context_key = format!("context_{}", context.sentence_index);
            self.coherence_tracker.insert(context_key, local_coherence);
        }

        // Global coherence analysis
        let global_coherence = self.measure_global_contextual_coherence(contexts)?;

        // Combine local and global measures
        let local_avg = coherence_measures.iter().sum::<f64>() / coherence_measures.len() as f64;
        let combined_coherence = (local_avg * self.config.local_coherence_weight
            + global_coherence * self.config.global_coherence_weight)
            / (self.config.local_coherence_weight + self.config.global_coherence_weight);

        Ok(combined_coherence)
    }

    /// Analyze contextual transitions between contexts
    fn analyze_contextual_transitions(
        &mut self,
        contexts: &[SemanticContext],
    ) -> ContextResult<Vec<ContextualTransition>> {
        let mut transitions = Vec::new();

        if contexts.len() < 2 {
            return Ok(transitions);
        }

        for i in 1..contexts.len() {
            let transition = self.analyze_single_transition(&contexts[i - 1], &contexts[i])?;
            transitions.push(transition);
        }

        // Store for pattern analysis
        self.transition_patterns = transitions.clone();

        Ok(transitions)
    }

    /// Identify contextual adaptation patterns
    fn identify_adaptation_patterns(
        &self,
        contexts: &[SemanticContext],
    ) -> ContextResult<Vec<ContextualAdaptation>> {
        let mut patterns = Vec::new();

        // Look for systematic adaptation patterns
        let window_size = self.config.adaptation_window_size;

        for window_start in 0..=(contexts.len().saturating_sub(window_size)) {
            let window = &contexts[window_start..window_start + window_size];

            if let Some(pattern) = self.detect_adaptation_pattern(window)? {
                patterns.push(pattern);
            }
        }

        Ok(patterns)
    }

    /// Detect context evolution patterns
    fn detect_evolution_patterns(
        &mut self,
        contexts: &[SemanticContext],
    ) -> ContextResult<Vec<ContextEvolutionPattern>> {
        let mut evolution_patterns = Vec::new();

        if contexts.len() < self.config.min_evolution_window {
            return Ok(evolution_patterns);
        }

        // Analyze semantic evolution over time
        let evolution_windows = self.create_evolution_windows(contexts)?;

        for window in evolution_windows {
            let pattern = self.analyze_evolution_window(&window)?;
            evolution_patterns.push(pattern);
        }

        // Store for future reference
        self.evolution_patterns = evolution_patterns.clone();

        Ok(evolution_patterns)
    }

    /// Perform comprehensive coherence analysis
    fn perform_coherence_analysis(
        &self,
        contexts: &[SemanticContext],
    ) -> ContextResult<ContextualCoherence> {
        let inter_context_coherence = self.analyze_inter_context_coherence(contexts)?;
        let intra_context_coherence = self.analyze_intra_context_coherence(contexts)?;
        let temporal_coherence = self.analyze_temporal_coherence(contexts)?;
        let thematic_coherence = self.analyze_thematic_coherence(contexts)?;

        Ok(ContextualCoherence {
            inter_context_coherence,
            intra_context_coherence,
            temporal_coherence,
            thematic_coherence,
            overall_coherence: self.calculate_overall_coherence(
                inter_context_coherence,
                intra_context_coherence,
                temporal_coherence,
                thematic_coherence,
            )?,
            coherence_stability: self.measure_coherence_stability(contexts)?,
            coherence_breakdown_points: self.identify_coherence_breakdowns(contexts)?,
        })
    }

    // Helper methods for context analysis

    fn validate_config(config: &ContextAnalysisConfig) -> ContextResult<()> {
        if config.context_window_size < 1 {
            return Err(ContextAnalysisError::ConfigError(
                "context_window_size must be at least 1".to_string(),
            ));
        }

        if config.local_coherence_weight < 0.0 || config.global_coherence_weight < 0.0 {
            return Err(ContextAnalysisError::ConfigError(
                "coherence weights must be non-negative".to_string(),
            ));
        }

        Ok(())
    }

    fn extract_sentences(&self, text: &str) -> Vec<String> {
        text.split(&self.config.sentence_delimiters)
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty())
            .collect()
    }

    fn generate_cache_key(&self, text: &str, metadata: Option<&HashMap<String, String>>) -> u64 {
        let mut hasher = DefaultHasher::new();
        text.hash(&mut hasher);
        if let Some(meta) = metadata {
            for (k, v) in meta {
                k.hash(&mut hasher);
                v.hash(&mut hasher);
            }
        }
        self.config.hash(&mut hasher);
        hasher.finish()
    }

    fn extract_contextual_features(
        &self,
        sentence: &str,
        context_window: &[String],
    ) -> ContextResult<HashMap<String, f64>> {
        let mut features = HashMap::new();

        // Basic linguistic features
        features.insert(
            "sentence_length".to_string(),
            sentence.split_whitespace().count() as f64,
        );
        features.insert(
            "context_window_size".to_string(),
            context_window.len() as f64,
        );

        // Semantic density
        let semantic_density = self.calculate_semantic_density(sentence, context_window)?;
        features.insert("semantic_density".to_string(), semantic_density);

        // Contextual overlap
        let contextual_overlap = self.calculate_contextual_overlap(sentence, context_window)?;
        features.insert("contextual_overlap".to_string(), contextual_overlap);

        // Semantic novelty
        let semantic_novelty = self.calculate_semantic_novelty(sentence, context_window)?;
        features.insert("semantic_novelty".to_string(), semantic_novelty);

        Ok(features)
    }

    fn calculate_context_strength(
        &self,
        position: usize,
        sentences: &[String],
    ) -> ContextResult<f64> {
        // Context strength based on position and surrounding content
        let total_sentences = sentences.len() as f64;
        let relative_position = position as f64 / total_sentences;

        // Stronger context in middle of text
        let position_strength = 1.0 - (2.0 * (relative_position - 0.5)).abs();

        // Content-based strength
        let content_strength = if position > 0 && position < sentences.len() - 1 {
            self.calculate_content_connectivity(
                &sentences[position],
                &sentences[position - 1],
                &sentences[position + 1],
            )?
        } else {
            0.5 // Edge cases have moderate strength
        };

        Ok((position_strength + content_strength) / 2.0)
    }

    fn update_context_tracking(&mut self, contexts: &[SemanticContext]) -> ContextResult<()> {
        // Update context windows queue
        self.context_windows.clear();
        for context in contexts {
            self.context_windows.push_back(context.clone());

            // Maintain maximum queue size
            if self.context_windows.len() > self.config.max_tracked_contexts {
                self.context_windows.pop_front();
            }
        }

        // Update contextual mappings
        for context in contexts {
            for keyword in &context.contextual_keywords {
                self.contextual_mappings
                    .entry(keyword.clone())
                    .or_insert_with(Vec::new)
                    .push(context.clone());
            }
        }

        Ok(())
    }
}

impl Default for SemanticContextAnalyzer {
    fn default() -> Self {
        Self::new(ContextAnalysisConfig::default()).expect("default context config should be valid")
    }
}

// Additional implementation methods would continue here...
// This represents the core structure for comprehensive contextual semantic analysis