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
//! Timing Analysis Module
//!
//! This module provides comprehensive timing analysis for prosodic fluency,
//! including pause placement evaluation, tempo analysis, duration patterns,
//! speech rate calculation, and timing variability assessment with sophisticated
//! prosodic break detection and syllable timing analysis.

use super::config::TimingAnalysisConfig;
use super::results::{
    BreakType, ChangeDirection, ConsistencyMetrics, DurationAnalysis, DurationPattern,
    IsochronyClass, IsochronyMeasures, LengtheningEffect, LengtheningType, ProsodicBreak,
    SpeechRateAnalysis, SpeechRateChange, SpeechRateClass, SyllableTimingAnalysis,
    SyllableTimingPattern, TempoAnalysis, TempoChange, TempoClass, TimingMetrics,
    TimingVariabilityAnalysis, VariabilityPattern,
};
use crate::error::TextAnalysisError;
use serde::{Deserialize, Serialize};
use std::collections::hash_map::DefaultHasher;
use std::collections::{HashMap, HashSet, VecDeque};
use std::hash::{Hash, Hasher};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum TimingAnalysisError {
    #[error("Invalid timing analysis configuration: {0}")]
    ConfigError(String),
    #[error("Timing calculation failed: {0}")]
    CalculationError(String),
    #[error("Pause analysis error: {0}")]
    PauseError(String),
    #[error("Tempo analysis error: {0}")]
    TempoError(String),
}

pub type TimingResult<T> = Result<T, TimingAnalysisError>;

/// Timing information for syllables and segments
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimingInfo {
    /// Syllable durations (milliseconds)
    pub syllable_durations: Vec<f64>,
    /// Pause durations between syllables
    pub pause_durations: Vec<f64>,
    /// Speaking rate (syllables per second)
    pub speaking_rate: f64,
    /// Overall timing confidence
    pub confidence: f64,
    /// Timing context information
    pub context: String,
}

impl TimingInfo {
    /// Create new timing information
    pub fn new(syllable_durations: Vec<f64>, speaking_rate: f64) -> Self {
        let pause_durations = vec![0.0; syllable_durations.len().saturating_sub(1)];

        Self {
            syllable_durations,
            pause_durations,
            speaking_rate,
            confidence: 0.8,
            context: String::new(),
        }
    }

    /// Calculate total duration
    pub fn total_duration(&self) -> f64 {
        self.syllable_durations.iter().sum::<f64>() + self.pause_durations.iter().sum::<f64>()
    }

    /// Calculate average syllable duration
    pub fn average_syllable_duration(&self) -> f64 {
        if self.syllable_durations.is_empty() {
            0.0
        } else {
            self.syllable_durations.iter().sum::<f64>() / self.syllable_durations.len() as f64
        }
    }

    /// Calculate speech rate (syllables per second)
    pub fn calculate_speech_rate(&self) -> f64 {
        let total_time = self.total_duration() / 1000.0; // Convert to seconds
        if total_time > 0.0 {
            self.syllable_durations.len() as f64 / total_time
        } else {
            0.0
        }
    }
}

/// Pause placement analyzer for evaluating prosodic breaks
#[derive(Debug, Clone)]
pub struct PausePlacementAnalyzer {
    config: TimingAnalysisConfig,
    pause_detection_params: PauseDetectionParams,
    break_classification_params: BreakClassificationParams,
}

#[derive(Debug, Clone)]
pub struct PauseDetectionParams {
    /// Minimum pause duration threshold (ms)
    pub min_pause_duration: f64,
    /// Silence threshold for pause detection
    pub silence_threshold: f64,
    /// Context window for pause analysis
    pub context_window: usize,
    /// Breathing pause detection sensitivity
    pub breathing_sensitivity: f64,
}

#[derive(Debug, Clone)]
pub struct BreakClassificationParams {
    /// Classification thresholds for break types
    pub minor_break_threshold: f64,
    pub major_break_threshold: f64,
    pub boundary_break_threshold: f64,
    /// Strength calculation weights
    pub duration_weight: f64,
    pub context_weight: f64,
    pub syntactic_weight: f64,
}

impl Default for PauseDetectionParams {
    fn default() -> Self {
        Self {
            min_pause_duration: 50.0,
            silence_threshold: 0.1,
            context_window: 3,
            breathing_sensitivity: 0.7,
        }
    }
}

impl Default for BreakClassificationParams {
    fn default() -> Self {
        Self {
            minor_break_threshold: 100.0,
            major_break_threshold: 200.0,
            boundary_break_threshold: 400.0,
            duration_weight: 0.4,
            context_weight: 0.3,
            syntactic_weight: 0.3,
        }
    }
}

impl PausePlacementAnalyzer {
    /// Create new pause placement analyzer
    pub fn new(config: TimingAnalysisConfig) -> Self {
        Self {
            config,
            pause_detection_params: PauseDetectionParams::default(),
            break_classification_params: BreakClassificationParams::default(),
        }
    }

    /// Analyze pause placement in timing information
    pub fn analyze_pause_placement(
        &self,
        timing_info: &TimingInfo,
        syllables: &[String],
    ) -> TimingResult<Vec<ProsodicBreak>> {
        let mut prosodic_breaks = Vec::new();

        // Analyze explicit pauses
        for (i, &pause_duration) in timing_info.pause_durations.iter().enumerate() {
            if pause_duration >= self.pause_detection_params.min_pause_duration {
                let break_info =
                    self.analyze_pause_at_position(i, pause_duration, timing_info, syllables)?;
                prosodic_breaks.push(break_info);
            }
        }

        // Detect implicit breaks based on duration patterns
        let implicit_breaks = self.detect_implicit_breaks(timing_info, syllables)?;
        prosodic_breaks.extend(implicit_breaks);

        Ok(prosodic_breaks)
    }

    /// Analyze pause at specific position
    fn analyze_pause_at_position(
        &self,
        position: usize,
        duration: f64,
        timing_info: &TimingInfo,
        syllables: &[String],
    ) -> TimingResult<ProsodicBreak> {
        let break_type = self.classify_break_type(duration)?;
        let strength = self.calculate_break_strength(position, duration, timing_info, syllables)?;
        let context = self.build_break_context(position, syllables);

        Ok(ProsodicBreak {
            position,
            break_type,
            strength,
            duration: Some(duration),
            context,
        })
    }

    /// Classify break type based on duration
    fn classify_break_type(&self, duration: f64) -> TimingResult<BreakType> {
        if duration >= self.break_classification_params.boundary_break_threshold {
            Ok(BreakType::Boundary)
        } else if duration >= self.break_classification_params.major_break_threshold {
            Ok(BreakType::Major)
        } else if duration >= self.break_classification_params.minor_break_threshold {
            Ok(BreakType::Minor)
        } else {
            Ok(BreakType::Pause)
        }
    }

    /// Calculate break strength considering multiple factors
    fn calculate_break_strength(
        &self,
        position: usize,
        duration: f64,
        timing_info: &TimingInfo,
        syllables: &[String],
    ) -> TimingResult<f64> {
        // Duration component
        let duration_strength = (duration / 500.0).min(1.0); // Normalize to 500ms max

        // Context component (syllable boundary strength)
        let context_strength = self.calculate_context_strength(position, timing_info, syllables)?;

        // Syntactic component (simplified - would use actual parsing)
        let syntactic_strength = self.estimate_syntactic_strength(position, syllables);

        // Weighted combination
        let combined_strength = duration_strength
            * self.break_classification_params.duration_weight
            + context_strength * self.break_classification_params.context_weight
            + syntactic_strength * self.break_classification_params.syntactic_weight;

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

    /// Calculate context strength based on surrounding syllables
    fn calculate_context_strength(
        &self,
        position: usize,
        timing_info: &TimingInfo,
        syllables: &[String],
    ) -> TimingResult<f64> {
        if position >= syllables.len() {
            return Ok(0.0);
        }

        let window_size = self.pause_detection_params.context_window;
        let start = position.saturating_sub(window_size);
        let end = (position + window_size + 1).min(syllables.len());

        // Check for natural break points (word boundaries, phrase boundaries)
        let mut context_score = 0.5; // Base score

        // Word boundary detection (simplified)
        if position > 0 && position < syllables.len() {
            let prev_syllable = &syllables[position - 1];
            let next_syllable = &syllables[position];

            // Heuristic: different word if significant syllable difference
            if self.likely_different_words(prev_syllable, next_syllable) {
                context_score += 0.3;
            }
        }

        // Phrase boundary indicators
        if self.likely_phrase_boundary(position, syllables) {
            context_score += 0.4;
        }

        Ok(context_score.min(1.0))
    }

    /// Heuristic to detect different words
    fn likely_different_words(&self, syl1: &str, syl2: &str) -> bool {
        // Simple heuristic based on syllable characteristics
        syl1.len() != syl2.len() || !syl1.chars().next().unwrap_or(' ').is_lowercase()
    }

    /// Heuristic to detect phrase boundaries
    fn likely_phrase_boundary(&self, position: usize, syllables: &[String]) -> bool {
        if position == 0 || position >= syllables.len() - 1 {
            return true;
        }

        // Check for function words that might indicate phrase boundaries
        const FUNCTION_WORDS: &[&str] = &[
            "the", "a", "an", "and", "or", "but", "of", "to", "for", "with",
        ];

        if position < syllables.len() {
            let syllable = syllables[position].to_lowercase();
            FUNCTION_WORDS.iter().any(|&fw| syllable.starts_with(fw))
        } else {
            false
        }
    }

    /// Estimate syntactic strength (simplified)
    fn estimate_syntactic_strength(&self, position: usize, syllables: &[String]) -> f64 {
        // Simplified syntactic boundary detection
        let relative_position = position as f64 / syllables.len() as f64;

        // Stronger breaks at clause/sentence boundaries (beginning and end)
        if relative_position < 0.1 || relative_position > 0.9 {
            0.8
        } else if relative_position > 0.4 && relative_position < 0.6 {
            0.7 // Mid-sentence phrase boundaries
        } else {
            0.4 // Word boundaries
        }
    }

    /// Build context description for break
    fn build_break_context(&self, position: usize, syllables: &[String]) -> String {
        let start = position.saturating_sub(2);
        let end = (position + 3).min(syllables.len());

        if start < end {
            syllables[start..end].join(" ")
        } else {
            "boundary".to_string()
        }
    }

    /// Detect implicit breaks based on duration patterns
    fn detect_implicit_breaks(
        &self,
        timing_info: &TimingInfo,
        syllables: &[String],
    ) -> TimingResult<Vec<ProsodicBreak>> {
        let mut implicit_breaks = Vec::new();

        if timing_info.syllable_durations.len() < 2 {
            return Ok(implicit_breaks);
        }

        // Look for unusually long syllables that might indicate implicit breaks
        let mean_duration = timing_info.average_syllable_duration();
        let threshold = mean_duration * 1.5; // 50% longer than average

        for (i, &duration) in timing_info.syllable_durations.iter().enumerate() {
            if duration >= threshold {
                // Check if this is a natural lengthening position
                if self.is_natural_lengthening_position(i, syllables) {
                    let break_strength =
                        self.calculate_implicit_break_strength(duration, mean_duration);

                    implicit_breaks.push(ProsodicBreak {
                        position: i,
                        break_type: BreakType::Minor,
                        strength: break_strength,
                        duration: Some(duration),
                        context: self.build_break_context(i, syllables),
                    });
                }
            }
        }

        Ok(implicit_breaks)
    }

    /// Check if position is natural for lengthening
    fn is_natural_lengthening_position(&self, position: usize, syllables: &[String]) -> bool {
        // Phrase-final lengthening is common
        let relative_position = position as f64 / syllables.len() as f64;
        relative_position > 0.8 || // Near end
        position == syllables.len() - 1 || // Final syllable
        self.likely_phrase_boundary(position, syllables) // Phrase boundary
    }

    /// Calculate implicit break strength
    fn calculate_implicit_break_strength(&self, duration: f64, mean_duration: f64) -> f64 {
        if mean_duration > 0.0 {
            let lengthening_ratio = duration / mean_duration;
            ((lengthening_ratio - 1.0) / 2.0).min(1.0).max(0.0)
        } else {
            0.0
        }
    }

    /// Calculate overall pause placement accuracy
    pub fn calculate_pause_accuracy(
        &self,
        breaks: &[ProsodicBreak],
        expected_breaks: &[usize],
    ) -> f64 {
        if expected_breaks.is_empty() {
            return if breaks.is_empty() { 1.0 } else { 0.5 };
        }

        let detected_positions: HashSet<usize> = breaks.iter().map(|b| b.position).collect();
        let expected_positions: HashSet<usize> = expected_breaks.iter().cloned().collect();

        let intersection_size = detected_positions.intersection(&expected_positions).count();
        let union_size = detected_positions.union(&expected_positions).count();

        if union_size > 0 {
            intersection_size as f64 / union_size as f64
        } else {
            1.0
        }
    }
}

/// Tempo analyzer for analyzing speech rate and tempo patterns
#[derive(Debug, Clone)]
pub struct TempoAnalyzer {
    config: TimingAnalysisConfig,
    tempo_analysis_params: TempoAnalysisParams,
    rate_classification_params: RateClassificationParams,
}

#[derive(Debug, Clone)]
pub struct TempoAnalysisParams {
    /// Window size for tempo analysis (number of syllables)
    pub analysis_window: usize,
    /// Minimum tempo change threshold (syllables/second)
    pub min_tempo_change: f64,
    /// Smoothing factor for tempo calculation
    pub smoothing_factor: f64,
    /// Tempo regularity analysis depth
    pub regularity_depth: usize,
}

#[derive(Debug, Clone)]
pub struct RateClassificationParams {
    /// Speech rate thresholds (syllables per second)
    pub very_slow_threshold: f64,
    pub slow_threshold: f64,
    pub normal_range: (f64, f64),
    pub fast_threshold: f64,
    pub very_fast_threshold: f64,
}

impl Default for TempoAnalysisParams {
    fn default() -> Self {
        Self {
            analysis_window: 5,
            min_tempo_change: 0.5,
            smoothing_factor: 0.3,
            regularity_depth: 10,
        }
    }
}

impl Default for RateClassificationParams {
    fn default() -> Self {
        Self {
            very_slow_threshold: 2.0,
            slow_threshold: 3.0,
            normal_range: (3.5, 5.5),
            fast_threshold: 6.0,
            very_fast_threshold: 7.5,
        }
    }
}

impl TempoAnalyzer {
    /// Create new tempo analyzer
    pub fn new(config: TimingAnalysisConfig) -> Self {
        Self {
            config,
            tempo_analysis_params: TempoAnalysisParams::default(),
            rate_classification_params: RateClassificationParams::default(),
        }
    }

    /// Analyze tempo patterns in timing information
    pub fn analyze_tempo(&self, timing_info: &TimingInfo) -> TimingResult<TempoAnalysis> {
        let local_tempos = self.calculate_local_tempos(timing_info)?;
        let average_tempo = self.calculate_average_tempo(&local_tempos);
        let tempo_variability = self.calculate_tempo_variability(&local_tempos);
        let tempo_changes = self.detect_tempo_changes(&local_tempos)?;
        let regularity = self.calculate_tempo_regularity(&local_tempos);
        let tempo_class = self.classify_tempo(average_tempo);

        Ok(TempoAnalysis {
            average_tempo,
            tempo_variability,
            tempo_changes,
            regularity,
            tempo_class,
        })
    }

    /// Calculate local tempos using sliding window
    fn calculate_local_tempos(&self, timing_info: &TimingInfo) -> TimingResult<Vec<f64>> {
        let window_size = self.tempo_analysis_params.analysis_window;
        let mut local_tempos = Vec::new();

        if timing_info.syllable_durations.len() < window_size {
            // Not enough data for windowed analysis
            let overall_tempo = timing_info.calculate_speech_rate();
            return Ok(vec![overall_tempo]);
        }

        for i in 0..=timing_info
            .syllable_durations
            .len()
            .saturating_sub(window_size)
        {
            let window_durations = &timing_info.syllable_durations[i..i + window_size];
            let window_total_ms = window_durations.iter().sum::<f64>();
            let window_total_s = window_total_ms / 1000.0;

            let local_tempo = if window_total_s > 0.0 {
                window_size as f64 / window_total_s
            } else {
                0.0
            };

            local_tempos.push(local_tempo);
        }

        // Apply smoothing
        Ok(self.smooth_tempo_sequence(&local_tempos))
    }

    /// Apply smoothing to tempo sequence
    fn smooth_tempo_sequence(&self, tempos: &[f64]) -> Vec<f64> {
        if tempos.len() < 2 {
            return tempos.to_vec();
        }

        let mut smoothed = Vec::with_capacity(tempos.len());
        let alpha = self.tempo_analysis_params.smoothing_factor;

        smoothed.push(tempos[0]);

        for i in 1..tempos.len() {
            let smoothed_value = alpha * tempos[i] + (1.0 - alpha) * smoothed[i - 1];
            smoothed.push(smoothed_value);
        }

        smoothed
    }

    /// Calculate average tempo
    fn calculate_average_tempo(&self, local_tempos: &[f64]) -> f64 {
        if local_tempos.is_empty() {
            0.0
        } else {
            local_tempos.iter().sum::<f64>() / local_tempos.len() as f64
        }
    }

    /// Calculate tempo variability
    fn calculate_tempo_variability(&self, local_tempos: &[f64]) -> f64 {
        if local_tempos.len() < 2 {
            return 0.0;
        }

        let mean = self.calculate_average_tempo(local_tempos);
        let variance = local_tempos
            .iter()
            .map(|&tempo| (tempo - mean).powi(2))
            .sum::<f64>()
            / local_tempos.len() as f64;

        variance.sqrt()
    }

    /// Detect tempo changes
    fn detect_tempo_changes(&self, local_tempos: &[f64]) -> TimingResult<Vec<TempoChange>> {
        let mut tempo_changes = Vec::new();
        let min_change = self.tempo_analysis_params.min_tempo_change;

        for i in 1..local_tempos.len() {
            let prev_tempo = local_tempos[i - 1];
            let curr_tempo = local_tempos[i];
            let change_magnitude = (curr_tempo - prev_tempo).abs();

            if change_magnitude >= min_change {
                let direction = if curr_tempo > prev_tempo {
                    ChangeDirection::Increase
                } else if curr_tempo < prev_tempo {
                    ChangeDirection::Decrease
                } else {
                    ChangeDirection::Stable
                };

                tempo_changes.push(TempoChange {
                    position: i,
                    magnitude: change_magnitude,
                    direction,
                    context: format!(
                        "tempo change from {:.1} to {:.1} syl/s",
                        prev_tempo, curr_tempo
                    ),
                });
            }
        }

        Ok(tempo_changes)
    }

    /// Calculate tempo regularity
    fn calculate_tempo_regularity(&self, local_tempos: &[f64]) -> f64 {
        if local_tempos.len() < 2 {
            return 1.0;
        }

        let variability = self.calculate_tempo_variability(local_tempos);
        let mean_tempo = self.calculate_average_tempo(local_tempos);

        if mean_tempo > 0.0 {
            let coefficient_of_variation = variability / mean_tempo;
            1.0 / (1.0 + coefficient_of_variation)
        } else {
            0.0
        }
    }

    /// Classify tempo based on speech rate
    fn classify_tempo(&self, average_tempo: f64) -> TempoClass {
        let params = &self.rate_classification_params;

        if average_tempo <= params.very_slow_threshold {
            TempoClass::VerySlow
        } else if average_tempo <= params.slow_threshold {
            TempoClass::Slow
        } else if average_tempo >= params.very_fast_threshold {
            TempoClass::VeryFast
        } else if average_tempo >= params.fast_threshold {
            TempoClass::Fast
        } else {
            TempoClass::Moderate
        }
    }
}

/// Duration analyzer for analyzing syllable duration patterns
#[derive(Debug, Clone)]
pub struct DurationAnalyzer {
    config: TimingAnalysisConfig,
    duration_analysis_params: DurationAnalysisParams,
}

#[derive(Debug, Clone)]
pub struct DurationAnalysisParams {
    /// Pattern detection window size
    pub pattern_window: usize,
    /// Minimum pattern strength threshold
    pub pattern_threshold: f64,
    /// Lengthening detection sensitivity
    pub lengthening_sensitivity: f64,
    /// Duration normalization parameters
    pub normalization_params: DurationNormalizationParams,
}

#[derive(Debug, Clone)]
pub struct DurationNormalizationParams {
    /// Minimum expected syllable duration (ms)
    pub min_syllable_duration: f64,
    /// Maximum expected syllable duration (ms)
    pub max_syllable_duration: f64,
    /// Normalization method
    pub normalization_method: NormalizationMethod,
}

#[derive(Debug, Clone)]
pub enum NormalizationMethod {
    ZScore,
    MinMax,
    Quantile,
}

impl Default for DurationAnalysisParams {
    fn default() -> Self {
        Self {
            pattern_window: 4,
            pattern_threshold: 0.6,
            lengthening_sensitivity: 0.3,
            normalization_params: DurationNormalizationParams {
                min_syllable_duration: 50.0,
                max_syllable_duration: 500.0,
                normalization_method: NormalizationMethod::ZScore,
            },
        }
    }
}

impl DurationAnalyzer {
    /// Create new duration analyzer
    pub fn new(config: TimingAnalysisConfig) -> Self {
        Self {
            config,
            duration_analysis_params: DurationAnalysisParams::default(),
        }
    }

    /// Analyze duration patterns
    pub fn analyze_duration_patterns(
        &self,
        timing_info: &TimingInfo,
    ) -> TimingResult<DurationAnalysis> {
        let normalized_durations = self.normalize_durations(&timing_info.syllable_durations)?;

        let average_duration = timing_info.average_syllable_duration();
        let duration_variability = self.calculate_duration_variability(&normalized_durations);
        let duration_patterns = self.detect_duration_patterns(&normalized_durations)?;
        let lengthening_effects =
            self.detect_lengthening_effects(&timing_info.syllable_durations)?;

        Ok(DurationAnalysis {
            average_syllable_duration: average_duration,
            duration_variability,
            duration_patterns,
            lengthening_effects,
        })
    }

    /// Normalize durations for analysis
    fn normalize_durations(&self, durations: &[f64]) -> TimingResult<Vec<f64>> {
        if durations.is_empty() {
            return Ok(Vec::new());
        }

        match self
            .duration_analysis_params
            .normalization_params
            .normalization_method
        {
            NormalizationMethod::ZScore => {
                let mean = durations.iter().sum::<f64>() / durations.len() as f64;
                let variance = durations.iter().map(|&d| (d - mean).powi(2)).sum::<f64>()
                    / durations.len() as f64;
                let std_dev = variance.sqrt();

                if std_dev > 0.0 {
                    Ok(durations.iter().map(|&d| (d - mean) / std_dev).collect())
                } else {
                    Ok(vec![0.0; durations.len()])
                }
            }
            NormalizationMethod::MinMax => {
                let min_duration = durations.iter().fold(f64::INFINITY, |a, &b| a.min(b));
                let max_duration = durations.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));
                let range = max_duration - min_duration;

                if range > 0.0 {
                    Ok(durations
                        .iter()
                        .map(|&d| (d - min_duration) / range)
                        .collect())
                } else {
                    Ok(vec![0.5; durations.len()])
                }
            }
            NormalizationMethod::Quantile => {
                // Simple quantile normalization (median-based)
                let mut sorted_durations = durations.to_vec();
                sorted_durations.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
                let median = if sorted_durations.len() % 2 == 0 {
                    let mid = sorted_durations.len() / 2;
                    (sorted_durations[mid - 1] + sorted_durations[mid]) / 2.0
                } else {
                    sorted_durations[sorted_durations.len() / 2]
                };

                if median > 0.0 {
                    Ok(durations.iter().map(|&d| d / median).collect())
                } else {
                    Ok(vec![1.0; durations.len()])
                }
            }
        }
    }

    /// Calculate duration variability
    fn calculate_duration_variability(&self, normalized_durations: &[f64]) -> f64 {
        if normalized_durations.len() < 2 {
            return 0.0;
        }

        let mean = normalized_durations.iter().sum::<f64>() / normalized_durations.len() as f64;
        let variance = normalized_durations
            .iter()
            .map(|&d| (d - mean).powi(2))
            .sum::<f64>()
            / normalized_durations.len() as f64;

        variance.sqrt()
    }

    /// Detect duration patterns
    fn detect_duration_patterns(
        &self,
        normalized_durations: &[f64],
    ) -> TimingResult<Vec<DurationPattern>> {
        let mut patterns = Vec::new();
        let window_size = self.duration_analysis_params.pattern_window;

        if normalized_durations.len() < window_size {
            return Ok(patterns);
        }

        // Sliding window pattern detection
        for i in 0..=normalized_durations.len().saturating_sub(window_size) {
            let window = &normalized_durations[i..i + window_size];

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

        // Merge overlapping patterns
        Ok(self.merge_overlapping_patterns(patterns))
    }

    /// Analyze duration window for patterns
    fn analyze_duration_window(
        &self,
        window: &[f64],
        start_position: usize,
    ) -> TimingResult<Option<DurationPattern>> {
        // Check for different pattern types
        if let Some(pattern) = self.detect_alternating_pattern(window, start_position) {
            return Ok(Some(pattern));
        }

        if let Some(pattern) = self.detect_ascending_pattern(window, start_position) {
            return Ok(Some(pattern));
        }

        if let Some(pattern) = self.detect_descending_pattern(window, start_position) {
            return Ok(Some(pattern));
        }

        Ok(None)
    }

    /// Detect alternating duration pattern
    fn detect_alternating_pattern(
        &self,
        window: &[f64],
        start_position: usize,
    ) -> Option<DurationPattern> {
        if window.len() < 4 {
            return None;
        }

        let mut alternations = 0;
        let mut total_comparisons = 0;

        for i in 2..window.len() {
            let trend_prev = window[i - 1] > window[i - 2];
            let trend_curr = window[i] > window[i - 1];

            if trend_prev != trend_curr {
                alternations += 1;
            }
            total_comparisons += 1;
        }

        let alternation_ratio = alternations as f64 / total_comparisons as f64;

        if alternation_ratio >= self.duration_analysis_params.pattern_threshold {
            Some(DurationPattern {
                pattern_type: "alternating".to_string(),
                strength: alternation_ratio,
                positions: (start_position..start_position + window.len()).collect(),
                regularity: self.calculate_pattern_regularity(window),
            })
        } else {
            None
        }
    }

    /// Detect ascending duration pattern
    fn detect_ascending_pattern(
        &self,
        window: &[f64],
        start_position: usize,
    ) -> Option<DurationPattern> {
        let ascending_count = window.windows(2).filter(|pair| pair[1] > pair[0]).count();

        let ascending_ratio = ascending_count as f64 / (window.len() - 1) as f64;

        if ascending_ratio >= self.duration_analysis_params.pattern_threshold {
            Some(DurationPattern {
                pattern_type: "ascending".to_string(),
                strength: ascending_ratio,
                positions: (start_position..start_position + window.len()).collect(),
                regularity: self.calculate_pattern_regularity(window),
            })
        } else {
            None
        }
    }

    /// Detect descending duration pattern
    fn detect_descending_pattern(
        &self,
        window: &[f64],
        start_position: usize,
    ) -> Option<DurationPattern> {
        let descending_count = window.windows(2).filter(|pair| pair[1] < pair[0]).count();

        let descending_ratio = descending_count as f64 / (window.len() - 1) as f64;

        if descending_ratio >= self.duration_analysis_params.pattern_threshold {
            Some(DurationPattern {
                pattern_type: "descending".to_string(),
                strength: descending_ratio,
                positions: (start_position..start_position + window.len()).collect(),
                regularity: self.calculate_pattern_regularity(window),
            })
        } else {
            None
        }
    }

    /// Calculate pattern regularity
    fn calculate_pattern_regularity(&self, window: &[f64]) -> f64 {
        if window.len() < 2 {
            return 1.0;
        }

        let differences: Vec<f64> = window
            .windows(2)
            .map(|pair| (pair[1] - pair[0]).abs())
            .collect();

        let mean_diff = differences.iter().sum::<f64>() / differences.len() as f64;
        let diff_variance = differences
            .iter()
            .map(|&d| (d - mean_diff).powi(2))
            .sum::<f64>()
            / differences.len() as f64;

        if mean_diff > 0.0 {
            1.0 / (1.0 + diff_variance.sqrt() / mean_diff)
        } else {
            1.0
        }
    }

    /// Merge overlapping patterns
    fn merge_overlapping_patterns(&self, patterns: Vec<DurationPattern>) -> Vec<DurationPattern> {
        // Simple non-overlapping selection - would be enhanced for production
        let mut merged = Vec::new();
        let mut last_end_position = 0;

        for pattern in patterns {
            if let Some(&first_pos) = pattern.positions.first() {
                if first_pos >= last_end_position {
                    if let Some(&last_pos) = pattern.positions.last() {
                        last_end_position = last_pos + 1;
                        merged.push(pattern);
                    }
                }
            }
        }

        merged
    }

    /// Detect lengthening effects
    fn detect_lengthening_effects(
        &self,
        durations: &[f64],
    ) -> TimingResult<Vec<LengtheningEffect>> {
        let mut lengthening_effects = Vec::new();

        if durations.is_empty() {
            return Ok(lengthening_effects);
        }

        let mean_duration = durations.iter().sum::<f64>() / durations.len() as f64;
        let lengthening_threshold =
            mean_duration * (1.0 + self.duration_analysis_params.lengthening_sensitivity);

        for (i, &duration) in durations.iter().enumerate() {
            if duration >= lengthening_threshold {
                let lengthening_factor = duration / mean_duration;
                let lengthening_type = self.classify_lengthening_type(i, durations.len());
                let cause = self.determine_lengthening_cause(i, durations);

                lengthening_effects.push(LengtheningEffect {
                    position: i,
                    factor: lengthening_factor,
                    lengthening_type,
                    cause,
                });
            }
        }

        Ok(lengthening_effects)
    }

    /// Classify type of lengthening
    fn classify_lengthening_type(&self, position: usize, total_length: usize) -> LengtheningType {
        let relative_position = position as f64 / total_length as f64;

        if relative_position > 0.8 {
            LengtheningType::PhraseFinal
        } else if relative_position > 0.6 {
            LengtheningType::PreBoundary
        } else {
            // Check context for stress or focus (simplified)
            LengtheningType::Stress
        }
    }

    /// Determine cause of lengthening
    fn determine_lengthening_cause(&self, position: usize, durations: &[f64]) -> String {
        let relative_position = position as f64 / durations.len() as f64;

        if relative_position > 0.9 {
            "phrase-final lengthening".to_string()
        } else if relative_position < 0.1 {
            "phrase-initial emphasis".to_string()
        } else {
            "stress-induced lengthening".to_string()
        }
    }
}

/// Main timing analyzer orchestrating all timing components
#[derive(Debug, Clone)]
pub struct TimingAnalyzer {
    config: TimingAnalysisConfig,
    pause_analyzer: PausePlacementAnalyzer,
    tempo_analyzer: TempoAnalyzer,
    duration_analyzer: DurationAnalyzer,
    analysis_cache: HashMap<u64, TimingMetrics>,
}

impl TimingAnalyzer {
    /// Create new timing analyzer
    pub fn new(config: TimingAnalysisConfig) -> TimingResult<Self> {
        Self::validate_config(&config)?;

        let pause_analyzer = PausePlacementAnalyzer::new(config.clone());
        let tempo_analyzer = TempoAnalyzer::new(config.clone());
        let duration_analyzer = DurationAnalyzer::new(config.clone());

        Ok(Self {
            config,
            pause_analyzer,
            tempo_analyzer,
            duration_analyzer,
            analysis_cache: HashMap::new(),
        })
    }

    /// Analyze timing patterns in text
    pub fn analyze_timing(
        &mut self,
        sentences: &[String],
        timing_data: Option<&TimingInfo>,
    ) -> TimingResult<TimingMetrics> {
        let cache_key = self.generate_cache_key(sentences, timing_data);
        if let Some(cached) = self.analysis_cache.get(&cache_key) {
            return Ok(cached.clone());
        }

        // Extract or simulate timing information
        let timing_info = if let Some(timing) = timing_data {
            timing.clone()
        } else {
            self.simulate_timing_info(sentences)?
        };

        // Overall timing score
        let overall_score = self.calculate_overall_timing_score(&timing_info, sentences)?;

        // Pause placement analysis
        let prosodic_breaks = self
            .pause_analyzer
            .analyze_pause_placement(&timing_info, &self.extract_syllables(sentences))?;
        let expected_breaks = self.estimate_expected_breaks(sentences);
        let pause_accuracy = self
            .pause_analyzer
            .calculate_pause_accuracy(&prosodic_breaks, &expected_breaks);

        // Tempo analysis
        let tempo_analysis = if self.config.enable_tempo_analysis {
            Some(self.tempo_analyzer.analyze_tempo(&timing_info)?)
        } else {
            None
        };

        // Duration analysis
        let duration_analysis = if self.config.enable_duration_analysis {
            Some(
                self.duration_analyzer
                    .analyze_duration_patterns(&timing_info)?,
            )
        } else {
            None
        };

        // Speech rate analysis
        let speech_rate_analysis = if self.config.calculate_speech_rate {
            Some(self.analyze_speech_rate(&timing_info, sentences)?)
        } else {
            None
        };

        // Timing variability analysis
        let variability_analysis = if self.config.analyze_timing_variability {
            Some(self.analyze_timing_variability(&timing_info)?)
        } else {
            None
        };

        // Syllable timing analysis
        let syllable_timing = if self.config.analyze_syllable_timing {
            Some(self.analyze_syllable_timing(&timing_info, sentences)?)
        } else {
            None
        };

        let metrics = TimingMetrics {
            overall_timing_score: overall_score,
            pause_accuracy,
            tempo_analysis,
            duration_analysis,
            speech_rate_analysis,
            variability_analysis,
            break_analysis: prosodic_breaks,
            syllable_timing,
        };

        // Cache results
        self.analysis_cache.insert(cache_key, metrics.clone());

        Ok(metrics)
    }

    // Helper methods for timing analysis

    fn validate_config(config: &TimingAnalysisConfig) -> TimingResult<()> {
        if config.pause_weight < 0.0 {
            return Err(TimingAnalysisError::ConfigError(
                "Pause weight must be non-negative".to_string(),
            ));
        }

        if config.pause_accuracy_threshold < 0.0 || config.pause_accuracy_threshold > 1.0 {
            return Err(TimingAnalysisError::ConfigError(
                "Pause accuracy threshold must be between 0.0 and 1.0".to_string(),
            ));
        }

        Ok(())
    }

    fn generate_cache_key(&self, sentences: &[String], timing_data: Option<&TimingInfo>) -> u64 {
        let mut hasher = DefaultHasher::new();

        for sentence in sentences {
            sentence.hash(&mut hasher);
        }

        if let Some(timing) = timing_data {
            for &duration in &timing.syllable_durations {
                (duration as i64).hash(&mut hasher);
            }
            (timing.speaking_rate as i64).hash(&mut hasher);
        }

        self.config.enabled.hash(&mut hasher);
        hasher.finish()
    }

    fn simulate_timing_info(&self, sentences: &[String]) -> TimingResult<TimingInfo> {
        let syllables = self.extract_syllables(sentences);
        let mut syllable_durations = Vec::new();

        // Simple duration simulation based on syllable characteristics
        for syllable in &syllables {
            let base_duration = 120.0; // ms
            let length_factor = (syllable.len() as f64).min(4.0) / 4.0;
            let vowel_count = syllable
                .chars()
                .filter(|c| "aeiouAEIOU".contains(*c))
                .count();
            let vowel_factor = 1.0 + (vowel_count as f64 * 0.1);

            let duration = base_duration * (0.8 + length_factor * 0.4) * vowel_factor;
            syllable_durations.push(duration);
        }

        let speaking_rate = if syllables.len() > 0 {
            let total_duration_s = syllable_durations.iter().sum::<f64>() / 1000.0;
            syllables.len() as f64 / total_duration_s
        } else {
            4.0 // Default rate
        };

        Ok(TimingInfo::new(syllable_durations, speaking_rate))
    }

    fn extract_syllables(&self, sentences: &[String]) -> Vec<String> {
        let mut syllables = Vec::new();

        for sentence in sentences {
            let words: Vec<&str> = sentence.split_whitespace().collect();
            for word in words {
                let word_syllables = self.extract_word_syllables(word);
                syllables.extend(word_syllables);
            }
        }

        syllables
    }

    fn extract_word_syllables(&self, word: &str) -> Vec<String> {
        // Simple syllable extraction
        let vowels = "aeiouAEIOU";
        let mut syllables = Vec::new();
        let chars: Vec<char> = word.chars().collect();

        let mut current_syllable = String::new();
        let mut has_vowel = false;

        for ch in chars {
            current_syllable.push(ch);

            if vowels.contains(ch) {
                if has_vowel {
                    continue; // Diphthong
                }
                has_vowel = true;
            } else if has_vowel {
                syllables.push(current_syllable.trim().to_string());
                current_syllable = String::new();
                has_vowel = false;
            }
        }

        if !current_syllable.is_empty() {
            syllables.push(current_syllable);
        }

        if syllables.is_empty() {
            vec![word.to_string()]
        } else {
            syllables
        }
    }

    fn calculate_overall_timing_score(
        &self,
        timing_info: &TimingInfo,
        sentences: &[String],
    ) -> TimingResult<f64> {
        let mut score_components = Vec::new();

        // Rate appropriateness
        let rate_score = self.evaluate_rate_appropriateness(timing_info.speaking_rate);
        score_components.push(rate_score);

        // Duration consistency
        if !timing_info.syllable_durations.is_empty() {
            let consistency_score =
                self.evaluate_duration_consistency(&timing_info.syllable_durations);
            score_components.push(consistency_score);
        }

        // Timing naturalness
        let naturalness_score = self.evaluate_timing_naturalness(timing_info, sentences)?;
        score_components.push(naturalness_score);

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

    fn evaluate_rate_appropriateness(&self, speaking_rate: f64) -> f64 {
        // Optimal speaking rate is typically 3.5-5.5 syllables per second
        let optimal_range = (3.5, 5.5);

        if speaking_rate >= optimal_range.0 && speaking_rate <= optimal_range.1 {
            1.0
        } else if speaking_rate < optimal_range.0 {
            // Too slow
            (speaking_rate / optimal_range.0).min(1.0)
        } else {
            // Too fast
            (optimal_range.1 / speaking_rate).min(1.0)
        }
    }

    fn evaluate_duration_consistency(&self, durations: &[f64]) -> f64 {
        if durations.len() < 2 {
            return 1.0;
        }

        let mean_duration = durations.iter().sum::<f64>() / durations.len() as f64;
        let coefficient_of_variation = if mean_duration > 0.0 {
            let variance = durations
                .iter()
                .map(|&d| (d - mean_duration).powi(2))
                .sum::<f64>()
                / durations.len() as f64;
            variance.sqrt() / mean_duration
        } else {
            1.0
        };

        // Lower variability = higher consistency
        1.0 / (1.0 + coefficient_of_variation)
    }

    fn evaluate_timing_naturalness(
        &self,
        timing_info: &TimingInfo,
        sentences: &[String],
    ) -> TimingResult<f64> {
        // Simple naturalness evaluation based on expected patterns
        let mut naturalness_factors = Vec::new();

        // Final lengthening (common in natural speech)
        if !timing_info.syllable_durations.is_empty() {
            let final_duration = timing_info.syllable_durations.last().expect("syllable_durations verified non-empty above");
            let mean_duration = timing_info.average_syllable_duration();

            if mean_duration > 0.0 {
                let final_ratio = final_duration / mean_duration;
                // Final syllables are often 20-50% longer
                let final_lengthening_score = if final_ratio >= 1.2 && final_ratio <= 1.5 {
                    1.0
                } else {
                    0.7
                };
                naturalness_factors.push(final_lengthening_score);
            }
        }

        // Pause appropriateness
        let total_pause_time = timing_info.pause_durations.iter().sum::<f64>();
        let total_speech_time = timing_info.syllable_durations.iter().sum::<f64>();
        let pause_ratio = if total_speech_time > 0.0 {
            total_pause_time / total_speech_time
        } else {
            0.0
        };

        // Natural pause ratio is typically 10-30% of speech time
        let pause_score = if pause_ratio >= 0.1 && pause_ratio <= 0.3 {
            1.0
        } else if pause_ratio < 0.1 {
            pause_ratio / 0.1
        } else {
            0.3 / pause_ratio
        };
        naturalness_factors.push(pause_score);

        Ok(if naturalness_factors.is_empty() {
            0.5
        } else {
            naturalness_factors.iter().sum::<f64>() / naturalness_factors.len() as f64
        })
    }

    fn estimate_expected_breaks(&self, sentences: &[String]) -> Vec<usize> {
        let mut expected_breaks = Vec::new();
        let mut position = 0;

        for (i, sentence) in sentences.iter().enumerate() {
            let words = sentence.split_whitespace().count();

            // Major breaks at sentence boundaries
            if i > 0 {
                expected_breaks.push(position);
            }

            // Minor breaks within long sentences
            if words > 8 {
                let mid_position = position + words / 2;
                expected_breaks.push(mid_position);
            }

            position += words;
        }

        expected_breaks
    }

    fn analyze_speech_rate(
        &self,
        timing_info: &TimingInfo,
        sentences: &[String],
    ) -> TimingResult<SpeechRateAnalysis> {
        let words_per_minute = self.calculate_words_per_minute(timing_info, sentences);
        let syllables_per_second = timing_info.speaking_rate;
        let rate_variability = self.calculate_rate_variability(timing_info)?;
        let rate_changes = self.detect_rate_changes(timing_info)?;
        let rate_class = self.classify_speech_rate(syllables_per_second);

        Ok(SpeechRateAnalysis {
            words_per_minute,
            syllables_per_second,
            rate_variability,
            rate_changes,
            rate_class,
        })
    }

    fn calculate_words_per_minute(&self, timing_info: &TimingInfo, sentences: &[String]) -> f64 {
        let word_count = sentences
            .iter()
            .map(|s| s.split_whitespace().count())
            .sum::<usize>();

        let total_time_minutes = timing_info.total_duration() / 60000.0; // Convert ms to minutes

        if total_time_minutes > 0.0 {
            word_count as f64 / total_time_minutes
        } else {
            0.0
        }
    }

    fn calculate_rate_variability(&self, timing_info: &TimingInfo) -> TimingResult<f64> {
        // Calculate local rates and their variability
        let window_size = 5;
        let mut local_rates = Vec::new();

        for i in 0..=timing_info
            .syllable_durations
            .len()
            .saturating_sub(window_size)
        {
            let window_durations = &timing_info.syllable_durations[i..i + window_size];
            let window_time = window_durations.iter().sum::<f64>() / 1000.0; // Convert to seconds

            if window_time > 0.0 {
                let rate = window_size as f64 / window_time;
                local_rates.push(rate);
            }
        }

        if local_rates.len() < 2 {
            return Ok(0.0);
        }

        let mean_rate = local_rates.iter().sum::<f64>() / local_rates.len() as f64;
        let variance = local_rates
            .iter()
            .map(|&rate| (rate - mean_rate).powi(2))
            .sum::<f64>()
            / local_rates.len() as f64;

        Ok(variance.sqrt())
    }

    fn detect_rate_changes(&self, timing_info: &TimingInfo) -> TimingResult<Vec<SpeechRateChange>> {
        // Simplified rate change detection
        let mut rate_changes = Vec::new();

        // Would implement sliding window rate calculation and change detection
        // For now, return empty vector as placeholder

        Ok(rate_changes)
    }

    fn classify_speech_rate(&self, rate: f64) -> SpeechRateClass {
        if rate < 2.5 {
            SpeechRateClass::VerySlow
        } else if rate < 3.5 {
            SpeechRateClass::Slow
        } else if rate < 5.5 {
            SpeechRateClass::Normal
        } else if rate < 7.0 {
            SpeechRateClass::Fast
        } else {
            SpeechRateClass::VeryFast
        }
    }

    fn analyze_timing_variability(
        &self,
        timing_info: &TimingInfo,
    ) -> TimingResult<TimingVariabilityAnalysis> {
        let overall_variability = self.calculate_overall_variability(timing_info)?;
        let component_variability = self.calculate_component_variability(timing_info)?;
        let variability_patterns = self.detect_variability_patterns(timing_info)?;
        let consistency_metrics = self.calculate_consistency_metrics(timing_info)?;

        Ok(TimingVariabilityAnalysis {
            overall_variability,
            component_variability,
            variability_patterns,
            consistency_metrics,
        })
    }

    fn calculate_overall_variability(&self, timing_info: &TimingInfo) -> TimingResult<f64> {
        self.evaluate_duration_consistency(&timing_info.syllable_durations);
        Ok(1.0 - self.evaluate_duration_consistency(&timing_info.syllable_durations))
    }

    fn calculate_component_variability(
        &self,
        timing_info: &TimingInfo,
    ) -> TimingResult<HashMap<String, f64>> {
        let mut variability = HashMap::new();

        variability.insert(
            "syllable_duration".to_string(),
            1.0 - self.evaluate_duration_consistency(&timing_info.syllable_durations),
        );

        if !timing_info.pause_durations.is_empty() {
            variability.insert(
                "pause_duration".to_string(),
                1.0 - self.evaluate_duration_consistency(&timing_info.pause_durations),
            );
        }

        Ok(variability)
    }

    fn detect_variability_patterns(
        &self,
        timing_info: &TimingInfo,
    ) -> TimingResult<Vec<VariabilityPattern>> {
        // Placeholder for variability pattern detection
        Ok(Vec::new())
    }

    fn calculate_consistency_metrics(
        &self,
        timing_info: &TimingInfo,
    ) -> TimingResult<ConsistencyMetrics> {
        let overall_consistency =
            self.evaluate_duration_consistency(&timing_info.syllable_durations);

        Ok(ConsistencyMetrics {
            beat_consistency: overall_consistency,
            rhythm_consistency: overall_consistency,
            pause_consistency: if timing_info.pause_durations.is_empty() {
                1.0
            } else {
                self.evaluate_duration_consistency(&timing_info.pause_durations)
            },
            overall_consistency,
        })
    }

    fn analyze_syllable_timing(
        &self,
        timing_info: &TimingInfo,
        sentences: &[String],
    ) -> TimingResult<SyllableTimingAnalysis> {
        let average_duration = timing_info.average_syllable_duration();
        let timing_patterns = self.detect_syllable_timing_patterns(timing_info)?;
        let timing_regularity = self.evaluate_duration_consistency(&timing_info.syllable_durations);
        let isochrony_measures = self.analyze_isochrony(timing_info)?;

        Ok(SyllableTimingAnalysis {
            average_duration,
            timing_patterns,
            timing_regularity,
            isochrony_measures,
        })
    }

    fn detect_syllable_timing_patterns(
        &self,
        timing_info: &TimingInfo,
    ) -> TimingResult<Vec<SyllableTimingPattern>> {
        // Placeholder for syllable timing pattern detection
        Ok(Vec::new())
    }

    fn analyze_isochrony(&self, timing_info: &TimingInfo) -> TimingResult<IsochronyMeasures> {
        // Simplified isochrony analysis
        let regularity = self.evaluate_duration_consistency(&timing_info.syllable_durations);

        let classification = if regularity > 0.8 {
            IsochronyClass::StrongSyllableTimed
        } else if regularity > 0.6 {
            IsochronyClass::WeakSyllableTimed
        } else {
            IsochronyClass::Mixed
        };

        Ok(IsochronyMeasures {
            stress_timed: regularity * 0.5, // Simplified
            syllable_timed: regularity,
            mora_timed: Some(regularity * 0.3),
            classification,
        })
    }
}

impl Default for TimingAnalyzer {
    fn default() -> Self {
        Self::new(TimingAnalysisConfig {
            enabled: true,
            pause_weight: 0.15,
            enable_break_detection: true,
            pause_accuracy_threshold: 0.7,
            enable_tempo_analysis: true,
            tempo_regularity_preference: 0.6,
            enable_duration_analysis: true,
            analyze_syllable_timing: true,
            calculate_speech_rate: true,
            analyze_timing_variability: true,
        })
        .expect("timing config should be valid")
    }
}