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
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
//! Semantic Relations Analysis Module
//!
//! This module provides comprehensive semantic relationship analysis for text fluency
//! evaluation. It focuses on detecting, analyzing, and measuring various types of
//! semantic relationships within and between textual elements.

use super::config::RelationsAnalysisConfig;
use super::results::{
    RelationStrength, RelationshipHierarchy, RelationshipNetwork, RelationshipPattern,
    SemanticGraph, SemanticRelation, SemanticRelationsMetrics,
};
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 RelationsAnalysisError {
    #[error("Invalid relations analysis configuration: {0}")]
    ConfigError(String),
    #[error("Relationship calculation failed: {0}")]
    CalculationError(String),
    #[error("Semantic relations analysis error: {0}")]
    AnalysisError(String),
}

pub type RelationsResult<T> = Result<T, RelationsAnalysisError>;

/// Semantic relation types for comprehensive analysis
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum SemanticRelationType {
    /// Hierarchical relationships (hypernym/hyponym)
    Hierarchical,
    /// Similarity relationships (synonyms, near-synonyms)
    Similarity,
    /// Opposition relationships (antonyms, contrasts)
    Opposition,
    /// Causal relationships (cause-effect)
    Causal,
    /// Temporal relationships (before-after, sequence)
    Temporal,
    /// Spatial relationships (location, direction)
    Spatial,
    /// Part-whole relationships (meronymy)
    PartWhole,
    /// Functional relationships (purpose, use)
    Functional,
    /// Associative relationships (common co-occurrence)
    Associative,
    /// Thematic relationships (topic-related)
    Thematic,
}

/// Advanced semantic relations analyzer
#[derive(Debug, Clone)]
pub struct SemanticRelationsAnalyzer {
    config: RelationsAnalysisConfig,
    relation_graph: SemanticGraph,
    detected_relations: Vec<SemanticRelation>,
    relation_patterns: Vec<RelationshipPattern>,
    hierarchies: BTreeMap<String, RelationshipHierarchy>,
    relation_cache: HashMap<u64, SemanticRelationsMetrics>,
    network_cache: HashMap<String, RelationshipNetwork>,
    strength_tracker: HashMap<String, f64>,
}

impl SemanticRelationsAnalyzer {
    /// Create new relations analyzer with configuration
    pub fn new(config: RelationsAnalysisConfig) -> RelationsResult<Self> {
        Self::validate_config(&config)?;

        Ok(Self {
            config,
            relation_graph: SemanticGraph::new(),
            detected_relations: Vec::new(),
            relation_patterns: Vec::new(),
            hierarchies: BTreeMap::new(),
            relation_cache: HashMap::new(),
            network_cache: HashMap::new(),
            strength_tracker: HashMap::new(),
        })
    }

    /// Analyze semantic relations in text comprehensively
    pub fn analyze_semantic_relations(
        &mut self,
        text: &str,
        domain_knowledge: Option<&HashMap<String, Vec<String>>>,
    ) -> RelationsResult<SemanticRelationsMetrics> {
        let cache_key = self.generate_cache_key(text, domain_knowledge);
        if let Some(cached) = self.relation_cache.get(&cache_key) {
            return Ok(cached.clone());
        }

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

        // Build semantic graph from text
        self.build_semantic_graph(&sentences, domain_knowledge)?;

        // Core relation analysis
        metrics.overall_relation_score = self.calculate_overall_relation_score()?;
        metrics.relation_density = self.calculate_relation_density()?;
        metrics.relation_coherence = self.measure_relation_coherence()?;
        metrics.semantic_connectivity = self.analyze_semantic_connectivity()?;

        // Detect and analyze relationships
        metrics.detected_relations = self.detect_semantic_relations(&sentences)?;
        metrics.relationship_networks = self.build_relationship_networks()?;
        metrics.relation_strengths = self.calculate_relation_strengths()?;

        // Advanced relationship analysis
        if self.config.analyze_advanced_relations {
            metrics.relationship_patterns = self.identify_relationship_patterns()?;
            metrics.hierarchical_structures = self.analyze_hierarchical_structures()?;
            metrics.semantic_graph_metrics = self.calculate_graph_metrics()?;
            metrics.relation_evolution = self.trace_relation_evolution(&sentences)?;
        }

        // Multi-level relationship analysis
        if self.config.multi_level_analysis {
            metrics.local_relations = self.analyze_local_relations(&sentences)?;
            metrics.global_relations = self.analyze_global_relations(&sentences)?;
            metrics.cross_sentence_relations = self.analyze_cross_sentence_relations(&sentences)?;
        }

        // Relationship quality assessment
        if self.config.assess_relation_quality {
            metrics.relation_quality_metrics = self.assess_relation_quality()?;
            metrics.redundancy_analysis = self.analyze_relation_redundancy()?;
            metrics.completeness_analysis = self.analyze_relation_completeness(&sentences)?;
        }

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

        Ok(metrics)
    }

    /// Build comprehensive semantic graph from sentences
    fn build_semantic_graph(
        &mut self,
        sentences: &[String],
        domain_knowledge: Option<&HashMap<String, Vec<String>>>,
    ) -> RelationsResult<()> {
        // Clear existing graph
        self.relation_graph = SemanticGraph::new();

        // Extract entities and concepts from all sentences
        let mut entities = HashMap::new();
        let mut concepts = HashMap::new();

        for (i, sentence) in sentences.iter().enumerate() {
            let sentence_entities = self.extract_entities(sentence)?;
            let sentence_concepts = self.extract_concepts(sentence)?;

            for entity in sentence_entities {
                entities
                    .entry(entity.clone())
                    .or_insert_with(Vec::new)
                    .push(i);
                self.relation_graph.add_node(&entity, "entity");
            }

            for concept in sentence_concepts {
                concepts
                    .entry(concept.clone())
                    .or_insert_with(Vec::new)
                    .push(i);
                self.relation_graph.add_node(&concept, "concept");
            }
        }

        // Build relationships between entities and concepts
        self.build_intra_sentence_relations(sentences)?;
        self.build_inter_sentence_relations(sentences)?;

        // Incorporate domain knowledge if available
        if let Some(knowledge) = domain_knowledge {
            self.incorporate_domain_knowledge(knowledge)?;
        }

        // Calculate relationship strengths
        self.calculate_graph_relationships()?;

        Ok(())
    }

    /// Calculate overall semantic relation score
    fn calculate_overall_relation_score(&self) -> RelationsResult<f64> {
        let relation_count = self.detected_relations.len() as f64;
        let potential_relations = self.estimate_potential_relations()?;

        if potential_relations == 0.0 {
            return Ok(0.0);
        }

        let coverage_score = relation_count / potential_relations;
        let quality_score = self.calculate_average_relation_quality()?;
        let coherence_score = self.measure_relation_coherence()?;

        // Weighted combination
        let overall_score = (coverage_score * self.config.coverage_weight
            + quality_score * self.config.quality_weight
            + coherence_score * self.config.coherence_weight)
            / (self.config.coverage_weight
                + self.config.quality_weight
                + self.config.coherence_weight);

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

    /// Calculate relation density in text
    fn calculate_relation_density(&self) -> RelationsResult<f64> {
        let node_count = self.relation_graph.node_count() as f64;
        let edge_count = self.relation_graph.edge_count() as f64;

        if node_count < 2.0 {
            return Ok(0.0);
        }

        // Maximum possible edges in an undirected graph
        let max_edges = (node_count * (node_count - 1.0)) / 2.0;
        let density = edge_count / max_edges;

        Ok(density)
    }

    /// Measure semantic relation coherence
    fn measure_relation_coherence(&self) -> RelationsResult<f64> {
        if self.detected_relations.is_empty() {
            return Ok(0.0);
        }

        let mut coherence_scores = Vec::new();

        // Analyze coherence of each relation type
        let relation_groups = self.group_relations_by_type();

        for (_relation_type, relations) in relation_groups {
            let type_coherence = self.calculate_type_coherence(&relations)?;
            coherence_scores.push(type_coherence);
        }

        // Overall coherence considering relation interactions
        let mean_coherence = coherence_scores.iter().sum::<f64>() / coherence_scores.len() as f64;
        let interaction_coherence = self.calculate_interaction_coherence()?;

        Ok((mean_coherence + interaction_coherence) / 2.0)
    }

    /// Analyze semantic connectivity in the graph
    fn analyze_semantic_connectivity(&self) -> RelationsResult<f64> {
        let components = self.relation_graph.connected_components();
        let total_nodes = self.relation_graph.node_count();

        if total_nodes == 0 {
            return Ok(0.0);
        }

        // Calculate connectivity metrics
        let largest_component_size = components
            .iter()
            .map(|component| component.len())
            .max()
            .unwrap_or(0) as f64;

        let connectivity_ratio = largest_component_size / total_nodes as f64;

        // Average path length in largest component
        let avg_path_length = self.calculate_average_path_length()?;
        let normalized_path_length = if avg_path_length > 0.0 {
            1.0 / avg_path_length.ln()
        } else {
            0.0
        };

        // Combine connectivity measures
        Ok((connectivity_ratio + normalized_path_length) / 2.0)
    }

    /// Detect semantic relations in sentences
    fn detect_semantic_relations(
        &mut self,
        sentences: &[String],
    ) -> RelationsResult<Vec<SemanticRelation>> {
        let mut relations = Vec::new();

        // Detect different types of semantic relations
        for relation_type in self.get_enabled_relation_types() {
            let type_relations = self.detect_relations_of_type(sentences, &relation_type)?;
            relations.extend(type_relations);
        }

        // Filter and validate relations
        let filtered_relations = self.filter_relations(relations)?;

        // Store detected relations
        self.detected_relations = filtered_relations.clone();

        Ok(filtered_relations)
    }

    /// Build relationship networks from detected relations
    fn build_relationship_networks(
        &mut self,
    ) -> RelationsResult<HashMap<String, RelationshipNetwork>> {
        let mut networks = HashMap::new();

        // Group relations by type to build type-specific networks
        let relation_groups = self.group_relations_by_type();

        for (relation_type, relations) in relation_groups {
            let network = self.build_network_for_type(&relation_type, &relations)?;
            networks.insert(relation_type.to_string(), network);
        }

        // Build overall semantic network
        let overall_network = self.build_overall_network()?;
        networks.insert("overall".to_string(), overall_network);

        // Cache networks for reuse
        self.network_cache = networks.clone();

        Ok(networks)
    }

    /// Calculate strengths of semantic relations
    fn calculate_relation_strengths(
        &mut self,
    ) -> RelationsResult<HashMap<String, RelationStrength>> {
        let mut strengths = HashMap::new();

        for relation in &self.detected_relations {
            let strength = self.calculate_individual_relation_strength(relation)?;
            let key = format!("{}_{}", relation.source, relation.target);
            strengths.insert(key.clone(), strength);

            // Update strength tracker
            self.strength_tracker.insert(key, strength.overall_strength);
        }

        Ok(strengths)
    }

    /// Identify relationship patterns in text
    fn identify_relationship_patterns(&mut self) -> RelationsResult<Vec<RelationshipPattern>> {
        let mut patterns = Vec::new();

        // Analyze sequential patterns
        let sequential_patterns = self.detect_sequential_patterns()?;
        patterns.extend(sequential_patterns);

        // Analyze hierarchical patterns
        let hierarchical_patterns = self.detect_hierarchical_patterns()?;
        patterns.extend(hierarchical_patterns);

        // Analyze clustering patterns
        let clustering_patterns = self.detect_clustering_patterns()?;
        patterns.extend(clustering_patterns);

        // Store patterns for future analysis
        self.relation_patterns = patterns.clone();

        Ok(patterns)
    }

    /// Analyze hierarchical structures in relations
    fn analyze_hierarchical_structures(
        &mut self,
    ) -> RelationsResult<BTreeMap<String, RelationshipHierarchy>> {
        let mut hierarchies = BTreeMap::new();

        // Extract hierarchical relations
        let hierarchical_relations: Vec<_> = self
            .detected_relations
            .iter()
            .filter(|r| r.relation_type == SemanticRelationType::Hierarchical)
            .collect();

        if hierarchical_relations.is_empty() {
            return Ok(hierarchies);
        }

        // Build hierarchy trees
        let hierarchy_roots = self.identify_hierarchy_roots(&hierarchical_relations)?;

        for root in hierarchy_roots {
            let hierarchy = self.build_hierarchy_from_root(&root, &hierarchical_relations)?;
            hierarchies.insert(root.clone(), hierarchy);
        }

        // Store hierarchies
        self.hierarchies = hierarchies.clone();

        Ok(hierarchies)
    }

    // Helper methods for relation analysis

    fn validate_config(config: &RelationsAnalysisConfig) -> RelationsResult<()> {
        if config.min_relation_strength < 0.0 || config.min_relation_strength > 1.0 {
            return Err(RelationsAnalysisError::ConfigError(
                "min_relation_strength must be between 0.0 and 1.0".to_string(),
            ));
        }

        if config.coverage_weight < 0.0
            || config.quality_weight < 0.0
            || config.coherence_weight < 0.0
        {
            return Err(RelationsAnalysisError::ConfigError(
                "all 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,
        domain_knowledge: Option<&HashMap<String, Vec<String>>>,
    ) -> u64 {
        let mut hasher = DefaultHasher::new();
        text.hash(&mut hasher);
        if let Some(knowledge) = domain_knowledge {
            for (k, v) in knowledge {
                k.hash(&mut hasher);
                for item in v {
                    item.hash(&mut hasher);
                }
            }
        }
        self.config.hash(&mut hasher);
        hasher.finish()
    }

    fn extract_entities(&self, sentence: &str) -> RelationsResult<Vec<String>> {
        let words: Vec<&str> = sentence.split_whitespace().collect();
        let mut entities = Vec::new();

        // Simple entity extraction based on capitalization and patterns
        for word in words {
            if self.is_potential_entity(word) {
                entities.push(word.to_lowercase());
            }
        }

        // Extract multi-word entities if enabled
        if self.config.extract_multiword_entities {
            entities.extend(self.extract_multiword_entities(sentence)?);
        }

        Ok(entities)
    }

    fn extract_concepts(&self, sentence: &str) -> RelationsResult<Vec<String>> {
        let words: Vec<&str> = sentence.split_whitespace().collect();
        let mut concepts = Vec::new();

        for word in words {
            if self.is_meaningful_concept(word) {
                concepts.push(word.to_lowercase());
            }
        }

        Ok(concepts)
    }

    fn is_potential_entity(&self, word: &str) -> bool {
        // Check if word could be an entity (proper noun, etc.)
        word.len() >= self.config.min_entity_length
            && word.chars().any(|c| c.is_uppercase())
            && word.chars().any(|c| c.is_alphabetic())
    }

    fn is_meaningful_concept(&self, word: &str) -> bool {
        word.len() >= self.config.min_concept_length
            && word.chars().any(|c| c.is_alphabetic())
            && !self.is_stop_word(word)
    }

    fn is_stop_word(&self, word: &str) -> bool {
        self.config.stop_words.contains(&word.to_lowercase())
    }

    fn get_enabled_relation_types(&self) -> Vec<SemanticRelationType> {
        let mut types = Vec::new();

        if self.config.detect_hierarchical {
            types.push(SemanticRelationType::Hierarchical);
        }
        if self.config.detect_similarity {
            types.push(SemanticRelationType::Similarity);
        }
        if self.config.detect_opposition {
            types.push(SemanticRelationType::Opposition);
        }
        if self.config.detect_causal {
            types.push(SemanticRelationType::Causal);
        }
        if self.config.detect_temporal {
            types.push(SemanticRelationType::Temporal);
        }
        if self.config.detect_spatial {
            types.push(SemanticRelationType::Spatial);
        }
        if self.config.detect_part_whole {
            types.push(SemanticRelationType::PartWhole);
        }
        if self.config.detect_functional {
            types.push(SemanticRelationType::Functional);
        }
        if self.config.detect_associative {
            types.push(SemanticRelationType::Associative);
        }
        if self.config.detect_thematic {
            types.push(SemanticRelationType::Thematic);
        }

        types
    }
}

impl Default for SemanticRelationsAnalyzer {
    fn default() -> Self {
        Self::new(RelationsAnalysisConfig::default()).expect("default relations config should be valid")
    }
}

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