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
//! Edit Distance Metrics for String Similarity
//!
//! This module provides comprehensive edit distance algorithms for measuring string similarity.
//! Edit distance quantifies how different two strings are by counting the minimum number of
//! single-character edits (insertions, deletions, or substitutions) needed to transform
//! one string into another.
//!
//! # Supported Algorithms
//!
//! - **Levenshtein Distance**: Standard edit distance with equal costs for all operations
//! - **Damerau-Levenshtein Distance**: Includes transposition as a fourth operation
//! - **Hamming Distance**: For strings of equal length, counts differing positions
//! - **Jaro Distance**: Focuses on character matches and transpositions
//! - **Jaro-Winkler Distance**: Jaro distance with prefix bonus
//! - **Optimal String Alignment**: Restricted Damerau-Levenshtein
//!
//! # Features
//!
//! - Multiple distance algorithms with different characteristics
//! - Normalized similarity scores (0.0 to 1.0)
//! - Configurable operation costs (insertion, deletion, substitution, transposition)
//! - Case-insensitive comparison options
//! - Unicode-aware character handling
//! - Efficient algorithms with space optimization
//!
//! # Example
//!
//! ```rust
//! use torsh_text::metrics::edit_distance::{EditDistance, EditDistanceConfig, DistanceAlgorithm};
//!
//! let calculator = EditDistance::new();
//!
//! // Basic Levenshtein distance
//! let distance = calculator.levenshtein("kitten", "sitting");
//! println!("Levenshtein distance: {}", distance);
//!
//! // Normalized similarity score
//! let similarity = calculator.normalized_levenshtein("kitten", "sitting");
//! println!("Similarity: {:.3}", similarity);
//!
//! // Custom configuration with different costs
//! let config = EditDistanceConfig::new()
//!     .with_insertion_cost(1.0)
//!     .with_deletion_cost(1.0)
//!     .with_substitution_cost(2.0); // Higher substitution cost
//!
//! let custom_calculator = EditDistance::with_config(config);
//! let weighted_distance = custom_calculator.calculate("hello", "world", DistanceAlgorithm::Levenshtein);
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```

use crate::{Result, TextError};
use std::cmp;
use std::collections::HashMap;

/// Available edit distance algorithms
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DistanceAlgorithm {
    /// Standard Levenshtein distance
    Levenshtein,
    /// Damerau-Levenshtein distance (allows transpositions)
    DamerauLevenshtein,
    /// Optimal String Alignment distance
    OptimalStringAlignment,
    /// Hamming distance (equal length strings only)
    Hamming,
    /// Jaro distance
    Jaro,
    /// Jaro-Winkler distance
    JaroWinkler,
}

/// Configuration for edit distance calculations
#[derive(Debug, Clone)]
pub struct EditDistanceConfig {
    /// Cost of inserting a character
    pub insertion_cost: f64,
    /// Cost of deleting a character
    pub deletion_cost: f64,
    /// Cost of substituting a character
    pub substitution_cost: f64,
    /// Cost of transposing two adjacent characters
    pub transposition_cost: f64,
    /// Whether to ignore case when comparing characters
    pub ignore_case: bool,
    /// Jaro-Winkler prefix scaling factor (0.0 to 0.25)
    pub jaro_winkler_prefix_scale: f64,
    /// Maximum prefix length to consider for Jaro-Winkler
    pub jaro_winkler_max_prefix: usize,
}

impl Default for EditDistanceConfig {
    fn default() -> Self {
        Self {
            insertion_cost: 1.0,
            deletion_cost: 1.0,
            substitution_cost: 1.0,
            transposition_cost: 1.0,
            ignore_case: false,
            jaro_winkler_prefix_scale: 0.1,
            jaro_winkler_max_prefix: 4,
        }
    }
}

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

    /// Set the cost for character insertion
    pub fn with_insertion_cost(mut self, cost: f64) -> Self {
        self.insertion_cost = cost.max(0.0);
        self
    }

    /// Set the cost for character deletion
    pub fn with_deletion_cost(mut self, cost: f64) -> Self {
        self.deletion_cost = cost.max(0.0);
        self
    }

    /// Set the cost for character substitution
    pub fn with_substitution_cost(mut self, cost: f64) -> Self {
        self.substitution_cost = cost.max(0.0);
        self
    }

    /// Set the cost for character transposition
    pub fn with_transposition_cost(mut self, cost: f64) -> Self {
        self.transposition_cost = cost.max(0.0);
        self
    }

    /// Enable or disable case-insensitive comparison
    pub fn with_ignore_case(mut self, ignore_case: bool) -> Self {
        self.ignore_case = ignore_case;
        self
    }

    /// Set Jaro-Winkler prefix scaling factor
    pub fn with_jaro_winkler_prefix_scale(mut self, scale: f64) -> Self {
        self.jaro_winkler_prefix_scale = scale.clamp(0.0, 0.25);
        self
    }

    /// Set maximum prefix length for Jaro-Winkler
    pub fn with_jaro_winkler_max_prefix(mut self, max_prefix: usize) -> Self {
        self.jaro_winkler_max_prefix = max_prefix;
        self
    }
}

/// Comprehensive edit distance calculator
#[derive(Debug, Clone)]
pub struct EditDistance {
    config: EditDistanceConfig,
}

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

impl EditDistance {
    /// Create a new edit distance calculator with default configuration
    pub fn new() -> Self {
        Self {
            config: EditDistanceConfig::default(),
        }
    }

    /// Create an edit distance calculator with custom configuration
    pub fn with_config(config: EditDistanceConfig) -> Self {
        Self { config }
    }

    /// Calculate edit distance using the specified algorithm
    pub fn calculate(&self, s1: &str, s2: &str, algorithm: DistanceAlgorithm) -> Result<f64> {
        match algorithm {
            DistanceAlgorithm::Levenshtein => Ok(self.levenshtein_distance(s1, s2) as f64),
            DistanceAlgorithm::DamerauLevenshtein => {
                Ok(self.damerau_levenshtein_distance(s1, s2) as f64)
            }
            DistanceAlgorithm::OptimalStringAlignment => {
                Ok(self.optimal_string_alignment_distance(s1, s2) as f64)
            }
            DistanceAlgorithm::Hamming => self.hamming_distance(s1, s2).map(|d| d as f64),
            DistanceAlgorithm::Jaro => Ok(1.0 - self.jaro_similarity(s1, s2)),
            DistanceAlgorithm::JaroWinkler => Ok(1.0 - self.jaro_winkler_similarity(s1, s2)),
        }
    }

    /// Calculate normalized similarity score (0.0 to 1.0) using the specified algorithm
    pub fn similarity(&self, s1: &str, s2: &str, algorithm: DistanceAlgorithm) -> Result<f64> {
        match algorithm {
            DistanceAlgorithm::Levenshtein => Ok(self.normalized_levenshtein(s1, s2)),
            DistanceAlgorithm::DamerauLevenshtein => {
                Ok(self.normalized_damerau_levenshtein(s1, s2))
            }
            DistanceAlgorithm::OptimalStringAlignment => {
                Ok(self.normalized_optimal_string_alignment(s1, s2))
            }
            DistanceAlgorithm::Hamming => self.normalized_hamming(s1, s2),
            DistanceAlgorithm::Jaro => Ok(self.jaro_similarity(s1, s2)),
            DistanceAlgorithm::JaroWinkler => Ok(self.jaro_winkler_similarity(s1, s2)),
        }
    }

    /// Calculate standard Levenshtein distance
    pub fn levenshtein(&self, s1: &str, s2: &str) -> usize {
        self.levenshtein_distance(s1, s2)
    }

    /// Calculate normalized Levenshtein similarity (0.0 to 1.0)
    pub fn normalized_levenshtein(&self, s1: &str, s2: &str) -> f64 {
        let distance = self.levenshtein_distance(s1, s2);
        let chars1 = if self.config.ignore_case {
            s1.to_lowercase().chars().count()
        } else {
            s1.chars().count()
        };
        let chars2 = if self.config.ignore_case {
            s2.to_lowercase().chars().count()
        } else {
            s2.chars().count()
        };
        let max_len = chars1.max(chars2);

        if max_len == 0 {
            1.0
        } else {
            1.0 - (distance as f64 / max_len as f64)
        }
    }

    /// Calculate Damerau-Levenshtein distance (allows transpositions)
    pub fn damerau_levenshtein(&self, s1: &str, s2: &str) -> usize {
        self.damerau_levenshtein_distance(s1, s2)
    }

    /// Calculate normalized Damerau-Levenshtein similarity
    pub fn normalized_damerau_levenshtein(&self, s1: &str, s2: &str) -> f64 {
        let distance = self.damerau_levenshtein_distance(s1, s2);
        let chars1 = if self.config.ignore_case {
            s1.to_lowercase().chars().count()
        } else {
            s1.chars().count()
        };
        let chars2 = if self.config.ignore_case {
            s2.to_lowercase().chars().count()
        } else {
            s2.chars().count()
        };
        let max_len = chars1.max(chars2);

        if max_len == 0 {
            1.0
        } else {
            1.0 - (distance as f64 / max_len as f64)
        }
    }

    /// Calculate Hamming distance (strings must be of equal length)
    pub fn hamming(&self, s1: &str, s2: &str) -> Result<usize> {
        self.hamming_distance(s1, s2)
    }

    /// Calculate normalized Hamming similarity
    pub fn normalized_hamming(&self, s1: &str, s2: &str) -> Result<f64> {
        let distance = self.hamming_distance(s1, s2)?;
        let len = if self.config.ignore_case {
            s1.to_lowercase().chars().count()
        } else {
            s1.chars().count()
        };

        if len == 0 {
            Ok(1.0)
        } else {
            Ok(1.0 - (distance as f64 / len as f64))
        }
    }

    /// Calculate Jaro similarity
    pub fn jaro(&self, s1: &str, s2: &str) -> f64 {
        self.jaro_similarity(s1, s2)
    }

    /// Calculate Jaro-Winkler similarity
    pub fn jaro_winkler(&self, s1: &str, s2: &str) -> f64 {
        self.jaro_winkler_similarity(s1, s2)
    }

    /// Calculate Optimal String Alignment distance
    pub fn optimal_string_alignment(&self, s1: &str, s2: &str) -> usize {
        self.optimal_string_alignment_distance(s1, s2)
    }

    /// Calculate normalized Optimal String Alignment similarity
    pub fn normalized_optimal_string_alignment(&self, s1: &str, s2: &str) -> f64 {
        let distance = self.optimal_string_alignment_distance(s1, s2);
        let chars1 = if self.config.ignore_case {
            s1.to_lowercase().chars().count()
        } else {
            s1.chars().count()
        };
        let chars2 = if self.config.ignore_case {
            s2.to_lowercase().chars().count()
        } else {
            s2.chars().count()
        };
        let max_len = chars1.max(chars2);

        if max_len == 0 {
            1.0
        } else {
            1.0 - (distance as f64 / max_len as f64)
        }
    }

    /// Compare multiple strings and find the most similar one
    pub fn find_most_similar(
        &self,
        target: &str,
        candidates: &[&str],
        algorithm: DistanceAlgorithm,
    ) -> Result<Option<SimilarityMatch>> {
        if candidates.is_empty() {
            return Ok(None);
        }

        let mut best_match = None;
        let mut best_similarity = -1.0;

        for (index, &candidate) in candidates.iter().enumerate() {
            let similarity = self.similarity(target, candidate, algorithm)?;

            if similarity > best_similarity {
                best_similarity = similarity;
                best_match = Some(SimilarityMatch {
                    index,
                    text: candidate.to_string(),
                    similarity,
                    distance: self.calculate(target, candidate, algorithm)?,
                });
            }
        }

        Ok(best_match)
    }

    /// Calculate pairwise distances between all strings in a collection
    pub fn pairwise_distances(
        &self,
        strings: &[&str],
        algorithm: DistanceAlgorithm,
    ) -> Result<Vec<Vec<f64>>> {
        let n = strings.len();
        let mut distances = vec![vec![0.0; n]; n];

        for i in 0..n {
            for j in i + 1..n {
                let distance = self.calculate(strings[i], strings[j], algorithm)?;
                distances[i][j] = distance;
                distances[j][i] = distance; // Symmetric
            }
        }

        Ok(distances)
    }

    // Private implementation methods

    /// Preprocess strings based on configuration
    fn preprocess(&self, s: &str) -> String {
        if self.config.ignore_case {
            s.to_lowercase()
        } else {
            s.to_string()
        }
    }

    /// Implementation of Levenshtein distance algorithm
    fn levenshtein_distance(&self, s1: &str, s2: &str) -> usize {
        let s1 = self.preprocess(s1);
        let s2 = self.preprocess(s2);

        let chars1: Vec<char> = s1.chars().collect();
        let chars2: Vec<char> = s2.chars().collect();

        let len1 = chars1.len();
        let len2 = chars2.len();

        if len1 == 0 {
            return len2;
        }
        if len2 == 0 {
            return len1;
        }

        let mut dp = vec![vec![0; len2 + 1]; len1 + 1];

        // Initialize base cases
        for i in 0..=len1 {
            dp[i][0] = (i as f64 * self.config.deletion_cost) as usize;
        }
        for j in 0..=len2 {
            dp[0][j] = (j as f64 * self.config.insertion_cost) as usize;
        }

        // Fill the matrix
        for i in 1..=len1 {
            for j in 1..=len2 {
                let cost = if chars1[i - 1] == chars2[j - 1] {
                    0.0
                } else {
                    self.config.substitution_cost
                };

                dp[i][j] = cmp::min(
                    cmp::min(
                        dp[i - 1][j] + self.config.deletion_cost as usize,
                        dp[i][j - 1] + self.config.insertion_cost as usize,
                    ),
                    dp[i - 1][j - 1] + cost as usize,
                );
            }
        }

        dp[len1][len2]
    }

    /// Implementation of Damerau-Levenshtein distance algorithm
    fn damerau_levenshtein_distance(&self, s1: &str, s2: &str) -> usize {
        let s1 = self.preprocess(s1);
        let s2 = self.preprocess(s2);

        let chars1: Vec<char> = s1.chars().collect();
        let chars2: Vec<char> = s2.chars().collect();

        let len1 = chars1.len();
        let len2 = chars2.len();

        if len1 == 0 {
            return len2;
        }
        if len2 == 0 {
            return len1;
        }

        // Create character alphabet
        let mut alphabet = HashMap::new();
        let mut char_index = 0;
        for &c in chars1.iter().chain(chars2.iter()) {
            if !alphabet.contains_key(&c) {
                alphabet.insert(c, char_index);
                char_index += 1;
            }
        }

        let max_distance = len1 + len2;
        let mut h = vec![vec![max_distance; len2 + 2]; len1 + 2];

        // Initialize
        h[0][0] = max_distance;
        for i in 0..=len1 {
            h[i + 1][0] = max_distance;
            h[i + 1][1] = i;
        }
        for j in 0..=len2 {
            h[0][j + 1] = max_distance;
            h[1][j + 1] = j;
        }

        let mut last_row = vec![0; alphabet.len()];

        for i in 1..=len1 {
            let mut last_match_col = 0;

            for j in 1..=len2 {
                let i1 = last_row[alphabet[&chars2[j - 1]]];
                let j1 = last_match_col;

                let cost = if chars1[i - 1] == chars2[j - 1] {
                    last_match_col = j;
                    0
                } else {
                    1
                };

                h[i + 1][j + 1] = cmp::min(
                    cmp::min(
                        h[i][j] + cost,  // substitution
                        h[i + 1][j] + 1, // insertion
                    ),
                    cmp::min(
                        h[i][j + 1] + 1,                             // deletion
                        h[i1][j1] + (i - i1 - 1) + 1 + (j - j1 - 1), // transposition
                    ),
                );
            }

            last_row[alphabet[&chars1[i - 1]]] = i;
        }

        h[len1 + 1][len2 + 1]
    }

    /// Implementation of Optimal String Alignment distance
    fn optimal_string_alignment_distance(&self, s1: &str, s2: &str) -> usize {
        let s1 = self.preprocess(s1);
        let s2 = self.preprocess(s2);

        let chars1: Vec<char> = s1.chars().collect();
        let chars2: Vec<char> = s2.chars().collect();

        let len1 = chars1.len();
        let len2 = chars2.len();

        if len1 == 0 {
            return len2;
        }
        if len2 == 0 {
            return len1;
        }

        let mut dp = vec![vec![0; len2 + 1]; len1 + 1];

        // Initialize
        for i in 0..=len1 {
            dp[i][0] = i;
        }
        for j in 0..=len2 {
            dp[0][j] = j;
        }

        // Fill the matrix
        for i in 1..=len1 {
            for j in 1..=len2 {
                let cost = if chars1[i - 1] == chars2[j - 1] { 0 } else { 1 };

                dp[i][j] = cmp::min(
                    cmp::min(
                        dp[i - 1][j] + 1, // deletion
                        dp[i][j - 1] + 1, // insertion
                    ),
                    dp[i - 1][j - 1] + cost, // substitution
                );

                // Check for transposition
                if i > 1
                    && j > 1
                    && chars1[i - 1] == chars2[j - 2]
                    && chars1[i - 2] == chars2[j - 1]
                {
                    dp[i][j] = cmp::min(dp[i][j], dp[i - 2][j - 2] + cost);
                }
            }
        }

        dp[len1][len2]
    }

    /// Implementation of Hamming distance algorithm
    fn hamming_distance(&self, s1: &str, s2: &str) -> Result<usize> {
        let s1 = self.preprocess(s1);
        let s2 = self.preprocess(s2);

        let chars1: Vec<char> = s1.chars().collect();
        let chars2: Vec<char> = s2.chars().collect();

        if chars1.len() != chars2.len() {
            return Err(TextError::Other(anyhow::anyhow!(
                "Hamming distance requires strings of equal length: {} vs {}",
                chars1.len(),
                chars2.len()
            )));
        }

        let mut distance = 0;
        for (c1, c2) in chars1.iter().zip(chars2.iter()) {
            if c1 != c2 {
                distance += 1;
            }
        }

        Ok(distance)
    }

    /// Implementation of Jaro similarity algorithm
    fn jaro_similarity(&self, s1: &str, s2: &str) -> f64 {
        let s1 = self.preprocess(s1);
        let s2 = self.preprocess(s2);

        let chars1: Vec<char> = s1.chars().collect();
        let chars2: Vec<char> = s2.chars().collect();

        let len1 = chars1.len();
        let len2 = chars2.len();

        if len1 == 0 && len2 == 0 {
            return 1.0;
        }
        if len1 == 0 || len2 == 0 {
            return 0.0;
        }

        let match_window = (cmp::max(len1, len2) / 2).saturating_sub(1);

        let mut s1_matches = vec![false; len1];
        let mut s2_matches = vec![false; len2];

        let mut matches = 0;

        // Identify matches
        for i in 0..len1 {
            let start = if i >= match_window {
                i - match_window
            } else {
                0
            };
            let end = cmp::min(i + match_window + 1, len2);

            for j in start..end {
                if s2_matches[j] || chars1[i] != chars2[j] {
                    continue;
                }

                s1_matches[i] = true;
                s2_matches[j] = true;
                matches += 1;
                break;
            }
        }

        if matches == 0 {
            return 0.0;
        }

        // Count transpositions
        let mut transpositions = 0;
        let mut k = 0;

        for i in 0..len1 {
            if !s1_matches[i] {
                continue;
            }

            while !s2_matches[k] {
                k += 1;
            }

            if chars1[i] != chars2[k] {
                transpositions += 1;
            }

            k += 1;
        }

        let jaro = (matches as f64 / len1 as f64
            + matches as f64 / len2 as f64
            + (matches as f64 - transpositions as f64 / 2.0) / matches as f64)
            / 3.0;

        jaro
    }

    /// Implementation of Jaro-Winkler similarity algorithm
    fn jaro_winkler_similarity(&self, s1: &str, s2: &str) -> f64 {
        let jaro = self.jaro_similarity(s1, s2);

        if jaro < 0.7 {
            return jaro;
        }

        let s1 = self.preprocess(s1);
        let s2 = self.preprocess(s2);

        let chars1: Vec<char> = s1.chars().collect();
        let chars2: Vec<char> = s2.chars().collect();

        // Calculate common prefix up to maximum of 4 characters
        let mut prefix_length = 0;
        let max_prefix = cmp::min(
            self.config.jaro_winkler_max_prefix,
            cmp::min(chars1.len(), chars2.len()),
        );

        for i in 0..max_prefix {
            if chars1[i] == chars2[i] {
                prefix_length += 1;
            } else {
                break;
            }
        }

        jaro + (self.config.jaro_winkler_prefix_scale * prefix_length as f64 * (1.0 - jaro))
    }
}

/// Result of a similarity search
#[derive(Debug, Clone)]
pub struct SimilarityMatch {
    /// Index of the matched string in the original collection
    pub index: usize,
    /// The matched text
    pub text: String,
    /// Similarity score (0.0 to 1.0)
    pub similarity: f64,
    /// Edit distance value
    pub distance: f64,
}

impl SimilarityMatch {
    /// Check if the match quality is high (similarity > 0.8)
    pub fn is_high_quality(&self) -> bool {
        self.similarity > 0.8
    }

    /// Check if the match quality is acceptable (similarity > 0.5)
    pub fn is_acceptable(&self) -> bool {
        self.similarity > 0.5
    }

    /// Get match quality category
    pub fn quality_category(&self) -> &'static str {
        if self.similarity > 0.9 {
            "Excellent"
        } else if self.similarity > 0.8 {
            "Good"
        } else if self.similarity > 0.6 {
            "Fair"
        } else if self.similarity > 0.4 {
            "Poor"
        } else {
            "Very Poor"
        }
    }
}

// Legacy compatibility functions
impl EditDistance {
    /// Legacy function for basic Levenshtein distance
    pub fn levenshtein_static(s1: &str, s2: &str) -> usize {
        EditDistance::new().levenshtein(s1, s2)
    }

    /// Legacy function for normalized Levenshtein similarity
    pub fn normalized_levenshtein_static(s1: &str, s2: &str) -> f64 {
        EditDistance::new().normalized_levenshtein(s1, s2)
    }
}

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

    #[test]
    fn test_levenshtein_basic() {
        let edit_distance = EditDistance::new();

        assert_eq!(edit_distance.levenshtein("", ""), 0);
        assert_eq!(edit_distance.levenshtein("", "abc"), 3);
        assert_eq!(edit_distance.levenshtein("abc", ""), 3);
        assert_eq!(edit_distance.levenshtein("abc", "abc"), 0);
        assert_eq!(edit_distance.levenshtein("kitten", "sitting"), 3);
        assert_eq!(edit_distance.levenshtein("saturday", "sunday"), 3);
    }

    #[test]
    fn test_normalized_levenshtein() {
        let edit_distance = EditDistance::new();

        assert!((edit_distance.normalized_levenshtein("", "") - 1.0).abs() < 1e-10);
        assert!((edit_distance.normalized_levenshtein("abc", "abc") - 1.0).abs() < 1e-10);
        assert!(edit_distance.normalized_levenshtein("", "abc") < 0.1);

        let similarity = edit_distance.normalized_levenshtein("kitten", "sitting");
        assert!(similarity > 0.0 && similarity < 1.0);
    }

    #[test]
    fn test_hamming_distance() {
        let edit_distance = EditDistance::new();

        assert_eq!(edit_distance.hamming("abc", "abc").expect("Hamming distance computation should succeed"), 0);
        assert_eq!(edit_distance.hamming("abc", "axc").expect("Hamming distance computation should succeed"), 1);
        assert_eq!(edit_distance.hamming("abc", "xyz").expect("Hamming distance computation should succeed"), 3);

        // Different lengths should return error
        assert!(edit_distance.hamming("ab", "abc").is_err());
    }

    #[test]
    fn test_jaro_similarity() {
        let edit_distance = EditDistance::new();

        assert!((edit_distance.jaro("", "") - 1.0).abs() < 1e-10);
        assert!((edit_distance.jaro("abc", "abc") - 1.0).abs() < 1e-10);
        assert_eq!(edit_distance.jaro("", "abc"), 0.0);

        let similarity = edit_distance.jaro("martha", "marhta");
        assert!(similarity > 0.8); // Should be high due to mostly matching characters
    }

    #[test]
    fn test_jaro_winkler_similarity() {
        let edit_distance = EditDistance::new();

        assert!((edit_distance.jaro_winkler("", "") - 1.0).abs() < 1e-10);
        assert!((edit_distance.jaro_winkler("abc", "abc") - 1.0).abs() < 1e-10);

        let jaro = edit_distance.jaro("martha", "marhta");
        let jaro_winkler = edit_distance.jaro_winkler("martha", "marhta");

        // Jaro-Winkler should be higher due to common prefix
        assert!(jaro_winkler >= jaro);

        // Test prefix bonus
        let no_prefix = edit_distance.jaro_winkler("abcd", "efgh");
        let with_prefix = edit_distance.jaro_winkler("abcd", "abxy");
        assert!(with_prefix > no_prefix);
    }

    #[test]
    fn test_damerau_levenshtein() {
        let edit_distance = EditDistance::new();

        assert_eq!(edit_distance.damerau_levenshtein("abc", "abc"), 0);
        assert_eq!(edit_distance.damerau_levenshtein("ca", "abc"), 2);

        // Transposition should cost 1
        assert_eq!(edit_distance.damerau_levenshtein("ab", "ba"), 1);

        // Should be better than regular Levenshtein for transpositions
        let regular = edit_distance.levenshtein("abcde", "acbde");
        let damerau = edit_distance.damerau_levenshtein("abcde", "acbde");
        assert!(damerau <= regular);
    }

    #[test]
    fn test_case_insensitive() {
        let edit_distance =
            EditDistance::new().with_config(EditDistanceConfig::new().with_ignore_case(true));

        assert_eq!(edit_distance.levenshtein("ABC", "abc"), 0);
        assert!((edit_distance.normalized_levenshtein("ABC", "abc") - 1.0).abs() < 1e-10);
        assert!((edit_distance.jaro("ABC", "abc") - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_custom_costs() {
        let config = EditDistanceConfig::new()
            .with_insertion_cost(1.0)
            .with_deletion_cost(1.0)
            .with_substitution_cost(2.0);

        let edit_distance = EditDistance::with_config(config);

        // With higher substitution cost, should prefer insertion+deletion over substitution
        let distance = edit_distance.levenshtein("a", "b");
        assert_eq!(distance, 2); // delete 'a', insert 'b'
    }

    #[test]
    fn test_find_most_similar() {
        let edit_distance = EditDistance::new();
        let candidates = &["hello", "world", "help", "held"];

        let result = edit_distance
            .find_most_similar("helo", candidates, DistanceAlgorithm::Levenshtein)
            .expect("operation should succeed");

        assert!(result.is_some());
        let best_match = result.expect("operation should succeed");
        assert_eq!(best_match.text, "hello"); // Should be closest
        assert!(best_match.similarity > 0.5);
    }

    #[test]
    fn test_pairwise_distances() {
        let edit_distance = EditDistance::new();
        let strings = &["abc", "abd", "xyz"];

        let distances = edit_distance
            .pairwise_distances(strings, DistanceAlgorithm::Levenshtein)
            .expect("operation should succeed");

        assert_eq!(distances.len(), 3);
        assert_eq!(distances[0].len(), 3);

        // Diagonal should be zero
        for i in 0..3 {
            assert_eq!(distances[i][i], 0.0);
        }

        // Matrix should be symmetric
        for i in 0..3 {
            for j in 0..3 {
                assert!((distances[i][j] - distances[j][i]).abs() < 1e-10);
            }
        }
    }

    #[test]
    fn test_legacy_functions() {
        assert_eq!(EditDistance::levenshtein_static("kitten", "sitting"), 3);

        let similarity = EditDistance::normalized_levenshtein_static("abc", "abc");
        assert!((similarity - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_similarity_match_helpers() {
        let match_result = SimilarityMatch {
            index: 0,
            text: "test".to_string(),
            similarity: 0.85,
            distance: 1.0,
        };

        assert!(match_result.is_high_quality());
        assert!(match_result.is_acceptable());
        assert_eq!(match_result.quality_category(), "Good");
    }

    #[test]
    fn test_unicode_support() {
        let edit_distance = EditDistance::new();

        // Test with Unicode characters
        let dist = edit_distance.levenshtein("café", "caffe");
        assert!(dist > 0);

        let similarity = edit_distance.normalized_levenshtein("naïve", "naive");
        assert!(similarity > 0.5);
    }

    #[test]
    fn test_algorithms_enum() {
        let edit_distance = EditDistance::new();

        for algorithm in &[
            DistanceAlgorithm::Levenshtein,
            DistanceAlgorithm::DamerauLevenshtein,
            DistanceAlgorithm::OptimalStringAlignment,
            DistanceAlgorithm::Jaro,
            DistanceAlgorithm::JaroWinkler,
        ] {
            let result = edit_distance.calculate("test", "best", *algorithm);
            assert!(result.is_ok());

            let similarity = edit_distance.similarity("test", "best", *algorithm);
            assert!(similarity.is_ok());
            assert!(similarity.expect("operation should succeed") >= 0.0 && similarity.expect("operation should succeed") <= 1.0);
        }

        // Hamming requires equal length strings
        let result = edit_distance.calculate("test", "best", DistanceAlgorithm::Hamming);
        assert!(result.is_ok());

        let result = edit_distance.calculate("test", "testing", DistanceAlgorithm::Hamming);
        assert!(result.is_err());
    }
}