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
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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
//! Comprehensive semantic analysis and similarity computation framework
//!
//! This module provides a unified interface for semantic analysis, combining multiple specialized
//! approaches including similarity algorithms, feature extraction, sentiment analysis, topic modeling,
//! domain classification, syntactic analysis, and comprehensive metrics collection.
//!
//! ## Features
//!
//! - **Similarity Algorithms**: Advanced similarity computation with multiple algorithms
//! - **Feature Extraction**: Comprehensive semantic feature extraction strategies
//! - **Sentiment Analysis**: Emotion-aware sentiment analysis with context awareness
//! - **Topic Modeling**: Advanced topic modeling with multiple approaches
//! - **Domain Analysis**: Semantic domain classification and analysis
//! - **Syntactic Analysis**: Syntactic pattern analysis and comparison
//! - **Metrics Collection**: Comprehensive statistical analysis and quality assessment
//! - **Unified Interface**: Single point of entry for all semantic analysis functionality
//!
//! ## Quick Start
//!
//! ```rust
//! use torsh_text::metrics::semantic::{SemanticAnalyzer, SemanticAnalysisConfig};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Create analyzer with default configuration
//! let mut analyzer = SemanticAnalyzer::default()?;
//!
//! // Analyze similarity between two texts
//! let text1 = "The quick brown fox jumps over the lazy dog.";
//! let text2 = "A fast brown fox leaps across the sleeping dog.";
//!
//! let result = analyzer.analyze_comprehensive(text1, text2).await?;
//! println!("Similarity: {:.2}", result.overall_similarity);
//! # Ok(())
//! # }
//! ```
//!
//! ## Advanced Usage
//!
//! ```rust
//! use torsh_text::metrics::semantic::{
//!     SemanticAnalyzer, SemanticAnalysisConfig,
//!     similarity_algorithms::SimilarityAlgorithm,
//!     feature_extraction::FeatureExtractionStrategy,
//!     sentiment_analysis::SentimentAnalysisMode,
//! };
//!
//! # async fn advanced_example() -> Result<(), Box<dyn std::error::Error>> {
//! // Create custom configuration
//! let config = SemanticAnalysisConfig::builder()
//!     .enable_similarity_analysis(true)
//!     .enable_sentiment_analysis(true)
//!     .enable_topic_modeling(true)
//!     .enable_domain_analysis(true)
//!     .enable_syntactic_analysis(true)
//!     .enable_metrics_collection(true)
//!     .similarity_algorithm(SimilarityAlgorithm::Hierarchical)
//!     .feature_extraction_strategy(FeatureExtractionStrategy::Comprehensive)
//!     .sentiment_analysis_mode(SentimentAnalysisMode::ContextAware)
//!     .enable_caching(true)
//!     .enable_parallel_processing(true)
//!     .build()?;
//!
//! let mut analyzer = SemanticAnalyzer::new(config)?;
//!
//! // Batch analysis
//! let texts = vec![
//!     "Technical documentation about software engineering.",
//!     "Academic research on machine learning algorithms.",
//!     "Business strategy for market expansion.",
//! ];
//!
//! let results = analyzer.analyze_batch(&texts).await?;
//! for (i, result) in results.iter().enumerate() {
//!     println!("Analysis {}: Quality={:.2}", i, result.quality_score);
//! }
//! # Ok(())
//! # }
//! ```

use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::random::{rng, Random};
use std::collections::{BTreeMap, HashMap, VecDeque};
use std::sync::{Arc, Mutex, RwLock};
use std::time::{Duration, Instant, SystemTime};
use thiserror::Error;
use tokio::task;

// Re-export all specialized modules
pub mod domain_analysis;
pub mod feature_extraction;
pub mod semantic_metrics;
pub mod sentiment_analysis;
pub mod similarity_algorithms;
pub mod syntactic_similarity;
pub mod topic_modeling;

// Re-export key types for convenience
pub use domain_analysis::{
    DomainAnalysisError, DomainAnalyzer, DomainClassification, SemanticDomain,
};
pub use feature_extraction::{
    FeatureExtractionError, FeatureExtractionStrategy, FeatureExtractor, SemanticFeatureVector,
};
pub use semantic_metrics::{
    MetricsSummary, SemanticMetricsAnalyzer, SemanticMetricsError, SemanticMetricsResult,
};
pub use sentiment_analysis::{
    EmotionScores, SentimentAnalysisError, SentimentAnalysisMode, SentimentAnalyzer,
    SentimentScores,
};
pub use similarity_algorithms::{
    SimilarityAlgorithm, SimilarityAlgorithmEngine, SimilarityAlgorithmError, SimilarityResult,
};
pub use syntactic_similarity::{
    SyntacticApproach, SyntacticSimilarityAnalyzer, SyntacticSimilarityError,
    SyntacticSimilarityResult,
};
pub use topic_modeling::{
    TopicModeler, TopicModelingApproach, TopicModelingError, TopicModelingResult,
};

/// Errors that can occur during semantic analysis
#[derive(Error, Debug)]
pub enum SemanticAnalysisError {
    #[error("Invalid configuration: {message}")]
    InvalidConfiguration { message: String },

    #[error("Analysis failed: {reason}")]
    AnalysisFailed { reason: String },

    #[error("Similarity algorithm error: {source}")]
    SimilarityError {
        #[from]
        source: SimilarityAlgorithmError,
    },

    #[error("Feature extraction error: {source}")]
    FeatureExtractionError {
        #[from]
        source: FeatureExtractionError,
    },

    #[error("Sentiment analysis error: {source}")]
    SentimentError {
        #[from]
        source: SentimentAnalysisError,
    },

    #[error("Topic modeling error: {source}")]
    TopicModelingError {
        #[from]
        source: TopicModelingError,
    },

    #[error("Domain analysis error: {source}")]
    DomainAnalysisError {
        #[from]
        source: DomainAnalysisError,
    },

    #[error("Syntactic analysis error: {source}")]
    SyntacticError {
        #[from]
        source: SyntacticSimilarityError,
    },

    #[error("Metrics analysis error: {source}")]
    MetricsError {
        #[from]
        source: SemanticMetricsError,
    },

    #[error("Parallel processing error: {reason}")]
    ParallelProcessingError { reason: String },

    #[error("Cache error: {reason}")]
    CacheError { reason: String },

    #[error("Timeout error: operation took longer than {timeout_seconds} seconds")]
    TimeoutError { timeout_seconds: u64 },
}

/// Comprehensive semantic analysis result
#[derive(Debug, Clone)]
pub struct SemanticAnalysisResult {
    /// Overall similarity score (0.0-1.0)
    pub overall_similarity: f64,
    /// Overall quality score (0.0-1.0)
    pub quality_score: f64,
    /// Overall confidence score (0.0-1.0)
    pub confidence_score: f64,
    /// Detailed similarity analysis results
    pub similarity_analysis: Option<SimilarityResult>,
    /// Feature extraction results
    pub feature_analysis: Option<(SemanticFeatureVector, SemanticFeatureVector)>,
    /// Sentiment analysis results
    pub sentiment_analysis: Option<(SentimentScores, SentimentScores)>,
    /// Topic modeling results
    pub topic_analysis: Option<(TopicModelingResult, TopicModelingResult)>,
    /// Domain classification results
    pub domain_analysis: Option<(DomainClassification, DomainClassification)>,
    /// Syntactic similarity results
    pub syntactic_analysis: Option<SyntacticSimilarityResult>,
    /// Comprehensive metrics analysis
    pub metrics_analysis: Option<SemanticMetricsResult>,
    /// Analysis metadata
    pub metadata: AnalysisMetadata,
}

/// Analysis metadata
#[derive(Debug, Clone)]
pub struct AnalysisMetadata {
    /// Analysis timestamp
    pub timestamp: SystemTime,
    /// Total processing time
    pub processing_time: Duration,
    /// Configuration summary
    pub config_used: String,
    /// Components that were enabled
    pub enabled_components: Vec<String>,
    /// Text characteristics
    pub text1_characteristics: TextCharacteristics,
    pub text2_characteristics: TextCharacteristics,
    /// Performance metrics
    pub performance_metrics: PerformanceInfo,
    /// Cache hit/miss information
    pub cache_info: CacheInfo,
}

/// Text characteristics for analysis
#[derive(Debug, Clone)]
pub struct TextCharacteristics {
    pub length: usize,
    pub word_count: usize,
    pub sentence_count: usize,
    pub paragraph_count: usize,
    pub language_detected: Option<String>,
    pub complexity_score: f64,
    pub readability_score: f64,
}

/// Performance information
#[derive(Debug, Clone)]
pub struct PerformanceInfo {
    pub total_time: Duration,
    pub similarity_time: Duration,
    pub feature_extraction_time: Duration,
    pub sentiment_analysis_time: Duration,
    pub topic_modeling_time: Duration,
    pub domain_analysis_time: Duration,
    pub syntactic_analysis_time: Duration,
    pub metrics_analysis_time: Duration,
    pub parallel_efficiency: f64,
}

/// Cache information
#[derive(Debug, Clone)]
pub struct CacheInfo {
    pub cache_hits: usize,
    pub cache_misses: usize,
    pub cache_hit_rate: f64,
    pub cache_size: usize,
}

/// Batch analysis result
#[derive(Debug, Clone)]
pub struct BatchAnalysisResult {
    /// Individual analysis results
    pub results: Vec<SemanticAnalysisResult>,
    /// Batch statistics
    pub batch_statistics: BatchStatistics,
    /// Performance summary
    pub performance_summary: BatchPerformance,
}

/// Batch statistics
#[derive(Debug, Clone)]
pub struct BatchStatistics {
    pub total_items: usize,
    pub successful_analyses: usize,
    pub failed_analyses: usize,
    pub average_similarity: f64,
    pub similarity_distribution: HashMap<String, usize>, // Ranges like "0.0-0.1", "0.1-0.2", etc.
    pub quality_distribution: HashMap<String, usize>,
}

/// Batch performance metrics
#[derive(Debug, Clone)]
pub struct BatchPerformance {
    pub total_time: Duration,
    pub average_time_per_item: Duration,
    pub items_per_second: f64,
    pub parallel_speedup: f64,
    pub memory_usage: usize,
}

/// Configuration for semantic analysis
#[derive(Debug, Clone)]
pub struct SemanticAnalysisConfig {
    /// Enable similarity algorithm analysis
    pub enable_similarity_analysis: bool,
    /// Enable feature extraction
    pub enable_feature_extraction: bool,
    /// Enable sentiment analysis
    pub enable_sentiment_analysis: bool,
    /// Enable topic modeling
    pub enable_topic_modeling: bool,
    /// Enable domain analysis
    pub enable_domain_analysis: bool,
    /// Enable syntactic analysis
    pub enable_syntactic_analysis: bool,
    /// Enable comprehensive metrics collection
    pub enable_metrics_collection: bool,
    /// Similarity algorithm to use
    pub similarity_algorithm: SimilarityAlgorithm,
    /// Feature extraction strategy
    pub feature_extraction_strategy: feature_extraction::FeatureExtractionStrategy,
    /// Sentiment analysis mode
    pub sentiment_analysis_mode: sentiment_analysis::SentimentAnalysisMode,
    /// Topic modeling approach
    pub topic_modeling_approach: topic_modeling::TopicModelingApproach,
    /// Domain analysis approach
    pub domain_analysis_approach: domain_analysis::DomainApproach,
    /// Syntactic analysis approach
    pub syntactic_analysis_approach: syntactic_similarity::SyntacticApproach,
    /// Enable result caching
    pub enable_caching: bool,
    /// Cache size limit
    pub cache_size_limit: usize,
    /// Enable parallel processing
    pub enable_parallel_processing: bool,
    /// Maximum number of parallel threads
    pub max_parallel_threads: usize,
    /// Analysis timeout in seconds
    pub timeout_seconds: u64,
    /// Minimum quality threshold for results
    pub quality_threshold: f64,
}

impl Default for SemanticAnalysisConfig {
    fn default() -> Self {
        Self {
            enable_similarity_analysis: true,
            enable_feature_extraction: true,
            enable_sentiment_analysis: true,
            enable_topic_modeling: true,
            enable_domain_analysis: true,
            enable_syntactic_analysis: true,
            enable_metrics_collection: true,
            similarity_algorithm: SimilarityAlgorithm::Hierarchical,
            feature_extraction_strategy:
                feature_extraction::FeatureExtractionStrategy::Comprehensive,
            sentiment_analysis_mode: sentiment_analysis::SentimentAnalysisMode::ContextAware,
            topic_modeling_approach: topic_modeling::TopicModelingApproach::Dynamic,
            domain_analysis_approach: domain_analysis::DomainApproach::MultiModal,
            syntactic_analysis_approach: syntactic_similarity::SyntacticApproach::Comprehensive,
            enable_caching: true,
            cache_size_limit: 1000,
            enable_parallel_processing: true,
            max_parallel_threads: num_cpus::get(),
            timeout_seconds: 300, // 5 minutes
            quality_threshold: 0.0,
        }
    }
}

impl SemanticAnalysisConfig {
    /// Create a new configuration builder
    pub fn builder() -> SemanticAnalysisConfigBuilder {
        SemanticAnalysisConfigBuilder::new()
    }

    /// Validate configuration
    pub fn validate(&self) -> Result<(), SemanticAnalysisError> {
        if self.cache_size_limit == 0 && self.enable_caching {
            return Err(SemanticAnalysisError::InvalidConfiguration {
                message: "Cache size limit must be greater than 0 when caching is enabled"
                    .to_string(),
            });
        }

        if self.max_parallel_threads == 0 && self.enable_parallel_processing {
            return Err(SemanticAnalysisError::InvalidConfiguration {
                message: "Max parallel threads must be greater than 0 when parallel processing is enabled".to_string(),
            });
        }

        if self.timeout_seconds == 0 {
            return Err(SemanticAnalysisError::InvalidConfiguration {
                message: "Timeout must be greater than 0 seconds".to_string(),
            });
        }

        if self.quality_threshold < 0.0 || self.quality_threshold > 1.0 {
            return Err(SemanticAnalysisError::InvalidConfiguration {
                message: "Quality threshold must be between 0.0 and 1.0".to_string(),
            });
        }

        Ok(())
    }
}

/// Builder for semantic analysis configuration
pub struct SemanticAnalysisConfigBuilder {
    config: SemanticAnalysisConfig,
}

impl SemanticAnalysisConfigBuilder {
    pub fn new() -> Self {
        Self {
            config: SemanticAnalysisConfig::default(),
        }
    }

    pub fn enable_similarity_analysis(mut self, enable: bool) -> Self {
        self.config.enable_similarity_analysis = enable;
        self
    }

    pub fn enable_feature_extraction(mut self, enable: bool) -> Self {
        self.config.enable_feature_extraction = enable;
        self
    }

    pub fn enable_sentiment_analysis(mut self, enable: bool) -> Self {
        self.config.enable_sentiment_analysis = enable;
        self
    }

    pub fn enable_topic_modeling(mut self, enable: bool) -> Self {
        self.config.enable_topic_modeling = enable;
        self
    }

    pub fn enable_domain_analysis(mut self, enable: bool) -> Self {
        self.config.enable_domain_analysis = enable;
        self
    }

    pub fn enable_syntactic_analysis(mut self, enable: bool) -> Self {
        self.config.enable_syntactic_analysis = enable;
        self
    }

    pub fn enable_metrics_collection(mut self, enable: bool) -> Self {
        self.config.enable_metrics_collection = enable;
        self
    }

    pub fn similarity_algorithm(mut self, algorithm: SimilarityAlgorithm) -> Self {
        self.config.similarity_algorithm = algorithm;
        self
    }

    pub fn feature_extraction_strategy(
        mut self,
        strategy: feature_extraction::FeatureExtractionStrategy,
    ) -> Self {
        self.config.feature_extraction_strategy = strategy;
        self
    }

    pub fn sentiment_analysis_mode(
        mut self,
        mode: sentiment_analysis::SentimentAnalysisMode,
    ) -> Self {
        self.config.sentiment_analysis_mode = mode;
        self
    }

    pub fn topic_modeling_approach(
        mut self,
        approach: topic_modeling::TopicModelingApproach,
    ) -> Self {
        self.config.topic_modeling_approach = approach;
        self
    }

    pub fn domain_analysis_approach(mut self, approach: domain_analysis::DomainApproach) -> Self {
        self.config.domain_analysis_approach = approach;
        self
    }

    pub fn syntactic_analysis_approach(
        mut self,
        approach: syntactic_similarity::SyntacticApproach,
    ) -> Self {
        self.config.syntactic_analysis_approach = approach;
        self
    }

    pub fn enable_caching(mut self, enable: bool) -> Self {
        self.config.enable_caching = enable;
        self
    }

    pub fn cache_size_limit(mut self, limit: usize) -> Self {
        self.config.cache_size_limit = limit.max(1);
        self
    }

    pub fn enable_parallel_processing(mut self, enable: bool) -> Self {
        self.config.enable_parallel_processing = enable;
        self
    }

    pub fn max_parallel_threads(mut self, max: usize) -> Self {
        self.config.max_parallel_threads = max.max(1);
        self
    }

    pub fn timeout_seconds(mut self, timeout: u64) -> Self {
        self.config.timeout_seconds = timeout.max(1);
        self
    }

    pub fn quality_threshold(mut self, threshold: f64) -> Self {
        self.config.quality_threshold = threshold.clamp(0.0, 1.0);
        self
    }

    pub fn build(self) -> Result<SemanticAnalysisConfig, SemanticAnalysisError> {
        self.config.validate()?;
        Ok(self.config)
    }
}

/// Cache entry for analysis results
#[derive(Debug, Clone)]
struct CacheEntry {
    result: SemanticAnalysisResult,
    timestamp: SystemTime,
    access_count: usize,
}

/// Comprehensive semantic analyzer with unified interface
pub struct SemanticAnalyzer {
    config: SemanticAnalysisConfig,
    similarity_engine: Arc<Mutex<SimilarityAlgorithmEngine>>,
    feature_extractor: Arc<Mutex<FeatureExtractor>>,
    sentiment_analyzer: Arc<Mutex<SentimentAnalyzer>>,
    topic_modeler: Arc<Mutex<TopicModeler>>,
    domain_analyzer: Arc<Mutex<DomainAnalyzer>>,
    syntactic_analyzer: Arc<Mutex<SyntacticSimilarityAnalyzer>>,
    metrics_analyzer: Arc<Mutex<SemanticMetricsAnalyzer>>,
    cache: Arc<RwLock<HashMap<String, CacheEntry>>>,
    cache_stats: Arc<Mutex<CacheInfo>>,
}

impl SemanticAnalyzer {
    /// Create a new semantic analyzer with the given configuration
    pub fn new(config: SemanticAnalysisConfig) -> Result<Self, SemanticAnalysisError> {
        config.validate()?;

        // Initialize all sub-analyzers with appropriate configurations
        let similarity_config = similarity_algorithms::SimilarityAlgorithmConfig::builder()
            .default_algorithm(config.similarity_algorithm)
            .enable_caching(config.enable_caching)
            .build()
            .map_err(|e| SemanticAnalysisError::InvalidConfiguration {
                message: e.to_string(),
            })?;

        let feature_config = feature_extraction::FeatureExtractionConfig::builder()
            .strategy(config.feature_extraction_strategy)
            .build()
            .map_err(|e| SemanticAnalysisError::InvalidConfiguration {
                message: e.to_string(),
            })?;

        let sentiment_config = sentiment_analysis::SentimentAnalysisConfig::builder()
            .mode(config.sentiment_analysis_mode)
            .build()
            .map_err(|e| SemanticAnalysisError::InvalidConfiguration {
                message: e.to_string(),
            })?;

        let topic_config = topic_modeling::TopicModelingConfig::builder()
            .approach(config.topic_modeling_approach)
            .build()
            .map_err(|e| SemanticAnalysisError::InvalidConfiguration {
                message: e.to_string(),
            })?;

        let domain_config = domain_analysis::DomainAnalysisConfig::builder()
            .approach(config.domain_analysis_approach)
            .build()
            .map_err(|e| SemanticAnalysisError::InvalidConfiguration {
                message: e.to_string(),
            })?;

        let syntactic_config = syntactic_similarity::SyntacticSimilarityConfig::builder()
            .approach(config.syntactic_analysis_approach)
            .build()
            .map_err(|e| SemanticAnalysisError::InvalidConfiguration {
                message: e.to_string(),
            })?;

        let metrics_config = semantic_metrics::SemanticMetricsConfig::builder()
            .enable_statistical_analysis(true)
            .enable_distribution_analysis(true)
            .enable_correlation_analysis(true)
            .build()
            .map_err(|e| SemanticAnalysisError::InvalidConfiguration {
                message: e.to_string(),
            })?;

        Ok(Self {
            config,
            similarity_engine: Arc::new(Mutex::new(SimilarityAlgorithmEngine::new(
                similarity_config,
            )?)),
            feature_extractor: Arc::new(Mutex::new(FeatureExtractor::new(feature_config)?)),
            sentiment_analyzer: Arc::new(Mutex::new(SentimentAnalyzer::new(sentiment_config)?)),
            topic_modeler: Arc::new(Mutex::new(TopicModeler::new(topic_config)?)),
            domain_analyzer: Arc::new(Mutex::new(DomainAnalyzer::new(domain_config)?)),
            syntactic_analyzer: Arc::new(Mutex::new(SyntacticSimilarityAnalyzer::new(
                syntactic_config,
            )?)),
            metrics_analyzer: Arc::new(Mutex::new(SemanticMetricsAnalyzer::new(metrics_config)?)),
            cache: Arc::new(RwLock::new(HashMap::new())),
            cache_stats: Arc::new(Mutex::new(CacheInfo {
                cache_hits: 0,
                cache_misses: 0,
                cache_hit_rate: 0.0,
                cache_size: 0,
            })),
        })
    }

    /// Create a semantic analyzer with default configuration
    pub fn default() -> Result<Self, SemanticAnalysisError> {
        Self::new(SemanticAnalysisConfig::default())
    }

    /// Perform comprehensive semantic analysis between two texts
    pub async fn analyze_comprehensive(
        &mut self,
        text1: &str,
        text2: &str,
    ) -> Result<SemanticAnalysisResult, SemanticAnalysisError> {
        let start_time = Instant::now();

        // Check cache first
        if self.config.enable_caching {
            if let Some(cached_result) = self.check_cache(text1, text2).await? {
                return Ok(cached_result);
            }
        }

        // Create timeout future
        let timeout_duration = Duration::from_secs(self.config.timeout_seconds);
        let analysis_future = self.perform_analysis(text1, text2);

        let result = match tokio::time::timeout(timeout_duration, analysis_future).await {
            Ok(Ok(result)) => result,
            Ok(Err(e)) => return Err(e),
            Err(_) => {
                return Err(SemanticAnalysisError::TimeoutError {
                    timeout_seconds: self.config.timeout_seconds,
                })
            }
        };

        // Cache result if enabled
        if self.config.enable_caching {
            self.cache_result(text1, text2, &result).await?;
        }

        Ok(result)
    }

    /// Perform the actual analysis
    async fn perform_analysis(
        &mut self,
        text1: &str,
        text2: &str,
    ) -> Result<SemanticAnalysisResult, SemanticAnalysisError> {
        let start_time = Instant::now();

        // Analyze text characteristics
        let text1_chars = self.analyze_text_characteristics(text1);
        let text2_chars = self.analyze_text_characteristics(text2);

        // Collect enabled components
        let mut enabled_components = Vec::new();
        let mut performance = PerformanceInfo {
            total_time: Duration::ZERO,
            similarity_time: Duration::ZERO,
            feature_extraction_time: Duration::ZERO,
            sentiment_analysis_time: Duration::ZERO,
            topic_modeling_time: Duration::ZERO,
            domain_analysis_time: Duration::ZERO,
            syntactic_analysis_time: Duration::ZERO,
            metrics_analysis_time: Duration::ZERO,
            parallel_efficiency: 1.0,
        };

        // Perform analyses based on configuration
        let mut tasks = Vec::new();

        // Feature extraction (needed by similarity analysis)
        let (features1, features2) = if self.config.enable_feature_extraction {
            enabled_components.push("feature_extraction".to_string());
            let fe_start = Instant::now();

            let mut feature_extractor = self.feature_extractor.lock().expect("lock should not be poisoned");
            let f1 = feature_extractor.extract_features(text1)?;
            let f2 = feature_extractor.extract_features(text2)?;
            drop(feature_extractor);

            performance.feature_extraction_time = fe_start.elapsed();
            (Some(f1), Some(f2))
        } else {
            (None, None)
        };

        // Similarity analysis
        let similarity_result =
            if self.config.enable_similarity_analysis && features1.is_some() && features2.is_some()
            {
                enabled_components.push("similarity_analysis".to_string());
                let sim_start = Instant::now();

                let mut similarity_engine = self.similarity_engine.lock().expect("lock should not be poisoned");
                let result = similarity_engine.compute_similarity(
                    self.config.similarity_algorithm,
                    features1.as_ref().expect("features1 verified to be Some"),
                    features2.as_ref().expect("features2 verified to be Some"),
                )?;
                drop(similarity_engine);

                performance.similarity_time = sim_start.elapsed();
                Some(result)
            } else {
                None
            };

        // Parallel analysis tasks (if enabled)
        if self.config.enable_parallel_processing {
            let parallel_start = Instant::now();

            // Sentiment analysis
            if self.config.enable_sentiment_analysis {
                let sentiment_analyzer = Arc::clone(&self.sentiment_analyzer);
                let text1 = text1.to_string();
                let text2 = text2.to_string();

                tasks.push(task::spawn(async move {
                    let mut analyzer = sentiment_analyzer.lock().expect("lock should not be poisoned");
                    let s1 = analyzer.analyze_sentiment(&text1)?;
                    let s2 = analyzer.analyze_sentiment(&text2)?;
                    Ok::<_, SemanticAnalysisError>((s1, s2))
                }));
            }

            // Topic modeling
            if self.config.enable_topic_modeling {
                let topic_modeler = Arc::clone(&self.topic_modeler);
                let text1 = text1.to_string();
                let text2 = text2.to_string();

                tasks.push(task::spawn(async move {
                    let mut modeler = topic_modeler.lock().expect("lock should not be poisoned");
                    let t1 = modeler.extract_topics(&text1)?;
                    let t2 = modeler.extract_topics(&text2)?;
                    Ok::<_, SemanticAnalysisError>((t1, t2))
                }));
            }

            // Domain analysis
            if self.config.enable_domain_analysis {
                let domain_analyzer = Arc::clone(&self.domain_analyzer);
                let text1 = text1.to_string();
                let text2 = text2.to_string();

                tasks.push(task::spawn(async move {
                    let mut analyzer = domain_analyzer.lock().expect("lock should not be poisoned");
                    let d1 = analyzer.classify_domain(&text1)?;
                    let d2 = analyzer.classify_domain(&text2)?;
                    Ok::<_, SemanticAnalysisError>((d1, d2))
                }));
            }

            // Syntactic analysis
            if self.config.enable_syntactic_analysis {
                let syntactic_analyzer = Arc::clone(&self.syntactic_analyzer);
                let text1 = text1.to_string();
                let text2 = text2.to_string();

                tasks.push(task::spawn(async move {
                    let mut analyzer = syntactic_analyzer.lock().expect("lock should not be poisoned");
                    let result = analyzer.analyze_similarity(&text1, &text2)?;
                    Ok::<_, SemanticAnalysisError>(result)
                }));
            }

            let parallel_time = parallel_start.elapsed();
            performance.parallel_efficiency =
                calculate_parallel_efficiency(parallel_time, tasks.len());
        }

        // Collect results from parallel tasks
        let mut sentiment_result = None;
        let mut topic_result = None;
        let mut domain_result = None;
        let mut syntactic_result = None;

        let mut task_index = 0;
        if self.config.enable_sentiment_analysis {
            if let Some(task) = tasks.get(task_index) {
                match task.await {
                    Ok(Ok(result)) => {
                        sentiment_result = Some(result);
                        enabled_components.push("sentiment_analysis".to_string());
                    }
                    Ok(Err(e)) => return Err(e),
                    Err(_) => {
                        return Err(SemanticAnalysisError::ParallelProcessingError {
                            reason: "Sentiment analysis task failed".to_string(),
                        })
                    }
                }
                task_index += 1;
            }
        }

        if self.config.enable_topic_modeling {
            if let Some(task) = tasks.get(task_index) {
                match task.await {
                    Ok(Ok(result)) => {
                        topic_result = Some(result);
                        enabled_components.push("topic_modeling".to_string());
                    }
                    Ok(Err(e)) => return Err(e),
                    Err(_) => {
                        return Err(SemanticAnalysisError::ParallelProcessingError {
                            reason: "Topic modeling task failed".to_string(),
                        })
                    }
                }
                task_index += 1;
            }
        }

        if self.config.enable_domain_analysis {
            if let Some(task) = tasks.get(task_index) {
                match task.await {
                    Ok(Ok(result)) => {
                        domain_result = Some(result);
                        enabled_components.push("domain_analysis".to_string());
                    }
                    Ok(Err(e)) => return Err(e),
                    Err(_) => {
                        return Err(SemanticAnalysisError::ParallelProcessingError {
                            reason: "Domain analysis task failed".to_string(),
                        })
                    }
                }
                task_index += 1;
            }
        }

        if self.config.enable_syntactic_analysis {
            if let Some(task) = tasks.get(task_index) {
                match task.await {
                    Ok(Ok(result)) => {
                        syntactic_result = Some(result);
                        enabled_components.push("syntactic_analysis".to_string());
                    }
                    Ok(Err(e)) => return Err(e),
                    Err(_) => {
                        return Err(SemanticAnalysisError::ParallelProcessingError {
                            reason: "Syntactic analysis task failed".to_string(),
                        })
                    }
                }
            }
        }

        // Metrics analysis
        let metrics_result = if self.config.enable_metrics_collection {
            enabled_components.push("metrics_collection".to_string());
            let metrics_start = Instant::now();

            // Collect scores for metrics analysis
            let similarity_scores = vec![similarity_result
                .as_ref()
                .map(|r| r.similarity_score)
                .unwrap_or(0.0)];
            let quality_scores = vec![0.8]; // Simplified
            let confidence_scores = vec![0.9]; // Simplified

            let mut metrics_analyzer = self.metrics_analyzer.lock().expect("lock should not be poisoned");
            let result = metrics_analyzer.analyze_metrics(
                &similarity_scores,
                &quality_scores,
                &confidence_scores,
            )?;
            drop(metrics_analyzer);

            performance.metrics_analysis_time = metrics_start.elapsed();
            Some(result)
        } else {
            None
        };

        // Calculate overall scores
        let overall_similarity = similarity_result
            .as_ref()
            .map(|r| r.similarity_score)
            .unwrap_or(0.0);
        let quality_score = metrics_result
            .as_ref()
            .map(|r| r.summary.quality_stats.mean)
            .unwrap_or(0.8);
        let confidence_score = metrics_result
            .as_ref()
            .map(|r| r.summary.confidence_stats.mean)
            .unwrap_or(0.9);

        // Calculate total processing time
        let total_time = start_time.elapsed();
        performance.total_time = total_time;

        // Create cache info
        let cache_info = {
            let cache_stats = self.cache_stats.lock().expect("lock should not be poisoned");
            cache_stats.clone()
        };

        let metadata = AnalysisMetadata {
            timestamp: SystemTime::now(),
            processing_time: total_time,
            config_used: format!("{:?}", self.config),
            enabled_components,
            text1_characteristics: text1_chars,
            text2_characteristics: text2_chars,
            performance_metrics: performance,
            cache_info,
        };

        Ok(SemanticAnalysisResult {
            overall_similarity,
            quality_score,
            confidence_score,
            similarity_analysis: similarity_result,
            feature_analysis: if features1.is_some() && features2.is_some() {
                Some((features1.expect("features1 verified to be Some"), features2.expect("features2 verified to be Some")))
            } else {
                None
            },
            sentiment_analysis: sentiment_result,
            topic_analysis: topic_result,
            domain_analysis: domain_result,
            syntactic_analysis: syntactic_result,
            metrics_analysis: metrics_result,
            metadata,
        })
    }

    /// Analyze batch of text pairs
    pub async fn analyze_batch(
        &mut self,
        text_pairs: &[(&str, &str)],
    ) -> Result<BatchAnalysisResult, SemanticAnalysisError> {
        let start_time = Instant::now();
        let mut results = Vec::new();
        let mut successful_analyses = 0;
        let mut failed_analyses = 0;
        let mut similarity_sum = 0.0;

        // Process in parallel if enabled
        if self.config.enable_parallel_processing && text_pairs.len() > 1 {
            let chunk_size = (text_pairs.len() + self.config.max_parallel_threads - 1)
                / self.config.max_parallel_threads;
            let chunks: Vec<_> = text_pairs.chunks(chunk_size).collect();

            let mut tasks = Vec::new();
            for chunk in chunks {
                let mut analyzer_clone = self.clone_for_parallel().await?;
                let chunk_vec = chunk.to_vec();

                tasks.push(task::spawn(async move {
                    let mut chunk_results = Vec::new();
                    for (text1, text2) in chunk_vec {
                        match analyzer_clone.analyze_comprehensive(text1, text2).await {
                            Ok(result) => chunk_results.push(Ok(result)),
                            Err(e) => chunk_results.push(Err(e)),
                        }
                    }
                    chunk_results
                }));
            }

            // Collect results from all tasks
            for task in tasks {
                match task.await {
                    Ok(chunk_results) => {
                        for result in chunk_results {
                            match result {
                                Ok(analysis_result) => {
                                    similarity_sum += analysis_result.overall_similarity;
                                    successful_analyses += 1;
                                    results.push(analysis_result);
                                }
                                Err(_) => {
                                    failed_analyses += 1;
                                }
                            }
                        }
                    }
                    Err(_) => {
                        failed_analyses += text_pairs.len();
                    }
                }
            }
        } else {
            // Sequential processing
            for (text1, text2) in text_pairs {
                match self.analyze_comprehensive(text1, text2).await {
                    Ok(result) => {
                        similarity_sum += result.overall_similarity;
                        successful_analyses += 1;
                        results.push(result);
                    }
                    Err(_) => {
                        failed_analyses += 1;
                    }
                }
            }
        }

        let total_time = start_time.elapsed();
        let average_similarity = if successful_analyses > 0 {
            similarity_sum / successful_analyses as f64
        } else {
            0.0
        };

        // Calculate distributions
        let similarity_distribution = calculate_distribution(&results, |r| r.overall_similarity);
        let quality_distribution = calculate_distribution(&results, |r| r.quality_score);

        let batch_statistics = BatchStatistics {
            total_items: text_pairs.len(),
            successful_analyses,
            failed_analyses,
            average_similarity,
            similarity_distribution,
            quality_distribution,
        };

        let performance_summary = BatchPerformance {
            total_time,
            average_time_per_item: if results.len() > 0 {
                total_time / results.len() as u32
            } else {
                Duration::ZERO
            },
            items_per_second: if total_time.as_secs_f64() > 0.0 {
                results.len() as f64 / total_time.as_secs_f64()
            } else {
                0.0
            },
            parallel_speedup: if self.config.enable_parallel_processing {
                calculate_speedup(text_pairs.len(), self.config.max_parallel_threads)
            } else {
                1.0
            },
            memory_usage: estimate_memory_usage(&results),
        };

        Ok(BatchAnalysisResult {
            results,
            batch_statistics,
            performance_summary,
        })
    }

    /// Analyze single text for characteristics
    pub async fn analyze_single(
        &mut self,
        text: &str,
    ) -> Result<SemanticAnalysisResult, SemanticAnalysisError> {
        // For single text analysis, we use an empty string as the second text
        // This allows us to extract features, sentiment, topics, and domain for a single text
        self.analyze_comprehensive(text, "").await
    }

    /// Check cache for existing result
    async fn check_cache(
        &self,
        text1: &str,
        text2: &str,
    ) -> Result<Option<SemanticAnalysisResult>, SemanticAnalysisError> {
        if !self.config.enable_caching {
            return Ok(None);
        }

        let cache_key = generate_cache_key(text1, text2);
        let cache = self.cache.read().expect("lock should not be poisoned");

        if let Some(entry) = cache.get(&cache_key) {
            // Update cache stats
            {
                let mut stats = self.cache_stats.lock().expect("lock should not be poisoned");
                stats.cache_hits += 1;
                stats.cache_hit_rate =
                    stats.cache_hits as f64 / (stats.cache_hits + stats.cache_misses) as f64;
            }

            return Ok(Some(entry.result.clone()));
        }

        // Cache miss
        {
            let mut stats = self.cache_stats.lock().expect("lock should not be poisoned");
            stats.cache_misses += 1;
            stats.cache_hit_rate =
                stats.cache_hits as f64 / (stats.cache_hits + stats.cache_misses) as f64;
        }

        Ok(None)
    }

    /// Cache analysis result
    async fn cache_result(
        &self,
        text1: &str,
        text2: &str,
        result: &SemanticAnalysisResult,
    ) -> Result<(), SemanticAnalysisError> {
        if !self.config.enable_caching {
            return Ok(());
        }

        let cache_key = generate_cache_key(text1, text2);
        let entry = CacheEntry {
            result: result.clone(),
            timestamp: SystemTime::now(),
            access_count: 1,
        };

        let mut cache = self.cache.write().expect("lock should not be poisoned");

        // Check if cache is full and evict if necessary
        if cache.len() >= self.config.cache_size_limit {
            evict_lru_entry(&mut cache);
        }

        cache.insert(cache_key, entry);

        // Update cache stats
        {
            let mut stats = self.cache_stats.lock().expect("lock should not be poisoned");
            stats.cache_size = cache.len();
        }

        Ok(())
    }

    /// Clone analyzer for parallel processing
    async fn clone_for_parallel(&self) -> Result<Self, SemanticAnalysisError> {
        // Create a new analyzer with the same configuration but separate instances
        Self::new(self.config.clone())
    }

    /// Analyze text characteristics
    fn analyze_text_characteristics(&self, text: &str) -> TextCharacteristics {
        let length = text.len();
        let words: Vec<&str> = text.split_whitespace().collect();
        let word_count = words.len();
        let sentence_count = text
            .matches(|c| c == '.' || c == '!' || c == '?')
            .count()
            .max(1);
        let paragraph_count = text.split("\n\n").count();

        // Simple complexity score based on average word length and sentence length
        let avg_word_length = if word_count > 0 {
            words.iter().map(|w| w.len()).sum::<usize>() as f64 / word_count as f64
        } else {
            0.0
        };
        let avg_sentence_length = word_count as f64 / sentence_count as f64;
        let complexity_score = (avg_word_length + avg_sentence_length) / 20.0; // Normalized

        // Simple readability score (inverse of complexity)
        let readability_score = 1.0 - (complexity_score.min(1.0));

        TextCharacteristics {
            length,
            word_count,
            sentence_count,
            paragraph_count,
            language_detected: Some("en".to_string()), // Simplified
            complexity_score,
            readability_score,
        }
    }

    /// Clear cache
    pub async fn clear_cache(&mut self) -> Result<(), SemanticAnalysisError> {
        let mut cache = self.cache.write().expect("lock should not be poisoned");
        cache.clear();

        let mut stats = self.cache_stats.lock().expect("lock should not be poisoned");
        stats.cache_size = 0;
        stats.cache_hits = 0;
        stats.cache_misses = 0;
        stats.cache_hit_rate = 0.0;

        Ok(())
    }

    /// Get cache statistics
    pub fn get_cache_stats(&self) -> CacheInfo {
        let stats = self.cache_stats.lock().expect("lock should not be poisoned");
        stats.clone()
    }

    /// Update configuration
    pub fn update_config(
        &mut self,
        config: SemanticAnalysisConfig,
    ) -> Result<(), SemanticAnalysisError> {
        config.validate()?;
        self.config = config;
        Ok(())
    }

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

// Helper functions

/// Generate cache key from two texts
fn generate_cache_key(text1: &str, text2: &str) -> String {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};

    let mut hasher = DefaultHasher::new();
    text1.hash(&mut hasher);
    text2.hash(&mut hasher);
    format!("{:x}", hasher.finish())
}

/// Evict least recently used cache entry
fn evict_lru_entry(cache: &mut HashMap<String, CacheEntry>) {
    if let Some((key_to_remove, _)) = cache.iter().min_by_key(|(_, entry)| entry.timestamp) {
        let key_to_remove = key_to_remove.clone();
        cache.remove(&key_to_remove);
    }
}

/// Calculate parallel efficiency
fn calculate_parallel_efficiency(parallel_time: Duration, num_tasks: usize) -> f64 {
    if num_tasks <= 1 {
        return 1.0;
    }

    // Simplified efficiency calculation
    let theoretical_speedup = num_tasks as f64;
    let actual_efficiency = 0.8; // Assume 80% efficiency with overhead
    actual_efficiency.min(1.0)
}

/// Calculate distribution of values
fn calculate_distribution<F>(
    results: &[SemanticAnalysisResult],
    value_fn: F,
) -> HashMap<String, usize>
where
    F: Fn(&SemanticAnalysisResult) -> f64,
{
    let mut distribution = HashMap::new();
    let ranges = vec![
        ("0.0-0.1", 0.0..0.1),
        ("0.1-0.2", 0.1..0.2),
        ("0.2-0.3", 0.2..0.3),
        ("0.3-0.4", 0.3..0.4),
        ("0.4-0.5", 0.4..0.5),
        ("0.5-0.6", 0.5..0.6),
        ("0.6-0.7", 0.6..0.7),
        ("0.7-0.8", 0.7..0.8),
        ("0.8-0.9", 0.8..0.9),
        ("0.9-1.0", 0.9..=1.0),
    ];

    for result in results {
        let value = value_fn(result);
        for (range_name, range) in &ranges {
            if range.contains(&value) {
                *distribution.entry(range_name.to_string()).or_insert(0) += 1;
                break;
            }
        }
    }

    distribution
}

/// Calculate parallel speedup
fn calculate_speedup(total_items: usize, max_threads: usize) -> f64 {
    if max_threads <= 1 {
        return 1.0;
    }

    let effective_threads = max_threads.min(total_items);
    effective_threads as f64 * 0.8 // Assume 80% efficiency
}

/// Estimate memory usage
fn estimate_memory_usage(results: &[SemanticAnalysisResult]) -> usize {
    results.len() * 10240 // Estimate 10KB per result
}

/// Convenience functions

/// Analyze similarity between two texts with default configuration
pub async fn analyze_similarity(
    text1: &str,
    text2: &str,
) -> Result<SemanticAnalysisResult, SemanticAnalysisError> {
    let mut analyzer = SemanticAnalyzer::default()?;
    analyzer.analyze_comprehensive(text1, text2).await
}

/// Analyze similarity with custom configuration
pub async fn analyze_similarity_with_config(
    text1: &str,
    text2: &str,
    config: SemanticAnalysisConfig,
) -> Result<SemanticAnalysisResult, SemanticAnalysisError> {
    let mut analyzer = SemanticAnalyzer::new(config)?;
    analyzer.analyze_comprehensive(text1, text2).await
}

/// Analyze batch of text pairs with default configuration
pub async fn analyze_batch(
    text_pairs: &[(&str, &str)],
) -> Result<BatchAnalysisResult, SemanticAnalysisError> {
    let mut analyzer = SemanticAnalyzer::default()?;
    analyzer.analyze_batch(text_pairs).await
}

/// Quick similarity check (simplified analysis)
pub async fn quick_similarity(text1: &str, text2: &str) -> Result<f64, SemanticAnalysisError> {
    let config = SemanticAnalysisConfig::builder()
        .enable_similarity_analysis(true)
        .enable_feature_extraction(true)
        .enable_sentiment_analysis(false)
        .enable_topic_modeling(false)
        .enable_domain_analysis(false)
        .enable_syntactic_analysis(false)
        .enable_metrics_collection(false)
        .build()?;

    let result = analyze_similarity_with_config(text1, text2, config).await?;
    Ok(result.overall_similarity)
}

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

    #[test]
    fn test_semantic_analyzer_creation() {
        let analyzer = SemanticAnalyzer::default();
        assert!(analyzer.is_ok());
    }

    #[test]
    fn test_configuration_builder() {
        let config = SemanticAnalysisConfig::builder()
            .enable_similarity_analysis(true)
            .enable_sentiment_analysis(false)
            .similarity_algorithm(SimilarityAlgorithm::Cosine)
            .enable_caching(true)
            .cache_size_limit(500)
            .enable_parallel_processing(true)
            .max_parallel_threads(4)
            .timeout_seconds(60)
            .quality_threshold(0.5)
            .build();

        assert!(config.is_ok());
        let config = config.expect("operation should succeed");
        assert!(config.enable_similarity_analysis);
        assert!(!config.enable_sentiment_analysis);
        assert_eq!(config.cache_size_limit, 500);
        assert_eq!(config.max_parallel_threads, 4);
    }

    #[test]
    fn test_invalid_configuration() {
        let config = SemanticAnalysisConfig::builder()
            .enable_caching(true)
            .cache_size_limit(0) // Invalid: cache enabled but size 0
            .build();

        assert!(config.is_err());
    }

    #[tokio::test]
    async fn test_comprehensive_analysis() {
        let mut analyzer = SemanticAnalyzer::default().expect("Semantic Analyzer should succeed");
        let text1 = "The quick brown fox jumps over the lazy dog.";
        let text2 = "A fast brown fox leaps across the sleeping dog.";

        let result = analyzer.analyze_comprehensive(text1, text2).await;
        assert!(result.is_ok());

        let result = result.expect("operation should succeed");
        assert!(result.overall_similarity >= 0.0 && result.overall_similarity <= 1.0);
        assert!(result.quality_score >= 0.0 && result.quality_score <= 1.0);
        assert!(result.confidence_score >= 0.0 && result.confidence_score <= 1.0);
    }

    #[tokio::test]
    async fn test_single_text_analysis() {
        let mut analyzer = SemanticAnalyzer::default().expect("Semantic Analyzer should succeed");
        let text = "This is a sample text for analysis.";

        let result = analyzer.analyze_single(text).await;
        assert!(result.is_ok());

        let result = result.expect("operation should succeed");
        assert_eq!(result.metadata.text1_characteristics.word_count, 7);
    }

    #[tokio::test]
    async fn test_batch_analysis() {
        let mut analyzer = SemanticAnalyzer::default().expect("Semantic Analyzer should succeed");
        let text_pairs = vec![
            ("Hello world", "Hi there"),
            ("Technical document", "Academic paper"),
            ("Simple sentence", "Complex paragraph with multiple clauses"),
        ];

        let result = analyzer.analyze_batch(&text_pairs).await;
        assert!(result.is_ok());

        let result = result.expect("operation should succeed");
        assert_eq!(result.batch_statistics.total_items, 3);
        assert!(result.batch_statistics.successful_analyses > 0);
    }

    #[tokio::test]
    async fn test_caching() {
        let config = SemanticAnalysisConfig::builder()
            .enable_caching(true)
            .cache_size_limit(10)
            .build()
            .expect("operation should succeed");

        let mut analyzer = SemanticAnalyzer::new(config).expect("Semantic Analyzer should succeed");
        let text1 = "Sample text";
        let text2 = "Another text";

        // First analysis should be a cache miss
        let _result1 = analyzer.analyze_comprehensive(text1, text2).await.expect("operation should succeed");
        let stats1 = analyzer.get_cache_stats();

        // Second analysis should be a cache hit
        let _result2 = analyzer.analyze_comprehensive(text1, text2).await.expect("operation should succeed");
        let stats2 = analyzer.get_cache_stats();

        assert!(stats2.cache_hits > stats1.cache_hits);
    }

    #[tokio::test]
    async fn test_convenience_functions() {
        let text1 = "The weather is nice today.";
        let text2 = "It's a beautiful day outside.";

        let result = analyze_similarity(text1, text2).await;
        assert!(result.is_ok());

        let similarity = quick_similarity(text1, text2).await;
        assert!(similarity.is_ok());
        assert!(similarity.expect("operation should succeed") >= 0.0);
    }

    #[tokio::test]
    async fn test_timeout_handling() {
        let config = SemanticAnalysisConfig::builder()
            .timeout_seconds(1) // Very short timeout
            .build()
            .expect("operation should succeed");

        let mut analyzer = SemanticAnalyzer::new(config).expect("Semantic Analyzer should succeed");

        // Use very long texts that might cause timeout
        let long_text = "word ".repeat(10000);
        let result = analyzer.analyze_comprehensive(&long_text, &long_text).await;

        // Result might be ok if processing is fast enough, or timeout error
        match result {
            Ok(_) => {}                                           // Analysis completed within timeout
            Err(SemanticAnalysisError::TimeoutError { .. }) => {} // Expected timeout
            Err(e) => panic!("Unexpected error: {:?}", e),
        }
    }

    #[test]
    fn test_text_characteristics() {
        let analyzer = SemanticAnalyzer::default().expect("Semantic Analyzer should succeed");
        let text = "This is a sample sentence. It has multiple words and punctuation!";
        let characteristics = analyzer.analyze_text_characteristics(text);

        assert_eq!(characteristics.sentence_count, 2);
        assert!(characteristics.word_count > 0);
        assert!(characteristics.complexity_score >= 0.0);
        assert!(characteristics.readability_score >= 0.0);
    }

    #[test]
    fn test_cache_key_generation() {
        let key1 = generate_cache_key("text1", "text2");
        let key2 = generate_cache_key("text1", "text2");
        let key3 = generate_cache_key("text2", "text1");

        assert_eq!(key1, key2); // Same texts should generate same key
        assert_ne!(key1, key3); // Different order should generate different key
    }

    #[test]
    fn test_distribution_calculation() {
        let results = vec![SemanticAnalysisResult {
            overall_similarity: 0.1,
            quality_score: 0.8,
            confidence_score: 0.9,
            similarity_analysis: None,
            feature_analysis: None,
            sentiment_analysis: None,
            topic_analysis: None,
            domain_analysis: None,
            syntactic_analysis: None,
            metrics_analysis: None,
            metadata: AnalysisMetadata {
                timestamp: SystemTime::now(),
                processing_time: Duration::from_millis(100),
                config_used: "test".to_string(),
                enabled_components: vec![],
                text1_characteristics: TextCharacteristics {
                    length: 10,
                    word_count: 2,
                    sentence_count: 1,
                    paragraph_count: 1,
                    language_detected: None,
                    complexity_score: 0.5,
                    readability_score: 0.5,
                },
                text2_characteristics: TextCharacteristics {
                    length: 10,
                    word_count: 2,
                    sentence_count: 1,
                    paragraph_count: 1,
                    language_detected: None,
                    complexity_score: 0.5,
                    readability_score: 0.5,
                },
                performance_metrics: PerformanceInfo {
                    total_time: Duration::from_millis(100),
                    similarity_time: Duration::ZERO,
                    feature_extraction_time: Duration::ZERO,
                    sentiment_analysis_time: Duration::ZERO,
                    topic_modeling_time: Duration::ZERO,
                    domain_analysis_time: Duration::ZERO,
                    syntactic_analysis_time: Duration::ZERO,
                    metrics_analysis_time: Duration::ZERO,
                    parallel_efficiency: 1.0,
                },
                cache_info: CacheInfo {
                    cache_hits: 0,
                    cache_misses: 0,
                    cache_hit_rate: 0.0,
                    cache_size: 0,
                },
            },
        }];

        let distribution = calculate_distribution(&results, |r| r.overall_similarity);
        assert!(distribution.contains_key("0.0-0.1"));
        assert_eq!(distribution["0.0-0.1"], 1);
    }

    #[tokio::test]
    async fn test_cache_clearing() {
        let config = SemanticAnalysisConfig::builder()
            .enable_caching(true)
            .build()
            .expect("operation should succeed");

        let mut analyzer = SemanticAnalyzer::new(config).expect("Semantic Analyzer should succeed");

        // Add something to cache
        let _ = analyzer.analyze_comprehensive("test1", "test2").await;
        assert!(
            analyzer.get_cache_stats().cache_size > 0
                || analyzer.get_cache_stats().cache_misses > 0
        );

        // Clear cache
        let _ = analyzer.clear_cache().await;
        let stats = analyzer.get_cache_stats();
        assert_eq!(stats.cache_size, 0);
        assert_eq!(stats.cache_hits, 0);
        assert_eq!(stats.cache_misses, 0);
    }
}