trustformers-tokenizers 0.1.1

Tokenizers for TrustformeRS
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
//! Music notation tokenizer for TrustformeRS
//!
//! This module provides specialized tokenization for various music notation formats
//! including ABC notation, MusicXML, MIDI, and other musical representations.

use crate::{TokenizedInput, Tokenizer};
use once_cell::sync::Lazy;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use trustformers_core::errors::Result;

/// Configuration for music tokenizer
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MusicTokenizerConfig {
    /// Maximum sequence length
    pub max_length: Option<usize>,
    /// Whether to include special music tokens
    pub include_special_tokens: bool,
    /// Whether to tokenize ABC notation
    pub tokenize_abc: bool,
    /// Whether to tokenize MusicXML
    pub tokenize_musicxml: bool,
    /// Whether to tokenize MIDI representations
    pub tokenize_midi: bool,
    /// Whether to tokenize chord symbols
    pub tokenize_chords: bool,
    /// Whether to preserve timing information
    pub preserve_timing: bool,
    /// Whether to preserve dynamics
    pub preserve_dynamics: bool,
    /// Vocabulary size limit
    pub vocab_size: Option<usize>,
    /// Time resolution for rhythmic values
    pub time_resolution: u32,
}

impl Default for MusicTokenizerConfig {
    fn default() -> Self {
        Self {
            max_length: Some(1024),
            include_special_tokens: true,
            tokenize_abc: true,
            tokenize_musicxml: false,
            tokenize_midi: false,
            tokenize_chords: true,
            preserve_timing: true,
            preserve_dynamics: true,
            vocab_size: Some(5000),
            time_resolution: 480, // Standard MIDI ticks per quarter note
        }
    }
}

/// Types of music tokens
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum MusicTokenType {
    /// Note names (C, D, E, F, G, A, B)
    NoteName,
    /// Accidentals (sharp, flat, natural)
    Accidental,
    /// Octave indicators
    Octave,
    /// Rhythmic values (whole, half, quarter, etc.)
    Duration,
    /// Rest symbols
    Rest,
    /// Time signatures
    TimeSignature,
    /// Key signatures
    KeySignature,
    /// Tempo markings
    Tempo,
    /// Dynamic markings (pp, p, mp, mf, f, ff)
    Dynamic,
    /// Articulation marks (staccato, legato, etc.)
    Articulation,
    /// Chord symbols (C, Am, G7, etc.)
    Chord,
    /// Barlines
    Barline,
    /// Clef symbols
    Clef,
    /// Ornaments (trill, mordent, etc.)
    Ornament,
    /// Special notation symbols
    Special,
    /// Measure numbers
    Measure,
    /// Lyrics
    Lyric,
    /// Unknown tokens
    Unknown,
}

/// Music token with metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MusicToken {
    /// Token text
    pub text: String,
    /// Token type
    pub token_type: MusicTokenType,
    /// Start position in original text
    pub start: usize,
    /// End position in original text
    pub end: usize,
    /// Musical metadata
    pub metadata: Option<MusicTokenMetadata>,
}

/// Metadata for music tokens
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MusicTokenMetadata {
    /// MIDI note number (0-127)
    pub midi_note: Option<u8>,
    /// Duration in ticks
    pub duration_ticks: Option<u32>,
    /// Velocity (dynamics)
    pub velocity: Option<u8>,
    /// Pitch class (0-11)
    pub pitch_class: Option<u8>,
    /// Octave number
    pub octave: Option<i8>,
    /// Beat position
    pub beat_position: Option<f64>,
    /// Measure number
    pub measure: Option<u32>,
    /// Voice/channel number
    pub voice: Option<u8>,
}

/// Music tokenizer implementation
pub struct MusicTokenizer {
    config: MusicTokenizerConfig,
    vocab: HashMap<String, u32>,
    id_to_token: HashMap<u32, String>,
    next_id: u32,
    note_names: HashMap<String, u8>,
    chord_patterns: Vec<Regex>,
    abc_patterns: Vec<Regex>,
    dynamics: HashMap<String, u8>,
}

// Static data for musical notes
static NOTE_NAMES: Lazy<HashMap<String, u8>> = Lazy::new(|| {
    let mut map = HashMap::new();
    // Chromatic notes with MIDI numbers (C4 = 60)
    map.insert("C".to_string(), 0);
    map.insert("C#".to_string(), 1);
    map.insert("Db".to_string(), 1);
    map.insert("D".to_string(), 2);
    map.insert("D#".to_string(), 3);
    map.insert("Eb".to_string(), 3);
    map.insert("E".to_string(), 4);
    map.insert("F".to_string(), 5);
    map.insert("F#".to_string(), 6);
    map.insert("Gb".to_string(), 6);
    map.insert("G".to_string(), 7);
    map.insert("G#".to_string(), 8);
    map.insert("Ab".to_string(), 8);
    map.insert("A".to_string(), 9);
    map.insert("A#".to_string(), 10);
    map.insert("Bb".to_string(), 10);
    map.insert("B".to_string(), 11);
    map
});

// Dynamic markings with velocity values
static DYNAMICS: Lazy<HashMap<String, u8>> = Lazy::new(|| {
    let mut map = HashMap::new();
    map.insert("ppp".to_string(), 16);
    map.insert("pp".to_string(), 32);
    map.insert("p".to_string(), 48);
    map.insert("mp".to_string(), 64);
    map.insert("mf".to_string(), 80);
    map.insert("f".to_string(), 96);
    map.insert("ff".to_string(), 112);
    map.insert("fff".to_string(), 127);
    map
});

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

impl MusicTokenizer {
    /// Create a new music tokenizer
    pub fn new() -> Self {
        Self::with_config(MusicTokenizerConfig::default())
    }

    /// Create with custom configuration
    pub fn with_config(config: MusicTokenizerConfig) -> Self {
        let mut tokenizer = Self {
            config,
            vocab: HashMap::new(),
            id_to_token: HashMap::new(),
            next_id: 0,
            note_names: NOTE_NAMES.clone(),
            chord_patterns: Self::create_chord_patterns(),
            abc_patterns: Self::create_abc_patterns(),
            dynamics: DYNAMICS.clone(),
        };

        tokenizer.initialize_vocab();
        tokenizer
    }

    /// Initialize vocabulary with music tokens
    fn initialize_vocab(&mut self) {
        // Add special tokens
        if self.config.include_special_tokens {
            self.add_token("[CLS]");
            self.add_token("[SEP]");
            self.add_token("[PAD]");
            self.add_token("[UNK]");
            self.add_token("[MASK]");
            self.add_token("[START_MUSIC]");
            self.add_token("[END_MUSIC]");
            self.add_token("[BAR]");
            self.add_token("[BEAT]");
            self.add_token("[MEASURE]");
        }

        // Add note names
        let note_names: Vec<String> = self.note_names.keys().cloned().collect();
        for note in note_names {
            self.add_token(&note);
        }

        // Add accidentals
        self.add_token("#");
        self.add_token("b");
        self.add_token("♯");
        self.add_token("â™­");
        self.add_token("â™®"); // Natural

        // Add octave numbers
        for octave in 0..9 {
            self.add_token(&octave.to_string());
        }

        // Add duration symbols (ABC notation)
        if self.config.tokenize_abc {
            self.add_token("1"); // Whole note
            self.add_token("2"); // Half note
            self.add_token("4"); // Quarter note
            self.add_token("8"); // Eighth note
            self.add_token("16"); // Sixteenth note
            self.add_token("32"); // Thirty-second note
            self.add_token("/"); // Division symbol
            self.add_token("."); // Dotted rhythm
            self.add_token("z"); // Rest
        }

        // Add dynamics
        if self.config.preserve_dynamics {
            let dynamics: Vec<String> = self.dynamics.keys().cloned().collect();
            for dynamic in dynamics {
                self.add_token(&dynamic);
            }
        }

        // Add time signatures
        self.add_token("4/4");
        self.add_token("3/4");
        self.add_token("2/4");
        self.add_token("6/8");
        self.add_token("9/8");
        self.add_token("12/8");

        // Add key signatures
        let keys = [
            "C", "G", "D", "A", "E", "B", "F#", "C#", "F", "Bb", "Eb", "Ab", "Db", "Gb", "Cb",
        ];
        for key in &keys {
            self.add_token(&format!("{}maj", key));
            self.add_token(&format!("{}min", key));
        }

        // Add clefs
        self.add_token("treble");
        self.add_token("bass");
        self.add_token("alto");
        self.add_token("tenor");

        // Add articulations
        self.add_token("staccato");
        self.add_token("legato");
        self.add_token("accent");
        self.add_token("tenuto");

        // Add ornaments
        self.add_token("trill");
        self.add_token("mordent");
        self.add_token("turn");
        self.add_token("appoggiatura");

        // Add chord symbols if enabled
        if self.config.tokenize_chords {
            let chord_types = ["", "m", "7", "M7", "m7", "dim", "aug", "sus2", "sus4"];
            for note in &["C", "D", "E", "F", "G", "A", "B"] {
                for chord_type in &chord_types {
                    self.add_token(&format!("{}{}", note, chord_type));
                }
            }
        }
    }

    /// Add token to vocabulary
    fn add_token(&mut self, token: &str) -> u32 {
        if let Some(&id) = self.vocab.get(token) {
            return id;
        }

        let id = self.next_id;
        self.vocab.insert(token.to_string(), id);
        self.id_to_token.insert(id, token.to_string());
        self.next_id += 1;
        id
    }

    /// Create chord recognition patterns
    fn create_chord_patterns() -> Vec<Regex> {
        vec![
            // Basic triads and sevenths
            Regex::new(r"[A-G][#b]?(m|maj|M|min|dim|aug|sus[24])?[67]?").expect("valid regex"),
            // Complex chord extensions
            Regex::new(r"[A-G][#b]?(add|sus|maj|min|dim|aug)\d+").expect("valid regex"),
            // Slash chords
            Regex::new(r"[A-G][#b]?[^/]*/[A-G][#b]?").expect("valid regex"),
        ]
    }

    /// Create ABC notation patterns
    fn create_abc_patterns() -> Vec<Regex> {
        vec![
            // ABC notes with accidentals and octaves
            Regex::new(r"[_=^]*[A-Ga-g][',]*").expect("valid regex"),
            // Duration modifiers
            Regex::new(r"\d*/?\.?").expect("valid regex"),
            // Rests
            Regex::new(r"z\d*/?\.?").expect("valid regex"),
            // Barlines
            Regex::new(r"\|[\|:\]]*").expect("valid regex"),
            // Chords (bracketed notes)
            Regex::new(r"\[([A-Ga-g][',]*\d*/?\.?)+\]").expect("valid regex"),
            // Slurs and ties
            Regex::new(r"[()~-]").expect("valid regex"),
        ]
    }

    /// Tokenize music notation
    pub fn tokenize_music(&self, text: &str) -> Result<Vec<MusicToken>> {
        let mut tokens = Vec::new();
        let mut pos = 0;

        // Detect music notation type
        if self.is_abc_notation(text) && self.config.tokenize_abc {
            tokens.extend(self.tokenize_abc(text, &mut pos)?);
        } else if self.is_chord_progression(text) && self.config.tokenize_chords {
            tokens.extend(self.tokenize_chords(text, &mut pos)?);
        } else if text.contains("<note>") && self.config.tokenize_musicxml {
            tokens.extend(self.tokenize_musicxml(text, &mut pos)?);
        } else {
            // Fallback tokenization
            tokens.extend(self.tokenize_fallback(text, &mut pos)?);
        }

        Ok(tokens)
    }

    /// Check if text is ABC notation
    fn is_abc_notation(&self, text: &str) -> bool {
        // ABC notation contains note letters with optional accidentals, barlines, rests
        // Accept if it has note letters and either has ABC-specific chars or is primarily notes
        let has_notes = text.chars().any(|c| "ABCDEFGabcdefg".contains(c));
        let has_abc_chars = text.chars().any(|c| "|zx'.^_=(),".contains(c));
        let note_ratio = text.chars().filter(|c| "ABCDEFGabcdefg".contains(*c)).count() as f64
            / text.len() as f64;

        has_notes && (has_abc_chars || note_ratio > 0.5)
    }

    /// Check if text is chord progression
    fn is_chord_progression(&self, text: &str) -> bool {
        self.chord_patterns.iter().any(|pattern| pattern.is_match(text))
    }

    /// Tokenize ABC notation
    fn tokenize_abc(&self, text: &str, pos: &mut usize) -> Result<Vec<MusicToken>> {
        let mut tokens = Vec::new();
        let mut current_pos = *pos;

        for pattern in &self.abc_patterns {
            for mat in pattern.find_iter(text) {
                if mat.start() >= current_pos {
                    let token_text = mat.as_str().to_string();
                    let token_type = self.classify_abc_token(&token_text);
                    let metadata = self.create_music_metadata(&token_text, &token_type);

                    tokens.push(MusicToken {
                        text: token_text,
                        token_type,
                        start: mat.start(),
                        end: mat.end(),
                        metadata,
                    });

                    current_pos = mat.end();
                }
            }
        }

        *pos = current_pos;
        Ok(tokens)
    }

    /// Tokenize chord progressions
    fn tokenize_chords(&self, text: &str, pos: &mut usize) -> Result<Vec<MusicToken>> {
        let mut tokens = Vec::new();
        let mut current_pos = *pos;

        for pattern in &self.chord_patterns {
            for mat in pattern.find_iter(text) {
                if mat.start() >= current_pos {
                    let token_text = mat.as_str().to_string();
                    let token_type = MusicTokenType::Chord;
                    let metadata = self.create_chord_metadata(&token_text);

                    tokens.push(MusicToken {
                        text: token_text,
                        token_type,
                        start: mat.start(),
                        end: mat.end(),
                        metadata,
                    });

                    current_pos = mat.end();
                }
            }
        }

        *pos = current_pos;
        Ok(tokens)
    }

    /// Tokenize MusicXML (simplified)
    fn tokenize_musicxml(&self, text: &str, pos: &mut usize) -> Result<Vec<MusicToken>> {
        let mut tokens = Vec::new();

        // Simple XML tag extraction
        let tag_regex = Regex::new(r"<([^>]+)>([^<]*)</[^>]+>").expect("valid regex");

        for mat in tag_regex.find_iter(text) {
            let token_text = mat.as_str().to_string();
            let token_type = MusicTokenType::Special;

            tokens.push(MusicToken {
                text: token_text,
                token_type,
                start: mat.start(),
                end: mat.end(),
                metadata: None,
            });
        }

        *pos += text.len();
        Ok(tokens)
    }

    /// Fallback tokenization
    fn tokenize_fallback(&self, text: &str, pos: &mut usize) -> Result<Vec<MusicToken>> {
        let mut tokens = Vec::new();

        for (i, ch) in text.char_indices() {
            tokens.push(MusicToken {
                text: ch.to_string(),
                token_type: MusicTokenType::Unknown,
                start: *pos + i,
                end: *pos + i + ch.len_utf8(),
                metadata: None,
            });
        }

        *pos += text.len();
        Ok(tokens)
    }

    /// Classify ABC token type
    fn classify_abc_token(&self, token: &str) -> MusicTokenType {
        if token.starts_with('z') {
            MusicTokenType::Rest
        } else if token.starts_with('|') {
            MusicTokenType::Barline
        } else if token.chars().any(|c| "ABCDEFGabcdefg".contains(c)) {
            MusicTokenType::NoteName
        } else if token.chars().all(|c| c.is_ascii_digit() || "/".contains(c))
            || token.contains('.')
        {
            MusicTokenType::Duration
        } else {
            MusicTokenType::Unknown
        }
    }

    /// Create music metadata
    fn create_music_metadata(
        &self,
        token: &str,
        token_type: &MusicTokenType,
    ) -> Option<MusicTokenMetadata> {
        match token_type {
            MusicTokenType::NoteName => self.parse_note_metadata(token),
            MusicTokenType::Duration => self.parse_duration_metadata(token),
            MusicTokenType::Dynamic => {
                if let Some(&velocity) = self.dynamics.get(token) {
                    Some(MusicTokenMetadata {
                        midi_note: None,
                        duration_ticks: None,
                        velocity: Some(velocity),
                        pitch_class: None,
                        octave: None,
                        beat_position: None,
                        measure: None,
                        voice: None,
                    })
                } else {
                    None
                }
            },
            _ => None,
        }
    }

    /// Parse note metadata from ABC notation
    fn parse_note_metadata(&self, token: &str) -> Option<MusicTokenMetadata> {
        let mut chars = token.chars().peekable();
        let mut accidental = 0i8; // -1 for flat, 0 for natural, 1 for sharp
        let mut note_char = None;
        let mut octave = 4i8; // Default octave

        // Parse accidentals
        while let Some(&ch) = chars.peek() {
            match ch {
                '_' => {
                    accidental -= 1;
                    chars.next();
                },
                '^' => {
                    accidental += 1;
                    chars.next();
                },
                '=' => {
                    accidental = 0;
                    chars.next();
                },
                _ => break,
            }
        }

        // Parse note
        if let Some(ch) = chars.next() {
            if "ABCDEFGabcdefg".contains(ch) {
                note_char = Some(ch);
                // Lowercase notes are in higher octave
                if ch.is_lowercase() {
                    octave += 1;
                }
            }
        }

        // Parse octave modifiers
        while let Some(&ch) = chars.peek() {
            match ch {
                '\'' => {
                    octave += 1;
                    chars.next();
                },
                ',' => {
                    octave -= 1;
                    chars.next();
                },
                _ => break,
            }
        }

        if let Some(note) = note_char {
            let note_upper = note.to_ascii_uppercase();
            if let Some(&base_pitch) = self.note_names.get(&note_upper.to_string()) {
                let pitch_class = (base_pitch as i8 + accidental).rem_euclid(12) as u8;
                let midi_note = (octave * 12 + pitch_class as i8) as u8;

                Some(MusicTokenMetadata {
                    midi_note: Some(midi_note),
                    duration_ticks: None,
                    velocity: None,
                    pitch_class: Some(pitch_class),
                    octave: Some(octave),
                    beat_position: None,
                    measure: None,
                    voice: None,
                })
            } else {
                None
            }
        } else {
            None
        }
    }

    /// Parse duration metadata
    fn parse_duration_metadata(&self, token: &str) -> Option<MusicTokenMetadata> {
        // Simple duration parsing for ABC notation
        let base_duration = self.config.time_resolution; // Quarter note duration

        let duration_ticks = if token.contains('/') {
            // Fractional duration
            if let Some(slash_pos) = token.find('/') {
                let numerator = token[..slash_pos].parse::<u32>().unwrap_or(1);
                let denominator = token[slash_pos + 1..].parse::<u32>().unwrap_or(2);
                base_duration * numerator / denominator
            } else {
                base_duration / 2
            }
        } else if let Ok(multiplier) = token.parse::<u32>() {
            base_duration * multiplier
        } else {
            base_duration
        };

        Some(MusicTokenMetadata {
            midi_note: None,
            duration_ticks: Some(duration_ticks),
            velocity: None,
            pitch_class: None,
            octave: None,
            beat_position: None,
            measure: None,
            voice: None,
        })
    }

    /// Create chord metadata
    fn create_chord_metadata(&self, chord: &str) -> Option<MusicTokenMetadata> {
        // Extract root note from chord symbol
        if let Some(root_char) = chord.chars().next() {
            if "ABCDEFG".contains(root_char) {
                let root_str = root_char.to_string();
                if let Some(&pitch_class) = self.note_names.get(&root_str) {
                    return Some(MusicTokenMetadata {
                        midi_note: Some(60 + pitch_class), // C4 + pitch class
                        duration_ticks: None,
                        velocity: None,
                        pitch_class: Some(pitch_class),
                        octave: Some(4),
                        beat_position: None,
                        measure: None,
                        voice: None,
                    });
                }
            }
        }
        None
    }

    /// Get vocabulary
    pub fn get_vocab(&self) -> &HashMap<String, u32> {
        &self.vocab
    }

    /// Get token by ID
    pub fn id_to_token(&self, id: u32) -> Option<&String> {
        self.id_to_token.get(&id)
    }

    /// Get configuration
    pub fn config(&self) -> &MusicTokenizerConfig {
        &self.config
    }
}

impl Tokenizer for MusicTokenizer {
    fn encode(&self, text: &str) -> Result<TokenizedInput> {
        let music_tokens = self.tokenize_music(text)?;
        let mut input_ids = Vec::new();

        for token in music_tokens {
            if let Some(&id) = self.vocab.get(&token.text) {
                input_ids.push(id);
            } else {
                // Use UNK token
                if let Some(&unk_id) = self.vocab.get("[UNK]") {
                    input_ids.push(unk_id);
                } else {
                    input_ids.push(0); // Fallback
                }
            }
        }

        // Apply max length constraint
        if let Some(max_len) = self.config.max_length {
            input_ids.truncate(max_len);
        }

        Ok(TokenizedInput {
            input_ids: input_ids.clone(),
            attention_mask: vec![1; input_ids.len()],
            token_type_ids: None,
            special_tokens_mask: None,
            offset_mapping: None,
            overflowing_tokens: None,
        })
    }

    fn decode(&self, token_ids: &[u32]) -> Result<String> {
        let mut result = String::new();

        for &id in token_ids {
            if let Some(token) = self.id_to_token.get(&id) {
                if !token.starts_with('[') || !token.ends_with(']') {
                    result.push_str(token);
                }
            }
        }

        Ok(result)
    }

    fn encode_pair(&self, text_a: &str, text_b: &str) -> Result<TokenizedInput> {
        let mut tokenized_a = self.encode(text_a)?;
        let tokenized_b = self.encode(text_b)?;

        // Add separator token if available
        if let Some(&sep_id) = self.vocab.get("[SEP]") {
            tokenized_a.input_ids.push(sep_id);
        }

        tokenized_a.input_ids.extend(tokenized_b.input_ids);

        // Apply max length constraint
        if let Some(max_len) = self.config.max_length {
            tokenized_a.input_ids.truncate(max_len);
        }

        Ok(tokenized_a)
    }

    fn vocab_size(&self) -> usize {
        self.vocab.len()
    }

    fn get_vocab(&self) -> HashMap<String, u32> {
        self.vocab.clone()
    }

    fn token_to_id(&self, token: &str) -> Option<u32> {
        self.vocab.get(token).copied()
    }

    fn id_to_token(&self, id: u32) -> Option<String> {
        self.id_to_token.get(&id).cloned()
    }
}

/// Music analysis results
pub struct MusicAnalysis {
    /// Token type distribution
    pub token_types: HashMap<MusicTokenType, usize>,
    /// Note distribution
    pub note_distribution: HashMap<String, usize>,
    /// Chord progression
    pub chord_progression: Vec<String>,
    /// Key signature (estimated)
    pub estimated_key: Option<String>,
    /// Time signature
    pub time_signature: Option<String>,
    /// Average tempo (if detectable)
    pub estimated_tempo: Option<f64>,
    /// Complexity score
    pub complexity_score: f64,
}

impl MusicTokenizer {
    /// Analyze musical content
    pub fn analyze(&self, text: &str) -> Result<MusicAnalysis> {
        let tokens = self.tokenize_music(text)?;

        let mut token_types = HashMap::new();
        let mut note_distribution = HashMap::new();
        let mut chord_progression = Vec::new();

        for token in &tokens {
            *token_types.entry(token.token_type.clone()).or_insert(0) += 1;

            match &token.token_type {
                MusicTokenType::NoteName => {
                    *note_distribution.entry(token.text.clone()).or_insert(0) += 1;
                },
                MusicTokenType::Chord => {
                    chord_progression.push(token.text.clone());
                },
                _ => {},
            }
        }

        let complexity_score = self.calculate_music_complexity(&tokens);
        let estimated_key = self.estimate_key(&note_distribution);

        Ok(MusicAnalysis {
            token_types,
            note_distribution,
            chord_progression,
            estimated_key,
            time_signature: None,  // Would need more sophisticated analysis
            estimated_tempo: None, // Would need timing information
            complexity_score,
        })
    }

    /// Calculate musical complexity score
    fn calculate_music_complexity(&self, tokens: &[MusicToken]) -> f64 {
        let mut score = 0.0;

        // Base score from number of tokens
        score += tokens.len() as f64 * 0.1;

        // Additional score for different token types
        let mut token_type_count = HashMap::new();
        for token in tokens {
            *token_type_count.entry(&token.token_type).or_insert(0) += 1;
        }

        score += token_type_count.len() as f64 * 0.5;

        // Bonus for musical elements
        for token in tokens {
            match token.token_type {
                MusicTokenType::Chord => score += 1.0,
                MusicTokenType::Ornament => score += 1.5,
                MusicTokenType::Dynamic => score += 0.5,
                MusicTokenType::Articulation => score += 0.3,
                _ => {},
            }
        }

        score
    }

    /// Estimate key signature from note distribution
    fn estimate_key(&self, note_distribution: &HashMap<String, usize>) -> Option<String> {
        // Simple key estimation based on most frequent notes
        if let Some((most_frequent_note, _)) =
            note_distribution.iter().max_by_key(|(_, &count)| count)
        {
            Some(format!(
                "{}maj",
                most_frequent_note.chars().next().unwrap_or('C')
            ))
        } else {
            None
        }
    }
}

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

    #[test]
    fn test_music_tokenizer_creation() {
        let tokenizer = MusicTokenizer::new();
        assert!(tokenizer.get_vocab().len() > 0);
        assert!(tokenizer.get_vocab().contains_key("C"));
        assert!(tokenizer.get_vocab().contains_key("G"));
    }

    #[test]
    fn test_abc_detection() {
        let tokenizer = MusicTokenizer::new();
        assert!(tokenizer.is_abc_notation("CDEFGAB"));
        assert!(tokenizer.is_abc_notation("C4 D4 E4 | F4 G4 A4 B4"));
        assert!(!tokenizer.is_abc_notation("hello world"));
    }

    #[test]
    fn test_chord_detection() {
        let tokenizer = MusicTokenizer::new();
        assert!(tokenizer.is_chord_progression("C Am F G"));
        assert!(tokenizer.is_chord_progression("Cmaj7 Dm7 G7"));
        assert!(!tokenizer.is_chord_progression("hello world"));
    }

    #[test]
    fn test_abc_encoding() {
        let tokenizer = MusicTokenizer::new();
        let result = tokenizer.encode("CDEFGAB");
        assert!(result.is_ok());
        let tokenized = result.expect("Operation failed in test");
        assert!(!tokenized.input_ids.is_empty());
    }

    #[test]
    fn test_chord_encoding() {
        let tokenizer = MusicTokenizer::new();
        let result = tokenizer.encode("C Am F G");
        assert!(result.is_ok());
        let tokenized = result.expect("Operation failed in test");
        assert!(!tokenized.input_ids.is_empty());
    }

    #[test]
    fn test_note_metadata_parsing() {
        let tokenizer = MusicTokenizer::new();
        let metadata = tokenizer.parse_note_metadata("C");
        assert!(metadata.is_some());
        let meta = metadata.expect("Operation failed in test");
        assert_eq!(meta.pitch_class, Some(0)); // C = 0
        assert_eq!(meta.octave, Some(4));
    }

    #[test]
    fn test_accidental_parsing() {
        let tokenizer = MusicTokenizer::new();
        let metadata = tokenizer.parse_note_metadata("^C");
        assert!(metadata.is_some());
        let meta = metadata.expect("Operation failed in test");
        assert_eq!(meta.pitch_class, Some(1)); // C# = 1
    }

    #[test]
    fn test_duration_parsing() {
        let tokenizer = MusicTokenizer::new();
        let metadata = tokenizer.parse_duration_metadata("4");
        assert!(metadata.is_some());
        let meta = metadata.expect("Operation failed in test");
        assert!(meta.duration_ticks.is_some());
    }

    #[test]
    fn test_music_analysis() {
        let tokenizer = MusicTokenizer::new();
        let analysis = tokenizer.analyze("CDEFGAB");
        assert!(analysis.is_ok());
        let result = analysis.expect("Operation failed in test");
        assert!(!result.note_distribution.is_empty());
        assert!(result.complexity_score > 0.0);
    }

    #[test]
    fn test_chord_metadata() {
        let tokenizer = MusicTokenizer::new();
        let metadata = tokenizer.create_chord_metadata("Cmaj7");
        assert!(metadata.is_some());
        let meta = metadata.expect("Operation failed in test");
        assert_eq!(meta.pitch_class, Some(0)); // C = 0
    }

    #[test]
    fn test_encoding_decoding_consistency() {
        let tokenizer = MusicTokenizer::new();
        let original = "CDEFG";
        let encoded = tokenizer.encode(original).expect("Encoding failed");
        let decoded = tokenizer.decode(&encoded.input_ids).expect("Decoding failed");
        assert!(!decoded.is_empty());
    }

    #[test]
    fn test_max_length_constraint() {
        let mut config = MusicTokenizerConfig::default();
        config.max_length = Some(5);
        let tokenizer = MusicTokenizer::with_config(config);

        let result = tokenizer.encode("CDEFGABCDEFGAB");
        assert!(result.is_ok());
        let tokenized = result.expect("Operation failed in test");
        assert!(tokenized.input_ids.len() <= 5);
    }

    #[test]
    fn test_dynamic_recognition() {
        let tokenizer = MusicTokenizer::new();
        assert!(tokenizer.get_vocab().contains_key("ff"));
        assert!(tokenizer.get_vocab().contains_key("pp"));

        if let Some(&velocity) = tokenizer.dynamics.get("ff") {
            assert!(velocity > 100);
        }
    }
}