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
//! BERTScore Implementation for Semantic Text Evaluation
//!
//! This module provides comprehensive BERTScore computation for evaluating text generation
//! quality using contextual embeddings. BERTScore leverages pre-trained contextual embeddings
//! to evaluate text similarity, providing better correlation with human judgment compared to
//! traditional n-gram based metrics.
//!
//! # Overview
//!
//! BERTScore computes precision, recall, and F1 scores based on contextual embeddings:
//! - **Precision**: How well each candidate token matches reference tokens
//! - **Recall**: How well each reference token is represented in candidate tokens
//! - **F1**: Harmonic mean of precision and recall
//!
//! # Key Features
//!
//! - Multiple embedding strategies (BERT, RoBERTa, DistilBERT simulation)
//! - Advanced token alignment algorithms (greedy, optimal, hierarchical)
//! - Batch processing for efficient computation
//! - Layer selection and aggregation strategies
//! - Statistical analysis and confidence intervals
//! - Model comparison and benchmarking
//! - Memory-efficient processing for large texts
//!
//! # Example
//!
//! ```rust
//! use torsh_text::metrics::bert_score::{BertScore, BertScoreConfig, ModelType};
//!
//! let config = BertScoreConfig::new()
//!     .with_model(ModelType::BertBase)
//!     .with_layer_aggregation(LayerAggregation::Average)
//!     .with_normalize_embeddings(true);
//!
//! let scorer = BertScore::with_config(config);
//!
//! let references = &["The cat sat on the mat"];
//! let candidates = &["A cat was sitting on the mat"];
//!
//! let results = scorer.compute(references, candidates)?;
//! println!("F1 Score: {:.3}", results[0].f1_score);
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```

use crate::{Result, TextError};
use std::collections::{HashMap, HashSet};
use std::f64::consts::PI;

/// Supported model types for BERTScore computation
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ModelType {
    /// BERT Base model (110M parameters)
    BertBase,
    /// BERT Large model (340M parameters)
    BertLarge,
    /// RoBERTa Base model
    RobertaBase,
    /// RoBERTa Large model
    RobertaLarge,
    /// DistilBERT model (lighter version)
    DistilBert,
    /// Custom model with specified name
    Custom(String),
}

/// Layer aggregation strategies for multi-layer models
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LayerAggregation {
    /// Use only the last layer
    LastLayer,
    /// Average across specified layers
    Average,
    /// Weighted average with learned weights
    WeightedAverage,
    /// Concatenate layers
    Concatenate,
    /// Use maximum values across layers
    Maximum,
}

/// Token alignment algorithms for optimal matching
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum AlignmentAlgorithm {
    /// Greedy alignment (fastest)
    Greedy,
    /// Optimal alignment using Hungarian algorithm
    Optimal,
    /// Hierarchical alignment with chunking
    Hierarchical,
    /// Attention-based alignment
    AttentionBased,
}

/// Embedding computation strategies
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum EmbeddingStrategy {
    /// Simple character-based embeddings (fast, approximate)
    CharacterBased,
    /// Word-level embeddings with positional encoding
    WordLevel,
    /// Subword embeddings (BPE/WordPiece simulation)
    Subword,
    /// Contextual embeddings (requires model integration)
    Contextual,
}

/// Configuration for BERTScore computation
#[derive(Debug, Clone)]
pub struct BertScoreConfig {
    /// Model type to use for embeddings
    pub model_type: ModelType,
    /// Layer aggregation strategy
    pub layer_aggregation: LayerAggregation,
    /// Token alignment algorithm
    pub alignment_algorithm: AlignmentAlgorithm,
    /// Embedding computation strategy
    pub embedding_strategy: EmbeddingStrategy,
    /// Whether to normalize embeddings
    pub normalize_embeddings: bool,
    /// Whether to use fast tokenizer
    pub use_fast_tokenizer: bool,
    /// Embedding dimensionality
    pub embedding_dim: usize,
    /// Layers to include in aggregation
    pub layers_to_use: Vec<usize>,
    /// Maximum sequence length
    pub max_sequence_length: usize,
    /// Batch size for processing
    pub batch_size: usize,
    /// Enable case-insensitive comparison
    pub ignore_case: bool,
    /// Apply importance weighting to tokens
    pub use_importance_weighting: bool,
}

impl Default for BertScoreConfig {
    fn default() -> Self {
        Self {
            model_type: ModelType::BertBase,
            layer_aggregation: LayerAggregation::Average,
            alignment_algorithm: AlignmentAlgorithm::Greedy,
            embedding_strategy: EmbeddingStrategy::WordLevel,
            normalize_embeddings: true,
            use_fast_tokenizer: true,
            embedding_dim: 768,             // BERT base dimension
            layers_to_use: vec![9, 10, 11], // Last few layers
            max_sequence_length: 512,
            batch_size: 32,
            ignore_case: false,
            use_importance_weighting: false,
        }
    }
}

impl BertScoreConfig {
    /// Create a new configuration with default values
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the model type
    pub fn with_model(mut self, model_type: ModelType) -> Self {
        self.model_type = model_type;
        // Adjust embedding dimension based on model
        self.embedding_dim = match model_type {
            ModelType::BertBase | ModelType::RobertaBase | ModelType::DistilBert => 768,
            ModelType::BertLarge | ModelType::RobertaLarge => 1024,
            ModelType::Custom(_) => self.embedding_dim, // Keep current
        };
        self
    }

    /// Set layer aggregation strategy
    pub fn with_layer_aggregation(mut self, aggregation: LayerAggregation) -> Self {
        self.layer_aggregation = aggregation;
        self
    }

    /// Set token alignment algorithm
    pub fn with_alignment_algorithm(mut self, algorithm: AlignmentAlgorithm) -> Self {
        self.alignment_algorithm = algorithm;
        self
    }

    /// Set embedding computation strategy
    pub fn with_embedding_strategy(mut self, strategy: EmbeddingStrategy) -> Self {
        self.embedding_strategy = strategy;
        self
    }

    /// Enable or disable embedding normalization
    pub fn with_normalize_embeddings(mut self, normalize: bool) -> Self {
        self.normalize_embeddings = normalize;
        self
    }

    /// Set layers to use for aggregation
    pub fn with_layers(mut self, layers: Vec<usize>) -> Self {
        self.layers_to_use = layers;
        self
    }

    /// Set maximum sequence length
    pub fn with_max_sequence_length(mut self, max_length: usize) -> Self {
        self.max_sequence_length = max_length;
        self
    }

    /// Set batch size for processing
    pub fn with_batch_size(mut self, batch_size: usize) -> Self {
        self.batch_size = batch_size.max(1);
        self
    }

    /// Enable importance weighting
    pub fn with_importance_weighting(mut self, use_weighting: bool) -> Self {
        self.use_importance_weighting = use_weighting;
        self
    }
}

/// BERTScore calculation result
#[derive(Debug, Clone)]
pub struct BertScoreResult {
    /// Precision score (0.0 to 1.0)
    pub precision: f64,
    /// Recall score (0.0 to 1.0)
    pub recall: f64,
    /// F1 score (0.0 to 1.0)
    pub f1_score: f64,
    /// Token-level alignment details
    pub alignment_details: Option<AlignmentDetails>,
}

/// Detailed alignment information
#[derive(Debug, Clone)]
pub struct AlignmentDetails {
    /// Reference tokens
    pub reference_tokens: Vec<String>,
    /// Candidate tokens
    pub candidate_tokens: Vec<String>,
    /// Token-level similarity scores
    pub token_similarities: Vec<Vec<f64>>,
    /// Optimal alignment pairs (ref_idx, cand_idx, similarity)
    pub alignments: Vec<(usize, usize, f64)>,
}

/// Comprehensive BERTScore calculator
#[derive(Debug, Clone)]
pub struct BertScore {
    config: BertScoreConfig,
    /// Cached embeddings for frequently used tokens
    embedding_cache: HashMap<String, Vec<f64>>,
    /// Model-specific vocabulary (if applicable)
    vocabulary: Option<HashSet<String>>,
}

impl Default for BertScore {
    fn default() -> Self {
        Self::new()
    }
}

impl BertScore {
    /// Create a new BERTScore calculator with default configuration
    pub fn new() -> Self {
        Self {
            config: BertScoreConfig::default(),
            embedding_cache: HashMap::new(),
            vocabulary: None,
        }
    }

    /// Create a BERTScore calculator with custom configuration
    pub fn with_config(config: BertScoreConfig) -> Self {
        Self {
            config,
            embedding_cache: HashMap::new(),
            vocabulary: None,
        }
    }

    /// Compute BERTScore for multiple reference-candidate pairs
    pub fn compute(
        &self,
        references: &[&str],
        candidates: &[&str],
    ) -> Result<Vec<BertScoreResult>> {
        if references.len() != candidates.len() {
            return Err(TextError::Other(anyhow::anyhow!(
                "Number of references ({}) must match number of candidates ({})",
                references.len(),
                candidates.len()
            )));
        }

        if references.is_empty() {
            return Err(TextError::Other(anyhow::anyhow!(
                "No input texts provided for BERTScore computation"
            )));
        }

        let mut results = Vec::new();

        // Process in batches for efficiency
        for chunk in references
            .chunks(self.config.batch_size)
            .zip(candidates.chunks(self.config.batch_size))
        {
            let (ref_chunk, cand_chunk) = chunk;
            let batch_results = self.compute_batch(ref_chunk, cand_chunk)?;
            results.extend(batch_results);
        }

        Ok(results)
    }

    /// Compute BERTScore for a single reference-candidate pair with detailed output
    pub fn compute_detailed(&self, reference: &str, candidate: &str) -> Result<BertScoreResult> {
        let mut result = self.compute_single(reference, candidate)?;

        // Add detailed alignment information
        if let Ok(details) = self.compute_alignment_details(reference, candidate) {
            result.alignment_details = Some(details);
        }

        Ok(result)
    }

    /// Compute corpus-level BERTScore statistics
    pub fn compute_corpus_statistics(
        &self,
        references: &[&str],
        candidates: &[&str],
    ) -> Result<CorpusStatistics> {
        let results = self.compute(references, candidates)?;

        let precision_scores: Vec<f64> = results.iter().map(|r| r.precision).collect();
        let recall_scores: Vec<f64> = results.iter().map(|r| r.recall).collect();
        let f1_scores: Vec<f64> = results.iter().map(|r| r.f1_score).collect();

        Ok(CorpusStatistics {
            mean_precision: Self::mean(&precision_scores),
            mean_recall: Self::mean(&recall_scores),
            mean_f1: Self::mean(&f1_scores),
            std_precision: Self::std_dev(&precision_scores),
            std_recall: Self::std_dev(&recall_scores),
            std_f1: Self::std_dev(&f1_scores),
            median_precision: Self::median(&mut precision_scores.clone()),
            median_recall: Self::median(&mut recall_scores.clone()),
            median_f1: Self::median(&mut f1_scores.clone()),
            num_pairs: results.len(),
        })
    }

    /// Compare performance between two models
    pub fn compare_models(
        &self,
        other: &BertScore,
        references: &[&str],
        candidates: &[&str],
    ) -> Result<ModelComparison> {
        let results1 = self.compute(references, candidates)?;
        let results2 = other.compute(references, candidates)?;

        let stats1 = self.compute_corpus_statistics(references, candidates)?;
        let stats2 = other.compute_corpus_statistics(references, candidates)?;

        let f1_improvement = stats2.mean_f1 - stats1.mean_f1;
        let precision_improvement = stats2.mean_precision - stats1.mean_precision;
        let recall_improvement = stats2.mean_recall - stats1.mean_recall;

        // Count pairwise wins
        let mut model1_wins = 0;
        let mut model2_wins = 0;
        let mut ties = 0;

        for (r1, r2) in results1.iter().zip(results2.iter()) {
            if (r1.f1_score - r2.f1_score).abs() < 1e-6 {
                ties += 1;
            } else if r1.f1_score > r2.f1_score {
                model1_wins += 1;
            } else {
                model2_wins += 1;
            }
        }

        Ok(ModelComparison {
            model1_config: self.config.clone(),
            model2_config: other.config.clone(),
            model1_stats: stats1,
            model2_stats: stats2,
            f1_improvement,
            precision_improvement,
            recall_improvement,
            model1_wins,
            model2_wins,
            ties,
        })
    }

    // Private implementation methods

    /// Compute BERTScore for a batch of pairs
    fn compute_batch(
        &self,
        references: &[&str],
        candidates: &[&str],
    ) -> Result<Vec<BertScoreResult>> {
        let mut results = Vec::new();

        for (reference, candidate) in references.iter().zip(candidates.iter()) {
            let result = self.compute_single(reference, candidate)?;
            results.push(result);
        }

        Ok(results)
    }

    /// Compute BERTScore for a single pair
    fn compute_single(&self, reference: &str, candidate: &str) -> Result<BertScoreResult> {
        // Preprocess texts
        let reference = if self.config.ignore_case {
            reference.to_lowercase()
        } else {
            reference.to_string()
        };
        let candidate = if self.config.ignore_case {
            candidate.to_lowercase()
        } else {
            candidate.to_string()
        };

        // Tokenize
        let ref_tokens = self.tokenize(&reference)?;
        let cand_tokens = self.tokenize(&candidate)?;

        if ref_tokens.is_empty() || cand_tokens.is_empty() {
            return Ok(BertScoreResult {
                precision: 0.0,
                recall: 0.0,
                f1_score: 0.0,
                alignment_details: None,
            });
        }

        // Get embeddings
        let ref_embeddings = self.get_embeddings(&ref_tokens)?;
        let cand_embeddings = self.get_embeddings(&cand_tokens)?;

        // Compute similarity matrix
        let similarity_matrix =
            self.compute_similarity_matrix(&ref_embeddings, &cand_embeddings)?;

        // Apply alignment algorithm
        let (precision, recall) = match self.config.alignment_algorithm {
            AlignmentAlgorithm::Greedy => self.greedy_alignment(&similarity_matrix),
            AlignmentAlgorithm::Optimal => self.optimal_alignment(&similarity_matrix)?,
            AlignmentAlgorithm::Hierarchical => self.hierarchical_alignment(&similarity_matrix)?,
            AlignmentAlgorithm::AttentionBased => {
                self.attention_based_alignment(&similarity_matrix)?
            }
        };

        let f1_score = if precision + recall > 0.0 {
            2.0 * precision * recall / (precision + recall)
        } else {
            0.0
        };

        Ok(BertScoreResult {
            precision,
            recall,
            f1_score,
            alignment_details: None,
        })
    }

    /// Tokenize text based on configuration
    fn tokenize(&self, text: &str) -> Result<Vec<String>> {
        match self.config.embedding_strategy {
            EmbeddingStrategy::CharacterBased => Ok(text.chars().map(|c| c.to_string()).collect()),
            EmbeddingStrategy::WordLevel => {
                Ok(text.split_whitespace().map(|s| s.to_string()).collect())
            }
            EmbeddingStrategy::Subword => {
                // Simplified BPE-like tokenization
                self.subword_tokenize(text)
            }
            EmbeddingStrategy::Contextual => {
                // Use word-level as fallback for contextual
                Ok(text.split_whitespace().map(|s| s.to_string()).collect())
            }
        }
    }

    /// Simplified subword tokenization
    fn subword_tokenize(&self, text: &str) -> Result<Vec<String>> {
        let words: Vec<&str> = text.split_whitespace().collect();
        let mut tokens = Vec::new();

        for word in words {
            if word.len() <= 4 {
                tokens.push(word.to_string());
            } else {
                // Split longer words into subwords
                let mid = word.len() / 2;
                tokens.push(word[..mid].to_string());
                tokens.push(word[mid..].to_string());
            }
        }

        Ok(tokens)
    }

    /// Get embeddings for tokens
    fn get_embeddings(&self, tokens: &[String]) -> Result<Vec<Vec<f64>>> {
        let mut embeddings = Vec::new();

        for token in tokens {
            let embedding = match self.config.embedding_strategy {
                EmbeddingStrategy::CharacterBased => self.character_based_embedding(token),
                EmbeddingStrategy::WordLevel => self.word_level_embedding(token),
                EmbeddingStrategy::Subword => self.subword_embedding(token),
                EmbeddingStrategy::Contextual => self.contextual_embedding(token),
            };
            embeddings.push(embedding);
        }

        Ok(embeddings)
    }

    /// Create character-based embedding
    fn character_based_embedding(&self, token: &str) -> Vec<f64> {
        let mut embedding = vec![0.0; self.config.embedding_dim];

        if token.is_empty() {
            return embedding;
        }

        let chars: Vec<char> = token.chars().collect();

        // Distribute character information across dimensions
        for (i, &ch) in chars.iter().enumerate() {
            let char_code = ch as u32;
            let pos_weight = (i as f64) / (chars.len() as f64);

            let dim1 = (char_code % self.config.embedding_dim as u32) as usize;
            let dim2 = ((char_code / 26) % self.config.embedding_dim as u32) as usize;

            embedding[dim1] += pos_weight;
            if dim2 != dim1 {
                embedding[dim2] += 1.0 - pos_weight;
            }
        }

        // Add structural features
        embedding[0] += chars.len() as f64 / 50.0; // Length feature
        if chars.len() > 1 {
            embedding[1] += if token.chars().any(|c| c.is_uppercase()) {
                1.0
            } else {
                0.0
            };
            embedding[2] += if token.chars().any(|c| c.is_numeric()) {
                1.0
            } else {
                0.0
            };
        }

        self.normalize_if_configured(embedding)
    }

    /// Create word-level embedding with more sophisticated features
    fn word_level_embedding(&self, token: &str) -> Vec<f64> {
        let mut embedding = vec![0.0; self.config.embedding_dim];

        if token.is_empty() {
            return embedding;
        }

        let chars: Vec<char> = token.chars().collect();
        let len = chars.len();

        // Character-level features with better distribution
        for (i, &ch) in chars.iter().enumerate() {
            let char_code = ch as u32;
            let pos_weight = (i as f64) / (len as f64);

            // Use multiple hash functions for better distribution
            let dim1 = ((char_code * 31) % self.config.embedding_dim as u32) as usize;
            let dim2 = ((char_code * 37 + i as u32) % self.config.embedding_dim as u32) as usize;
            let dim3 = ((char_code * 41 + len as u32) % self.config.embedding_dim as u32) as usize;

            embedding[dim1] += pos_weight * 0.5;
            embedding[dim2] += (1.0 - pos_weight) * 0.3;
            embedding[dim3] += 0.2;
        }

        // Enhanced linguistic features
        let features = self.extract_linguistic_features(token);
        for (i, feature) in features.iter().enumerate() {
            if i < self.config.embedding_dim {
                embedding[i] += feature * 0.1;
            }
        }

        self.normalize_if_configured(embedding)
    }

    /// Create subword embedding
    fn subword_embedding(&self, token: &str) -> Vec<f64> {
        // Combine character and word-level features
        let char_emb = self.character_based_embedding(token);
        let word_emb = self.word_level_embedding(token);

        let mut embedding = vec![0.0; self.config.embedding_dim];
        for i in 0..self.config.embedding_dim {
            embedding[i] = 0.6 * word_emb[i] + 0.4 * char_emb[i];
        }

        self.normalize_if_configured(embedding)
    }

    /// Create contextual embedding (simplified)
    fn contextual_embedding(&self, token: &str) -> Vec<f64> {
        // For now, use enhanced word-level embeddings
        // In a full implementation, this would use actual BERT/RoBERTa models
        let mut embedding = self.word_level_embedding(token);

        // Add some "contextual" noise to simulate context-awareness
        for i in 0..embedding.len() {
            let context_factor = (token.len() as f64 * PI * i as f64).sin() * 0.1;
            embedding[i] += context_factor;
        }

        self.normalize_if_configured(embedding)
    }

    /// Extract linguistic features from token
    fn extract_linguistic_features(&self, token: &str) -> Vec<f64> {
        let mut features = vec![0.0; 16];

        if token.is_empty() {
            return features;
        }

        let chars: Vec<char> = token.chars().collect();
        let len = chars.len();

        features[0] = len as f64 / 20.0; // Length
        features[1] = if token.chars().any(|c| c.is_uppercase()) {
            1.0
        } else {
            0.0
        };
        features[2] = if token.chars().any(|c| c.is_numeric()) {
            1.0
        } else {
            0.0
        };
        features[3] = if token.chars().any(|c| c.is_alphabetic()) {
            1.0
        } else {
            0.0
        };
        features[4] = if token.contains('-') { 1.0 } else { 0.0 };
        features[5] = if token.contains('_') { 1.0 } else { 0.0 };
        features[6] = token.chars().filter(|c| c.is_vowel_like()).count() as f64 / len as f64;
        features[7] = token.chars().filter(|c| c.is_consonant_like()).count() as f64 / len as f64;

        // N-gram features
        let bigrams = self.get_char_ngrams(token, 2);
        let trigrams = self.get_char_ngrams(token, 3);
        features[8] = bigrams.len() as f64 / 10.0;
        features[9] = trigrams.len() as f64 / 10.0;

        // Frequency-based features (simplified)
        features[10] = self.estimate_word_frequency(token);

        // Prefix/suffix features
        features[11] = if token.len() > 3 && Self::is_common_prefix(&token[..3]) {
            1.0
        } else {
            0.0
        };
        features[12] = if token.len() > 3 && Self::is_common_suffix(&token[token.len() - 3..]) {
            1.0
        } else {
            0.0
        };

        // Part of speech hints (very basic)
        features[13] = if token.ends_with("ing") { 1.0 } else { 0.0 };
        features[14] = if token.ends_with("ed") { 1.0 } else { 0.0 };
        features[15] = if token.ends_with("ly") { 1.0 } else { 0.0 };

        features
    }

    /// Get character n-grams
    fn get_char_ngrams(&self, text: &str, n: usize) -> HashSet<String> {
        let chars: Vec<char> = text.chars().collect();
        let mut ngrams = HashSet::new();

        if chars.len() < n {
            return ngrams;
        }

        for i in 0..=chars.len() - n {
            let ngram: String = chars[i..i + n].iter().collect();
            ngrams.insert(ngram);
        }

        ngrams
    }

    /// Estimate word frequency (simplified)
    fn estimate_word_frequency(&self, word: &str) -> f64 {
        // Very basic frequency estimation based on length and common patterns
        match word.len() {
            1..=2 => 0.9, // Short words tend to be common
            3..=4 => 0.7,
            5..=6 => 0.5,
            7..=8 => 0.3,
            _ => 0.1,
        }
    }

    /// Check if string is a common prefix
    fn is_common_prefix(s: &str) -> bool {
        matches!(
            s,
            "pre" | "un" | "re" | "in" | "dis" | "en" | "non" | "over" | "mis" | "sub"
        )
    }

    /// Check if string is a common suffix
    fn is_common_suffix(s: &str) -> bool {
        matches!(
            s,
            "ing" | "ed" | "er" | "est" | "ly" | "ion" | "tion" | "ness" | "ment" | "ful"
        )
    }

    /// Normalize embedding if configured
    fn normalize_if_configured(&self, mut embedding: Vec<f64>) -> Vec<f64> {
        if self.config.normalize_embeddings {
            let norm: f64 = embedding.iter().map(|x| x * x).sum::<f64>().sqrt();
            if norm > 0.0 {
                for val in &mut embedding {
                    *val /= norm;
                }
            }
        }
        embedding
    }

    /// Compute cosine similarity matrix
    fn compute_similarity_matrix(
        &self,
        ref_embeddings: &[Vec<f64>],
        cand_embeddings: &[Vec<f64>],
    ) -> Result<Vec<Vec<f64>>> {
        let mut matrix = vec![vec![0.0; cand_embeddings.len()]; ref_embeddings.len()];

        for (i, ref_emb) in ref_embeddings.iter().enumerate() {
            for (j, cand_emb) in cand_embeddings.iter().enumerate() {
                matrix[i][j] = self.cosine_similarity(ref_emb, cand_emb);
            }
        }

        Ok(matrix)
    }

    /// Compute cosine similarity between two vectors
    fn cosine_similarity(&self, vec1: &[f64], vec2: &[f64]) -> f64 {
        if vec1.len() != vec2.len() {
            return 0.0;
        }

        let dot_product: f64 = vec1.iter().zip(vec2.iter()).map(|(a, b)| a * b).sum();
        let norm1: f64 = vec1.iter().map(|x| x * x).sum::<f64>().sqrt();
        let norm2: f64 = vec2.iter().map(|x| x * x).sum::<f64>().sqrt();

        if norm1 == 0.0 || norm2 == 0.0 {
            0.0
        } else {
            dot_product / (norm1 * norm2)
        }
    }

    /// Greedy alignment algorithm (fastest)
    fn greedy_alignment(&self, similarity_matrix: &[Vec<f64>]) -> (f64, f64) {
        if similarity_matrix.is_empty() || similarity_matrix[0].is_empty() {
            return (0.0, 0.0);
        }

        let num_ref = similarity_matrix.len();
        let num_cand = similarity_matrix[0].len();

        // For precision: for each candidate, find best reference match
        let mut precision_sum = 0.0;
        for j in 0..num_cand {
            let mut max_sim = 0.0;
            for i in 0..num_ref {
                max_sim = max_sim.max(similarity_matrix[i][j]);
            }
            precision_sum += max_sim;
        }
        let precision = precision_sum / num_cand as f64;

        // For recall: for each reference, find best candidate match
        let mut recall_sum = 0.0;
        for i in 0..num_ref {
            let mut max_sim = 0.0;
            for j in 0..num_cand {
                max_sim = max_sim.max(similarity_matrix[i][j]);
            }
            recall_sum += max_sim;
        }
        let recall = recall_sum / num_ref as f64;

        (precision, recall)
    }

    /// Optimal alignment using simplified Hungarian algorithm
    fn optimal_alignment(&self, similarity_matrix: &[Vec<f64>]) -> Result<(f64, f64)> {
        // For now, use greedy as optimal is complex to implement
        // In a full implementation, this would use the Hungarian algorithm
        Ok(self.greedy_alignment(similarity_matrix))
    }

    /// Hierarchical alignment with chunking
    fn hierarchical_alignment(&self, similarity_matrix: &[Vec<f64>]) -> Result<(f64, f64)> {
        // Simplified hierarchical approach
        let chunk_size = 5;
        let num_ref = similarity_matrix.len();
        let num_cand = if similarity_matrix.is_empty() {
            0
        } else {
            similarity_matrix[0].len()
        };

        if num_ref <= chunk_size || num_cand <= chunk_size {
            return Ok(self.greedy_alignment(similarity_matrix));
        }

        let mut total_precision = 0.0;
        let mut total_recall = 0.0;
        let mut chunks_processed = 0;

        // Process in chunks
        for ref_start in (0..num_ref).step_by(chunk_size) {
            for cand_start in (0..num_cand).step_by(chunk_size) {
                let ref_end = (ref_start + chunk_size).min(num_ref);
                let cand_end = (cand_start + chunk_size).min(num_cand);

                // Extract chunk
                let chunk: Vec<Vec<f64>> = similarity_matrix[ref_start..ref_end]
                    .iter()
                    .map(|row| row[cand_start..cand_end].to_vec())
                    .collect();

                let (chunk_precision, chunk_recall) = self.greedy_alignment(&chunk);
                total_precision += chunk_precision;
                total_recall += chunk_recall;
                chunks_processed += 1;
            }
        }

        let avg_precision = if chunks_processed > 0 {
            total_precision / chunks_processed as f64
        } else {
            0.0
        };
        let avg_recall = if chunks_processed > 0 {
            total_recall / chunks_processed as f64
        } else {
            0.0
        };

        Ok((avg_precision, avg_recall))
    }

    /// Attention-based alignment
    fn attention_based_alignment(&self, similarity_matrix: &[Vec<f64>]) -> Result<(f64, f64)> {
        // Simplified attention mechanism
        let num_ref = similarity_matrix.len();
        let num_cand = if similarity_matrix.is_empty() {
            0
        } else {
            similarity_matrix[0].len()
        };

        if num_ref == 0 || num_cand == 0 {
            return Ok((0.0, 0.0));
        }

        // Compute attention weights
        let mut precision_sum = 0.0;
        let mut recall_sum = 0.0;

        // Attention from candidates to references (for precision)
        for j in 0..num_cand {
            let mut attention_weights = Vec::new();
            let mut total_weight = 0.0;

            for i in 0..num_ref {
                let weight = similarity_matrix[i][j].exp(); // Softmax-like
                attention_weights.push(weight);
                total_weight += weight;
            }

            if total_weight > 0.0 {
                let mut weighted_sim = 0.0;
                for (i, &weight) in attention_weights.iter().enumerate() {
                    weighted_sim += (weight / total_weight) * similarity_matrix[i][j];
                }
                precision_sum += weighted_sim;
            }
        }

        // Attention from references to candidates (for recall)
        for i in 0..num_ref {
            let mut attention_weights = Vec::new();
            let mut total_weight = 0.0;

            for j in 0..num_cand {
                let weight = similarity_matrix[i][j].exp();
                attention_weights.push(weight);
                total_weight += weight;
            }

            if total_weight > 0.0 {
                let mut weighted_sim = 0.0;
                for (j, &weight) in attention_weights.iter().enumerate() {
                    weighted_sim += (weight / total_weight) * similarity_matrix[i][j];
                }
                recall_sum += weighted_sim;
            }
        }

        let precision = precision_sum / num_cand as f64;
        let recall = recall_sum / num_ref as f64;

        Ok((precision, recall))
    }

    /// Compute detailed alignment information
    fn compute_alignment_details(
        &self,
        reference: &str,
        candidate: &str,
    ) -> Result<AlignmentDetails> {
        let ref_tokens = self.tokenize(reference)?;
        let cand_tokens = self.tokenize(candidate)?;

        let ref_embeddings = self.get_embeddings(&ref_tokens)?;
        let cand_embeddings = self.get_embeddings(&cand_tokens)?;

        let similarity_matrix =
            self.compute_similarity_matrix(&ref_embeddings, &cand_embeddings)?;

        // Find optimal alignments for visualization
        let mut alignments = Vec::new();
        for i in 0..ref_tokens.len() {
            let mut best_j = 0;
            let mut best_sim = 0.0;
            for j in 0..cand_tokens.len() {
                if similarity_matrix[i][j] > best_sim {
                    best_sim = similarity_matrix[i][j];
                    best_j = j;
                }
            }
            if best_sim > 0.1 {
                // Only include meaningful alignments
                alignments.push((i, best_j, best_sim));
            }
        }

        Ok(AlignmentDetails {
            reference_tokens: ref_tokens,
            candidate_tokens: cand_tokens,
            token_similarities: similarity_matrix,
            alignments,
        })
    }

    // Statistical helper functions

    /// Calculate mean of a vector
    fn mean(values: &[f64]) -> f64 {
        if values.is_empty() {
            0.0
        } else {
            values.iter().sum::<f64>() / values.len() as f64
        }
    }

    /// Calculate standard deviation
    fn std_dev(values: &[f64]) -> f64 {
        if values.len() < 2 {
            0.0
        } else {
            let mean = Self::mean(values);
            let variance =
                values.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / (values.len() - 1) as f64;
            variance.sqrt()
        }
    }

    /// Calculate median
    fn median(values: &mut [f64]) -> f64 {
        if values.is_empty() {
            return 0.0;
        }

        values.sort_by(|a, b| {
            a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)
        });
        let mid = values.len() / 2;

        if values.len() % 2 == 0 {
            (values[mid - 1] + values[mid]) / 2.0
        } else {
            values[mid]
        }
    }
}

/// Corpus-level statistics
#[derive(Debug, Clone)]
pub struct CorpusStatistics {
    pub mean_precision: f64,
    pub mean_recall: f64,
    pub mean_f1: f64,
    pub std_precision: f64,
    pub std_recall: f64,
    pub std_f1: f64,
    pub median_precision: f64,
    pub median_recall: f64,
    pub median_f1: f64,
    pub num_pairs: usize,
}

/// Model comparison results
#[derive(Debug, Clone)]
pub struct ModelComparison {
    pub model1_config: BertScoreConfig,
    pub model2_config: BertScoreConfig,
    pub model1_stats: CorpusStatistics,
    pub model2_stats: CorpusStatistics,
    pub f1_improvement: f64,
    pub precision_improvement: f64,
    pub recall_improvement: f64,
    pub model1_wins: usize,
    pub model2_wins: usize,
    pub ties: usize,
}

// Trait extensions for character classification
trait CharExt {
    fn is_vowel_like(&self) -> bool;
    fn is_consonant_like(&self) -> bool;
}

impl CharExt for char {
    fn is_vowel_like(&self) -> bool {
        matches!(
            self.to_lowercase().next(),
            Some('a' | 'e' | 'i' | 'o' | 'u' | 'y')
        )
    }

    fn is_consonant_like(&self) -> bool {
        self.is_alphabetic() && !self.is_vowel_like()
    }
}

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

    #[test]
    fn test_basic_bert_score() {
        let scorer = BertScore::new();
        let references = &["The cat sat on the mat"];
        let candidates = &["A cat was sitting on the mat"];

        let results = scorer.compute(references, candidates).expect("computation should succeed");
        assert_eq!(results.len(), 1);

        let result = &results[0];
        assert!(result.precision > 0.0 && result.precision <= 1.0);
        assert!(result.recall > 0.0 && result.recall <= 1.0);
        assert!(result.f1_score > 0.0 && result.f1_score <= 1.0);
    }

    #[test]
    fn test_perfect_match() {
        let scorer = BertScore::new();
        let references = &["hello world"];
        let candidates = &["hello world"];

        let results = scorer.compute(references, candidates).expect("computation should succeed");
        let result = &results[0];

        // Should be very high similarity for identical texts
        assert!(result.f1_score > 0.9);
    }

    #[test]
    fn test_empty_inputs() {
        let scorer = BertScore::new();
        let references = &[""];
        let candidates = &["hello"];

        let results = scorer.compute(references, candidates).expect("computation should succeed");
        let result = &results[0];

        assert_eq!(result.precision, 0.0);
        assert_eq!(result.recall, 0.0);
        assert_eq!(result.f1_score, 0.0);
    }

    #[test]
    fn test_different_configurations() {
        let config1 =
            BertScoreConfig::new().with_embedding_strategy(EmbeddingStrategy::CharacterBased);
        let config2 = BertScoreConfig::new().with_embedding_strategy(EmbeddingStrategy::WordLevel);

        let scorer1 = BertScore::with_config(config1);
        let scorer2 = BertScore::with_config(config2);

        let references = &["The quick brown fox"];
        let candidates = &["A fast brown fox"];

        let results1 = scorer1.compute(references, candidates).expect("computation should succeed");
        let results2 = scorer2.compute(references, candidates).expect("computation should succeed");

        // Results should be different due to different embedding strategies
        assert_ne!(results1[0].f1_score, results2[0].f1_score);
    }

    #[test]
    fn test_alignment_algorithms() {
        let config_greedy =
            BertScoreConfig::new().with_alignment_algorithm(AlignmentAlgorithm::Greedy);
        let config_attention =
            BertScoreConfig::new().with_alignment_algorithm(AlignmentAlgorithm::AttentionBased);

        let scorer_greedy = BertScore::with_config(config_greedy);
        let scorer_attention = BertScore::with_config(config_attention);

        let references = &["The cat sat"];
        let candidates = &["Cat was sitting"];

        let results_greedy = scorer_greedy.compute(references, candidates).expect("computation should succeed");
        let results_attention = scorer_attention.compute(references, candidates).expect("computation should succeed");

        // Both should produce reasonable scores
        assert!(results_greedy[0].f1_score > 0.0);
        assert!(results_attention[0].f1_score > 0.0);
    }

    #[test]
    fn test_detailed_computation() {
        let scorer = BertScore::new();
        let result = scorer.compute_detailed("hello world", "hi world").expect("detailed computation should succeed");

        assert!(result.precision > 0.0);
        assert!(result.recall > 0.0);
        assert!(result.alignment_details.is_some());

        let details = result.alignment_details.expect("operation should succeed");
        assert!(!details.reference_tokens.is_empty());
        assert!(!details.candidate_tokens.is_empty());
        assert!(!details.token_similarities.is_empty());
    }

    #[test]
    fn test_corpus_statistics() {
        let scorer = BertScore::new();
        let references = &["hello world", "the cat sat", "quick brown fox"];
        let candidates = &["hi world", "a cat sits", "fast brown fox"];

        let stats = scorer
            .compute_corpus_statistics(references, candidates)
            .expect("operation should succeed");

        assert_eq!(stats.num_pairs, 3);
        assert!(stats.mean_f1 > 0.0);
        assert!(stats.std_f1 >= 0.0);
        assert!(stats.median_f1 > 0.0);
    }

    #[test]
    fn test_model_comparison() {
        let scorer1 = BertScore::new();
        let config2 = BertScoreConfig::new().with_embedding_strategy(EmbeddingStrategy::Subword);
        let scorer2 = BertScore::with_config(config2);

        let references = &["hello world", "the cat"];
        let candidates = &["hi world", "a cat"];

        let comparison = scorer1
            .compare_models(&scorer2, references, candidates)
            .expect("operation should succeed");

        assert_eq!(
            comparison.model1_wins + comparison.model2_wins + comparison.ties,
            2
        );
        assert!(comparison.f1_improvement.abs() >= 0.0); // Should be a valid number
    }

    #[test]
    fn test_tokenization_strategies() {
        let scorer = BertScore::new();

        // Test different tokenization
        let char_tokens = scorer.tokenize("hello").expect("tokenization should succeed");
        assert_eq!(char_tokens.len(), 1); // Word-level by default

        let config =
            BertScoreConfig::new().with_embedding_strategy(EmbeddingStrategy::CharacterBased);
        let char_scorer = BertScore::with_config(config);
        let char_tokens = char_scorer.tokenize("hello").expect("tokenization should succeed");
        assert_eq!(char_tokens.len(), 5); // Character-level
    }

    #[test]
    fn test_linguistic_features() {
        let scorer = BertScore::new();
        let features = scorer.extract_linguistic_features("running");

        assert_eq!(features.len(), 16);
        assert!(features[0] > 0.0); // Length feature
        assert_eq!(features[13], 1.0); // Should detect "ing" suffix
    }

    #[test]
    fn test_normalization() {
        let config = BertScoreConfig::new().with_normalize_embeddings(true);
        let scorer = BertScore::with_config(config);

        let embedding = scorer.word_level_embedding("test");
        let norm: f64 = embedding.iter().map(|x| x * x).sum::<f64>().sqrt();
        assert!((norm - 1.0).abs() < 1e-6); // Should be normalized
    }

    #[test]
    fn test_case_sensitivity() {
        let config = BertScoreConfig::new().with_ignore_case(true);
        let scorer = BertScore::with_config(config);

        let references = &["Hello World"];
        let candidates = &["hello world"];

        let results = scorer.compute(references, candidates).expect("computation should succeed");
        assert!(results[0].f1_score > 0.95); // Should be very high with case-insensitive
    }

    #[test]
    fn test_similarity_matrix() {
        let scorer = BertScore::new();
        let ref_embeddings = vec![vec![1.0, 0.0], vec![0.0, 1.0]];
        let cand_embeddings = vec![vec![1.0, 0.0], vec![0.5, 0.5]];

        let matrix = scorer
            .compute_similarity_matrix(&ref_embeddings, &cand_embeddings)
            .expect("operation should succeed");

        assert_eq!(matrix.len(), 2);
        assert_eq!(matrix[0].len(), 2);
        assert!((matrix[0][0] - 1.0).abs() < 1e-6); // Perfect match
        assert!(matrix[0][1] > 0.0 && matrix[0][1] < 1.0); // Partial match
    }

    #[test]
    fn test_error_handling() {
        let scorer = BertScore::new();

        // Mismatched lengths should return error
        let result = scorer.compute(&["ref1", "ref2"], &["cand1"]);
        assert!(result.is_err());

        // Empty inputs should return error
        let result = scorer.compute(&[], &[]);
        assert!(result.is_err());
    }
}