lipilekhika 1.0.2

A transliteration library for Indian Brahmic scripts
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
use std::collections::HashMap;
use std::time::{Duration, Instant};

use crate::script_data::{List, ScriptData, get_normalized_script_name};
use crate::transliterate::transliterate::{
  TransliterationFnOptions, resolve_transliteration_rules, transliterate_text_core,
};

/// Default time in milliseconds after which the context will be cleared automatically.
pub const DEFAULT_AUTO_CONTEXT_CLEAR_TIME_MS: u64 = 4500;
/// Default value for using native numerals while typing.
pub const DEFAULT_USE_NATIVE_NUMERALS: bool = true;
/// Default value for including inherent vowels while typing.
/// By default avoids schwa deletion.
pub const DEFAULT_INCLUDE_INHERENT_VOWEL: bool = false;

/// Options for configuring a typing context.
#[derive(Debug, Clone)]
pub struct TypingContextOptions {
  /// The time in milliseconds after which the context will be cleared automatically.
  /// Defaults to [`DEFAULT_AUTO_CONTEXT_CLEAR_TIME_MS`].
  pub auto_context_clear_time_ms: u64,
  /// Use native numerals in transliteration/typing.
  /// Defaults to `DEFAULT_USE_NATIVE_NUMERALS`
  pub use_native_numerals: bool,
  /// Include inherent vowels (schwa character) in transliteration/typing.
  ///
  /// - `true`: `k` -> `क` (Eg. Hindi, Bengali, Gujarati, etc.)
  /// - `false`: `k` -> `क्` (Default behavior in transliteration. Eg. Sanskrit, Telugu, Tamil, Kannada, etc.)
  ///
  /// Defaults to `DEFAULT_INCLUDE_INHERENT_VOWEL`
  pub include_inherent_vowel: bool,
}

impl Default for TypingContextOptions {
  fn default() -> Self {
    Self {
      auto_context_clear_time_ms: DEFAULT_AUTO_CONTEXT_CLEAR_TIME_MS,
      use_native_numerals: DEFAULT_USE_NATIVE_NUMERALS,
      include_inherent_vowel: DEFAULT_INCLUDE_INHERENT_VOWEL,
    }
  }
}

/// Result of processing a single key in a typing context.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TypingDiff {
  /// Number of characters that should be deleted from the current "app" input state.
  pub to_delete_chars_count: usize,
  /// Text that should be inserted into the current "app" input state.
  pub diff_add_text: String,
}

/// Stateful isolated context for character-by-character input typing.
///
/// synchronous and uses Rust's internal script data cache.
#[derive(Debug)]
pub struct TypingContext {
  normalized_typing_lang: String,

  use_native_numerals: bool,
  include_inherent_vowel: bool,

  curr_input: String,
  curr_output: String,

  auto_context_clear_time: Duration,
  last_time: Option<Instant>,

  from_script_data: &'static ScriptData,
  to_script_data: &'static ScriptData,
  trans_options: HashMap<String, bool>,
  custom_rules: Vec<crate::script_data::Rule>,
}

impl TypingContext {
  /// Creates a new typing context for the given script/language.
  ///
  /// - `typing_lang` can be a script or language name/alias (normalized via `get_normalized_script_name`).
  /// - `options` configures timing and inherent vowel / numeral behavior.
  pub fn new(typing_lang: &str, options: Option<TypingContextOptions>) -> Result<Self, String> {
    let opts = options.unwrap_or_default();

    let normalized_typing_lang = get_normalized_script_name(typing_lang)
      .ok_or_else(|| format!("Invalid script name: {}", typing_lang))?;

    let from_script_data = ScriptData::get_script_data("Normal");
    let to_script_data = ScriptData::get_script_data(&normalized_typing_lang);

    let resolved = resolve_transliteration_rules(from_script_data, to_script_data, None);

    Ok(Self {
      normalized_typing_lang,
      use_native_numerals: opts.use_native_numerals,
      include_inherent_vowel: opts.include_inherent_vowel,
      curr_input: String::new(),
      curr_output: String::new(),
      auto_context_clear_time: Duration::from_millis(opts.auto_context_clear_time_ms),
      last_time: None,
      from_script_data,
      to_script_data,
      trans_options: resolved.trans_options,
      custom_rules: resolved.custom_rules,
    })
  }

  /// Clears all internal state and contexts.
  pub fn clear_context(&mut self) {
    self.last_time = None;
    self.curr_input.clear();
    self.curr_output.clear();
  }

  /// Internal helper to build transliteration options for typing mode.
  fn build_translit_options(&self) -> TransliterationFnOptions {
    TransliterationFnOptions {
      typing_mode: true,
      use_native_numerals: self.use_native_numerals,
      include_inherent_vowel: self.include_inherent_vowel,
    }
  }

  /// Accepts character-by-character input and returns the diff relative to the previous output.
  pub fn take_key_input(&mut self, key: &str) -> Result<TypingDiff, String> {
    // If key is empty, nothing to do.
    let Some(ch) = key.chars().next() else {
      return Ok(TypingDiff {
        to_delete_chars_count: 0,
        diff_add_text: String::new(),
      });
    };

    let now = Instant::now();
    if let Some(last) = self.last_time {
      if now.duration_since(last) > self.auto_context_clear_time {
        self.clear_context();
      }
    }

    self.curr_input.push(ch);
    let prev_output = self.curr_output.clone();

    let result = transliterate_text_core(
      self.curr_input.clone(),
      "Normal",
      &self.normalized_typing_lang,
      self.from_script_data,
      self.to_script_data,
      &self.trans_options,
      &self.custom_rules,
      Some(self.build_translit_options()),
    )?;

    let context_length = result.context_length;
    let output = result.output;

    if context_length > 0 {
      self.curr_output = output.clone();
    } else if context_length == 0 {
      self.clear_context();
    }

    // Calculate the diff between previous and current output, by common prefix length.
    let (to_delete_chars_count, diff_add_text) = compute_diff(&prev_output, &output);

    self.last_time = Some(Instant::now());

    Ok(TypingDiff {
      to_delete_chars_count,
      diff_add_text,
    })
  }

  /// Updates whether native numerals should be used for subsequent typing.
  pub fn update_use_native_numerals(&mut self, use_native_numerals: bool) {
    self.use_native_numerals = use_native_numerals;
  }

  /// Updates whether inherent vowels should be included for subsequent typing.
  pub fn update_include_inherent_vowel(&mut self, include_inherent_vowel: bool) {
    self.include_inherent_vowel = include_inherent_vowel;
  }
}

pub fn create_typing_context(
  typing_lang: &str,
  options: Option<TypingContextOptions>,
) -> Result<TypingContext, String> {
  TypingContext::new(typing_lang, options)
}

/// Compute the character-wise diff between previous and current outputs.
///
/// Returns `(to_delete_chars_count, diff_add_text)`.
fn compute_diff(prev_output: &str, output: &str) -> (usize, String) {
  let mut common_chars = 0usize;

  for (a, b) in prev_output.chars().zip(output.chars()) {
    if a != b {
      break;
    }
    common_chars += 1;
  }

  let to_delete_chars_count = prev_output.chars().count().saturating_sub(common_chars);
  let diff_add_text: String = output.chars().skip(common_chars).collect();

  (to_delete_chars_count, diff_add_text)
}

/// Helper used in tests to emulate per-key typing and accumulate the final output.
///
pub fn emulate_typing(
  text: &str,
  typing_lang: &str,
  options: Option<TypingContextOptions>,
) -> Result<String, String> {
  let mut ctx = create_typing_context(typing_lang, options)?;
  let mut result = String::new();

  for ch in text.chars() {
    let diff = ctx.take_key_input(&ch.to_string())?;

    if diff.to_delete_chars_count > 0 {
      truncate_last_chars(&mut result, diff.to_delete_chars_count);
    }

    result.push_str(&diff.diff_add_text);
  }

  Ok(result)
}

/// Truncate the last `n` characters from a UTF-8 string (character-wise, not bytes).
fn truncate_last_chars(s: &mut String, n: usize) {
  for _ in 0..n {
    if let Some((idx, _)) = s.char_indices().rev().next() {
      s.truncate(idx);
    } else {
      break;
    }
  }
}

/// Type of a character in a script's list.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ListType {
  Anya,
  Vyanjana,
  Matra,
  Svara,
}

impl ListType {
  /// Converts a List variant to its ListType.
  fn from_list(list: &List) -> Self {
    match list {
      List::Anya { .. } => ListType::Anya,
      List::Vyanjana { .. } => ListType::Vyanjana,
      List::Matra { .. } => ListType::Matra,
      List::Svara { .. } => ListType::Svara,
    }
  }
}

/// An item in the typing data map containing the text, its type, and associated input mappings.
pub type TypingDataMapItem = (String, ListType, Vec<String>);

/// Result of [`get_script_typing_data_map`] containing typing data for a script.
#[derive(Debug)]
pub struct ScriptTypingDataMap {
  /// Mappings for common characters across scripts (from krama_text_arr).
  pub common_krama_map: Vec<TypingDataMapItem>,
  /// Mappings for script-specific characters (from custom_script_chars_arr).
  /// Duplicate key mappings are handled in the common_krama_map.
  pub script_specific_krama_map: Vec<TypingDataMapItem>,
}

/// Returns the typing data map for a script.
///
/// - `script` - The script/language name to get the typing data map for.
///
/// Returns the typing data map for the script, containing mappings for
/// both common characters and script-specific characters.
///
/// Returns an error if the script name is invalid or is 'Normal' (English).
pub fn get_script_typing_data_map(script: &str) -> Result<ScriptTypingDataMap, String> {
  let normalized_typing_lang =
    get_normalized_script_name(script).ok_or_else(|| format!("Invalid script name: {}", script))?;

  if normalized_typing_lang == "Normal" {
    return Err(format!("Invalid script name: {}", script));
  }

  let script_data = ScriptData::get_script_data(&normalized_typing_lang);
  let common_attr = script_data.get_common_attr();

  /// Merges items that end up with the same displayed text (and type),
  /// and keeps mappings unique.
  fn merge_duplicate_text_mappings(items: Vec<TypingDataMapItem>) -> Vec<TypingDataMapItem> {
    use std::collections::{HashMap, HashSet};

    let mut key_to_index: HashMap<(String, ListType), usize> = HashMap::new();
    let mut mapping_sets: Vec<HashSet<String>> = Vec::new();
    let mut out: Vec<TypingDataMapItem> = Vec::new();

    for (text, list_type, mappings) in items {
      let key = (text.clone(), list_type.clone());

      if let Some(&existing_index) = key_to_index.get(&key) {
        // Merge into existing item
        let set = &mut mapping_sets[existing_index];
        let target_mappings = &mut out[existing_index].2;
        for m in mappings {
          if set.insert(m.clone()) {
            target_mappings.push(m);
          }
        }
      } else {
        // New item
        let mut set = HashSet::new();
        let mut uniq = Vec::new();
        for m in mappings {
          if set.insert(m.clone()) {
            uniq.push(m);
          }
        }
        key_to_index.insert(key, out.len());
        out.push((text, list_type, uniq));
        mapping_sets.push(set);
      }
    }

    // Drop items that have no typing mappings
    out
      .into_iter()
      .filter(|(_, _, mappings)| !mappings.is_empty())
      .collect()
  }

  // Initialize common_krama_map from krama_text_arr
  let mut common_krama_map: Vec<TypingDataMapItem> = common_attr
    .krama_text_arr
    .iter()
    .map(|(text, list_index)| {
      let list_type = list_index
        .and_then(|idx| common_attr.list.get(idx as usize))
        .map(ListType::from_list)
        .unwrap_or(ListType::Anya);
      (text.clone(), list_type, Vec::new())
    })
    .collect();

  // Initialize script_specific_krama_map from custom_script_chars_arr
  let mut script_specific_krama_map: Vec<TypingDataMapItem> = common_attr
    .custom_script_chars_arr
    .iter()
    .map(|(text, list_index, _)| {
      let list_type = list_index
        .and_then(|idx| common_attr.list.get(idx as usize))
        .map(ListType::from_list)
        .unwrap_or(ListType::Anya);
      (text.clone(), list_type, Vec::new())
    })
    .collect();

  // Populate mappings from typing_text_to_krama_map
  for (normal_text_map, item) in &common_attr.typing_text_to_krama_map {
    if normal_text_map.is_empty() {
      continue;
    }

    if let Some(custom_back_ref) = item.custom_back_ref {
      // Add to script_specific_krama_map
      if let Some(entry) = script_specific_krama_map.get_mut(custom_back_ref as usize) {
        entry.2.push(normal_text_map.clone());
      }
    } else if let Some(ref krama) = item.krama {
      // Ignore entries with length > 1 (intermediate typing states)
      if krama.len() == 1 {
        let krama_index = krama[0];
        if krama_index >= 0 {
          if let Some(entry) = common_krama_map.get_mut(krama_index as usize) {
            entry.2.push(normal_text_map.clone());
          }
        }
      }
    }
  }

  // Merge duplicates
  common_krama_map = merge_duplicate_text_mappings(common_krama_map);
  script_specific_krama_map = merge_duplicate_text_mappings(script_specific_krama_map);

  Ok(ScriptTypingDataMap {
    common_krama_map,
    script_specific_krama_map,
  })
}

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

  use crate::transliterate::helpers::VEDIC_SVARAS;
  use serde::Deserialize;
  use std::fs;
  use std::fs::OpenOptions;
  use std::io::Write;
  use std::path::{Path, PathBuf};

  /// For transliteration auto tests, `index` can be string or number in YAML.
  fn de_index<'de, D>(deserializer: D) -> Result<String, D::Error>
  where
    D: serde::Deserializer<'de>,
  {
    struct IndexVisitor;

    impl serde::de::Visitor<'_> for IndexVisitor {
      type Value = String;

      fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("a yaml index (number or string)")
      }

      fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
      where
        E: serde::de::Error,
      {
        Ok(v.to_string())
      }

      fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
      where
        E: serde::de::Error,
      {
        Ok(v.to_string())
      }

      fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
      where
        E: serde::de::Error,
      {
        Ok(v.to_string())
      }

      fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
      where
        E: serde::de::Error,
      {
        Ok(v.to_string())
      }

      fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
      where
        E: serde::de::Error,
      {
        Ok(v)
      }
    }

    deserializer.deserialize_any(IndexVisitor)
  }

  /// Schema matching transliteration YAML test data used in emulate-typing tests.
  #[derive(Debug, Deserialize)]
  struct TransliterationTestCase {
    #[serde(deserialize_with = "de_index")]
    #[allow(dead_code)]
    index: String,
    from: String,
    to: String,
    input: String,
    output: String,
    #[serde(default)]
    #[allow(dead_code)]
    options: Option<std::collections::HashMap<String, bool>>,
    #[serde(default)]
    #[allow(dead_code)]
    reversible: Option<bool>,
    #[serde(default)]
    todo: Option<bool>,
  }

  fn transliteration_test_data_root() -> PathBuf {
    // `packages/rust` -> `../../test_data/transliteration`
    let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    manifest_dir
      .join("..")
      .join("..")
      .join("test_data")
      .join("transliteration")
  }

  #[test]
  fn emulate_typing_auto_transliteration_yaml() {
    use serde_yaml_ng as yaml;

    let root = transliteration_test_data_root();
    let input_dirs = [root.join("auto-nor-brahmic"), root.join("auto-nor-other")];

    // Basic reporting counters
    let mut total_emulations: usize = 0;
    let mut auto_vedic_skipped: usize = 0;

    for folder in &input_dirs {
      let entries = fs::read_dir(folder)
        .unwrap_or_else(|e| panic!("Failed listing YAML files in `{}`: {e}", folder.display()));

      for entry in entries {
        let entry = entry.expect("Failed to read directory entry");
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) != Some("yaml") {
          continue;
        }

        let yaml_text = fs::read_to_string(&path)
          .unwrap_or_else(|e| panic!("Failed reading YAML file `{}`: {e}", path.display()));
        let cases: Vec<TransliterationTestCase> = yaml::from_str(&yaml_text)
          .unwrap_or_else(|e| panic!("Failed parsing YAML file `{}`: {e}", path.display()));

        let file_name = path
          .file_name()
          .and_then(|n| n.to_str())
          .unwrap_or("<unknown>")
          .to_string();

        for case in cases {
          if case.todo.unwrap_or(false) {
            continue;
          }
          if case.from != "Normal" || case.to == "Normal" {
            continue;
          }

          let input = &case.input;
          total_emulations += 1;
          let result = emulate_typing(input, &case.to, None)
            .unwrap_or_else(|e| panic!("emulate_typing error for {}: {}", path.display(), e));

          let error_message = format!(
            "Emulate Typing failed:\n  From: {}\n  To: {}\n  Input: \"{}\"\n  Expected: \"{}\"\n  Actual: \"{}\"",
            case.from, case.to, case.input, case.output, result
          );

          if file_name.starts_with("auto")
            && case.to == "Tamil-Extended"
            && VEDIC_SVARAS.iter().any(|sv| result.contains(*sv))
          {
            auto_vedic_skipped += 1;
            continue;
          }

          assert_eq!(result, case.output, "{}", error_message);
        }
      }
    }

    let passed = total_emulations.saturating_sub(auto_vedic_skipped);
    let summary = format!(
      "Emulate Typing (auto transliteration): total_emulations={}, auto_vedic_skipped={}, passed={}",
      total_emulations, auto_vedic_skipped, passed
    );
    println!("{}", summary);

    // Also write summary to a log file so it's visible even when tests succeed.
    let _ = std::fs::create_dir_all("test_log");
    if let Ok(mut file) = OpenOptions::new()
      .create(true)
      .write(true)
      .truncate(true)
      .open("test_log/typing_auto_emulate_log.txt")
    {
      let _ = writeln!(file, "{}", summary);
    }
  }

  /// Typed view of typing options from YAML.
  #[derive(Debug, Deserialize, Default)]
  struct TypingOptionsYaml {
    #[serde(rename = "useNativeNumerals")]
    #[serde(default)]
    use_native_numerals: Option<bool>,

    #[serde(rename = "includeInherentVowel")]
    #[serde(default)]
    include_inherent_vowel: Option<bool>,

    #[serde(rename = "autoContextTClearTimeMs")]
    #[serde(default)]
    auto_context_clear_time_ms: Option<u64>,
  }

  /// Schema for typing-mode YAML tests (`test_data/typing`).
  #[derive(Debug, Deserialize)]
  struct TypingTestCase {
    index: i64,
    text: String,
    output: String,
    script: String,
    #[serde(default)]
    preserve_check: bool,
    #[serde(default)]
    todo: bool,
    #[serde(default)]
    options: Option<TypingOptionsYaml>,
  }

  fn typing_test_data_root() -> PathBuf {
    // `packages/rust` -> `../../test_data/typing`
    let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    manifest_dir
      .join("..")
      .join("..")
      .join("test_data")
      .join("typing")
  }

  fn list_yaml_files_typing(dir: &Path, out: &mut Vec<PathBuf>) -> std::io::Result<()> {
    for entry in fs::read_dir(dir)? {
      let entry = entry?;
      let path = entry.path();
      if path.is_dir() {
        if path
          .file_name()
          .and_then(|n| n.to_str())
          .is_some_and(|n| n == "context")
        {
          continue;
        }
        list_yaml_files_typing(&path, out)?;
      } else if path.extension().and_then(|e| e.to_str()) == Some("yaml") {
        out.push(path);
      }
    }
    Ok(())
  }

  fn build_typing_options(opts: &Option<TypingOptionsYaml>) -> Option<TypingContextOptions> {
    let some_opts = match opts {
      None => return None,
      Some(o) => o,
    };

    let mut rust_opts = TypingContextOptions::default();
    if let Some(v) = some_opts.use_native_numerals {
      rust_opts.use_native_numerals = v;
    }
    if let Some(v) = some_opts.include_inherent_vowel {
      rust_opts.include_inherent_vowel = v;
    }
    if let Some(v) = some_opts.auto_context_clear_time_ms {
      rust_opts.auto_context_clear_time_ms = v;
    }

    Some(rust_opts)
  }

  #[test]
  fn typing_mode_yaml_tests() {
    use serde_yaml_ng as yaml;

    let root = typing_test_data_root();
    let mut files: Vec<PathBuf> = Vec::new();
    list_yaml_files_typing(&root, &mut files)
      .unwrap_or_else(|e| panic!("Failed listing YAML files in `{}`: {e}", root.display()));
    files.sort();

    assert!(
      !files.is_empty(),
      "No YAML typing test files found in `{}`",
      root.display()
    );

    // Basic reporting counters
    let mut total_emulations: usize = 0;
    let mut preserve_checks: usize = 0;

    for file in files {
      let yaml_text = fs::read_to_string(&file)
        .unwrap_or_else(|e| panic!("Failed reading YAML file `{}`: {e}", file.display()));
      let cases: Vec<TypingTestCase> = yaml::from_str(&yaml_text)
        .unwrap_or_else(|e| panic!("Failed parsing YAML file `{}`: {e}", file.display()));

      for case in cases {
        if case.todo {
          continue;
        }

        let opts = build_typing_options(&case.options);
        total_emulations += 1;
        let result = emulate_typing(&case.text, &case.script, opts.clone()).unwrap_or_else(|e| {
          panic!(
            "emulate_typing error in `{}` index {}: {}",
            file.display(),
            case.index,
            e
          )
        });

        assert_eq!(
          result,
          case.output,
          "Typing Mode failed in `{}` index {} (script {}): input {:?}",
          file.display(),
          case.index,
          case.script,
          case.text
        );

        if case.preserve_check {
          preserve_checks += 1;
          // Transliterate back to Normal with `all_to_normal:preserve_specific_chars`
          let mut trans_options = std::collections::HashMap::new();
          trans_options.insert("all_to_normal:preserve_specific_chars".to_string(), true);

          let preserved =
            crate::transliterate(&result, &case.script, "Normal", Some(&trans_options))
              .unwrap_or_else(|e| {
                panic!(
                  "transliterate (preserve check) error in `{}` index {}: {}",
                  file.display(),
                  case.index,
                  e
                )
              });

          assert_eq!(
            preserved,
            case.text,
            "Preserve check failed in `{}` index {} (script {}): input {:?}",
            file.display(),
            case.index,
            case.script,
            case.text
          );
        }
      }
    }

    let summary = format!(
      "Typing Mode: total_emulations={}, preserve_checks={}, passed={}",
      total_emulations, preserve_checks, total_emulations
    );
    println!("{}", summary);

    // Also write summary to a log file so it's visible even when tests succeed.
    let _ = std::fs::create_dir_all("test_log");
    if let Ok(mut file) = OpenOptions::new()
      .create(true)
      .write(true)
      .truncate(true)
      .open("test_log/typing_mode_log.txt")
    {
      let _ = writeln!(file, "{}", summary);
    }
  }

  #[test]
  fn test_get_script_typing_data_map_valid_script() {
    let result = get_script_typing_data_map("Devanagari");
    assert!(result.is_ok());
    let data = result.unwrap();

    // Should have some entries in both maps
    assert!(!data.common_krama_map.is_empty());

    // Verify structure: each item should be (text, type, mappings)
    for (text, _list_type, _mappings) in &data.common_krama_map {
      assert!(!text.is_empty());
    }
  }

  #[test]
  fn test_get_script_typing_data_map_normalized_names() {
    // Test with acronym that should be normalized
    let result = get_script_typing_data_map("dev");
    assert!(result.is_ok());
  }

  #[test]
  fn test_get_script_typing_data_map_invalid_script() {
    let result = get_script_typing_data_map("InvalidScript");
    assert!(result.is_err());
    assert_eq!(result.unwrap_err(), "Invalid script name: InvalidScript");
  }

  #[test]
  fn test_get_script_typing_data_map_normal_script() {
    // Should reject Normal/English
    let result = get_script_typing_data_map("Normal");
    assert!(result.is_err());
    assert_eq!(result.unwrap_err(), "Invalid script name: Normal");
  }

  #[test]
  fn test_get_script_typing_data_map_mappings_populated() {
    let result = get_script_typing_data_map("Telugu");
    assert!(result.is_ok());
    let data = result.unwrap();

    // At least some entries should have mappings populated
    let has_mappings = data
      .common_krama_map
      .iter()
      .any(|(_, _, mappings)| !mappings.is_empty());

    assert!(
      has_mappings,
      "Expected at least some characters to have typing mappings"
    );
  }
}