moine-ja 0.2.2

Japanese kana and romaji lattice adapter for moine
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
use std::collections::BTreeSet;

use moine_core::{
    levenshtein_str, normalized_similarity_from_distance, try_damerau_distance,
    try_damerau_levenshtein_str, try_distance, Lattice, StringDistanceWorkspace,
};

use crate::overrides::OverrideDictionary;
use crate::romaji::{
    can_build_direct_romaji_path, romaji_lattice, romaji_lattice_from_supported_segmented_readings,
    romaji_paths, JaLatticeError, RomajiSegmentMode,
};
use crate::unidic::{
    romaji_paths_from_reading_paths, DictionaryReadingOptions, DictionaryReadingPath,
    DictionaryReadingSegment, DictionaryReadingSegmentSource, UnidicReadingIndex,
};

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
/// Distances computed for one Japanese comparison.
pub struct JapaneseDistance {
    /// Plain Levenshtein distance over the original strings.
    pub surface_levenshtein: usize,
    /// Plain Damerau-Levenshtein distance over the original strings.
    pub surface_damerau: usize,
    /// Lattice Path Edit Distance over romaji reading lattices.
    pub lattice: usize,
    /// Lattice-aware Damerau-Levenshtein distance over reading lattices.
    pub lattice_damerau: usize,
    /// Minimum of surface Damerau-Levenshtein and non-Damerau LPED.
    ///
    /// This intentionally does not include `lattice_damerau`; use that field
    /// directly when lattice-side adjacent transpositions should count as one
    /// edit.
    pub combined: usize,
}

/// Compares two strings using direct kana/romaji handling plus overrides.
pub fn compare_with_overrides(
    left: &str,
    right: &str,
    overrides: &OverrideDictionary,
) -> Result<JapaneseDistance, JaLatticeError> {
    let left_lattice = overrides.romaji_lattice(left)?;
    let right_lattice = overrides.romaji_lattice(right)?;
    compare_lattices(left, right, &left_lattice, &right_lattice)
}

/// Compares two strings using direct handling and a Japanese dictionary index.
pub fn compare_with_unidic_index(
    left: &str,
    right: &str,
    index: &UnidicReadingIndex,
    options: DictionaryReadingOptions,
) -> Result<JapaneseDistance, JaLatticeError> {
    let options = validate_dictionary_options(options)?;
    let left_lattice = unidic_or_direct_lattice(left, index, options)?;
    let right_lattice = unidic_or_direct_lattice(right, index, options)?;
    compare_lattices(left, right, &left_lattice, &right_lattice)
}

/// Computes the best normalized similarity across dictionary-backed readings.
pub fn normalized_similarity_with_unidic_index(
    left: &str,
    right: &str,
    index: &UnidicReadingIndex,
    options: DictionaryReadingOptions,
) -> Result<f64, JaLatticeError> {
    let options = validate_dictionary_options(options)?;
    let left_paths = unidic_or_direct_romaji_paths(left, index, options)?;
    let right_paths = unidic_or_direct_romaji_paths(right, index, options)?;
    Ok(max_normalized_similarity(&left_paths, &right_paths))
}

/// Builds a romaji lattice from direct input, dictionary readings, or both.
pub fn unidic_or_direct_lattice(
    input: &str,
    index: &UnidicReadingIndex,
    options: DictionaryReadingOptions,
) -> Result<Lattice, JaLatticeError> {
    let options = validate_dictionary_options(options)?;
    let contains_ascii = contains_ascii_alphanumeric(input);
    if !contains_ascii {
        if let Ok(lattice) = romaji_lattice(input) {
            return Ok(lattice);
        }
        if let Some(lattice) = index.romaji_lattice(input, options)? {
            return Ok(lattice);
        }
    }

    if contains_ascii && can_build_direct_romaji_path(input) {
        if let Some(lattice) = direct_ascii_dictionary_lattice(input, index, options)? {
            return Ok(lattice);
        }
    }

    let dictionary_paths = index
        .try_reading_paths_with_stats(input, options)
        .map_err(|err| JaLatticeError::ArtifactPayload(err.to_string()))?
        .paths;
    if !dictionary_paths.is_empty() {
        if let Some(lattice) = lattice_from_dictionary_reading_paths(&dictionary_paths)? {
            return Ok(lattice);
        }
    }

    let hybrid_paths = index
        .try_hybrid_reading_paths_with_stats(input, options)
        .map_err(|err| JaLatticeError::ArtifactPayload(err.to_string()))?
        .paths;
    if !hybrid_paths.is_empty() {
        if let Some(lattice) = lattice_from_dictionary_reading_paths(&hybrid_paths)? {
            return Ok(lattice);
        }
    }

    romaji_lattice(input)
}

/// Returns romaji paths from direct input, dictionary readings, or both.
pub fn unidic_or_direct_romaji_paths(
    input: &str,
    index: &UnidicReadingIndex,
    options: DictionaryReadingOptions,
) -> Result<Vec<String>, JaLatticeError> {
    let options = validate_dictionary_options(options)?;
    let mut paths = BTreeSet::new();
    if let Ok(direct_paths) = romaji_paths(input) {
        if !contains_ascii_alphanumeric(input) {
            return Ok(direct_paths);
        }
        paths.extend(direct_paths);
        extend_exact_dictionary_paths(&mut paths, input, index, options)?;
        if should_try_hybrid_for_direct_ascii(input, index)? {
            let hybrid_paths = index
                .try_hybrid_reading_paths_with_stats(input, options)
                .map_err(|err| JaLatticeError::ArtifactPayload(err.to_string()))?
                .paths;
            extend_supported_dictionary_paths(&mut paths, &hybrid_paths)?;
        }
        return Ok(paths.into_iter().collect());
    }

    let dictionary_paths = index
        .try_reading_paths_with_stats(input, options)
        .map_err(|err| JaLatticeError::ArtifactPayload(err.to_string()))?
        .paths;
    let has_dictionary_paths = !dictionary_paths.is_empty();
    if has_dictionary_paths {
        extend_supported_dictionary_paths(&mut paths, &dictionary_paths)?;
    }

    if !has_dictionary_paths || paths.is_empty() {
        let hybrid_paths = index
            .try_hybrid_reading_paths_with_stats(input, options)
            .map_err(|err| JaLatticeError::ArtifactPayload(err.to_string()))?
            .paths;
        if !hybrid_paths.is_empty() {
            extend_supported_dictionary_paths(&mut paths, &hybrid_paths)?;
        }
    }

    if !paths.is_empty() {
        return Ok(paths.into_iter().collect());
    }

    romaji_paths(input)
}

fn validate_dictionary_options(
    options: DictionaryReadingOptions,
) -> Result<DictionaryReadingOptions, JaLatticeError> {
    options
        .validate()
        .map_err(|err| JaLatticeError::ArtifactPayload(err.to_string()))
}

fn contains_ascii_alphanumeric(input: &str) -> bool {
    input.chars().any(|ch| ch.is_ascii_alphanumeric())
}

type SegmentedRomajiInput = Vec<(String, RomajiSegmentMode)>;

fn direct_ascii_dictionary_lattice(
    input: &str,
    index: &UnidicReadingIndex,
    options: DictionaryReadingOptions,
) -> Result<Option<Lattice>, JaLatticeError> {
    let mut inputs = vec![vec![(input.to_string(), RomajiSegmentMode::Surface)]];
    extend_exact_dictionary_lattice_inputs(&mut inputs, input, index, options)?;
    if should_try_hybrid_for_direct_ascii(input, index)? {
        let hybrid_paths = index
            .try_hybrid_reading_paths_with_stats(input, options)
            .map_err(|err| JaLatticeError::ArtifactPayload(err.to_string()))?
            .paths;
        extend_dictionary_lattice_inputs(&mut inputs, &hybrid_paths);
    }
    lattice_from_segmented_inputs(&inputs)
}

fn lattice_from_dictionary_reading_paths(
    reading_paths: &[DictionaryReadingPath],
) -> Result<Option<Lattice>, JaLatticeError> {
    let mut inputs = Vec::new();
    extend_dictionary_lattice_inputs(&mut inputs, reading_paths);
    lattice_from_segmented_inputs(&inputs)
}

fn extend_dictionary_lattice_inputs(
    inputs: &mut Vec<SegmentedRomajiInput>,
    reading_paths: &[DictionaryReadingPath],
) {
    inputs.extend(reading_paths.iter().map(dictionary_lattice_input));
}

fn dictionary_lattice_input(path: &DictionaryReadingPath) -> SegmentedRomajiInput {
    path.segments
        .iter()
        .map(|segment| {
            let mode = match segment.source {
                DictionaryReadingSegmentSource::Dictionary => RomajiSegmentMode::Reading,
                DictionaryReadingSegmentSource::Direct => RomajiSegmentMode::Surface,
            };
            (segment.reading.clone(), mode)
        })
        .collect()
}

fn extend_exact_dictionary_lattice_inputs(
    inputs: &mut Vec<SegmentedRomajiInput>,
    surface: &str,
    index: &UnidicReadingIndex,
    options: DictionaryReadingOptions,
) -> Result<(), JaLatticeError> {
    let Some(surface_readings) = index
        .try_readings(surface)
        .map_err(|err| JaLatticeError::ArtifactPayload(err.to_string()))?
    else {
        return Ok(());
    };
    let surface_readings = if let Some(max_readings) = options.max_readings_per_segment {
        &surface_readings[..surface_readings.len().min(max_readings)]
    } else {
        surface_readings.as_ref()
    };
    inputs.extend(
        surface_readings
            .iter()
            .map(|reading| vec![(reading.clone(), RomajiSegmentMode::Reading)]),
    );
    Ok(())
}

fn lattice_from_segmented_inputs(
    inputs: &[SegmentedRomajiInput],
) -> Result<Option<Lattice>, JaLatticeError> {
    if inputs.is_empty() {
        return Ok(None);
    }

    romaji_lattice_from_supported_segmented_readings(
        inputs
            .iter()
            .map(|path| path.iter().map(|(reading, mode)| (reading.as_str(), *mode))),
    )
}

fn should_try_hybrid_for_direct_ascii(
    input: &str,
    index: &UnidicReadingIndex,
) -> Result<bool, JaLatticeError> {
    if !contains_ascii_alphanumeric(input) || input.is_ascii() {
        return Ok(false);
    }

    let mut run_start = None;
    for (idx, ch) in input.char_indices() {
        if ch.is_ascii_alphanumeric() {
            run_start.get_or_insert(idx);
        } else if let Some(start) = run_start.take() {
            if is_hybrid_ascii_run_candidate(input, start, idx)
                && has_dictionary_reading(&input[start..idx], index)?
            {
                return Ok(true);
            }
        }
    }

    if let Some(start) = run_start {
        return Ok(is_hybrid_ascii_run_candidate(input, start, input.len())
            && has_dictionary_reading(&input[start..], index)?);
    }
    Ok(false)
}

fn is_hybrid_ascii_run_candidate(input: &str, start: usize, end: usize) -> bool {
    start == 0 || end < input.len() || end - start > 1
}

fn has_dictionary_reading(
    surface: &str,
    index: &UnidicReadingIndex,
) -> Result<bool, JaLatticeError> {
    index
        .try_readings(surface)
        .map(|readings| readings.is_some())
        .map_err(|err| JaLatticeError::ArtifactPayload(err.to_string()))
}

fn extend_exact_dictionary_paths(
    paths: &mut BTreeSet<String>,
    surface: &str,
    index: &UnidicReadingIndex,
    options: DictionaryReadingOptions,
) -> Result<(), JaLatticeError> {
    let Some(surface_readings) = index
        .try_readings(surface)
        .map_err(|err| JaLatticeError::ArtifactPayload(err.to_string()))?
    else {
        return Ok(());
    };
    let surface_readings = if let Some(max_readings) = options.max_readings_per_segment {
        &surface_readings[..surface_readings.len().min(max_readings)]
    } else {
        surface_readings.as_ref()
    };
    let reading_paths = surface_readings
        .iter()
        .map(|reading| DictionaryReadingPath {
            segments: vec![DictionaryReadingSegment {
                surface: surface.to_string(),
                reading: reading.clone(),
                source: DictionaryReadingSegmentSource::Dictionary,
            }],
            joined_reading: reading.clone(),
        });
    for path in reading_paths {
        extend_supported_dictionary_paths(paths, std::slice::from_ref(&path))?;
    }
    Ok(())
}

fn extend_supported_dictionary_paths(
    paths: &mut BTreeSet<String>,
    reading_paths: &[DictionaryReadingPath],
) -> Result<(), JaLatticeError> {
    for path in reading_paths {
        match romaji_paths_from_reading_paths(std::slice::from_ref(path)) {
            Ok(romaji_paths) => paths.extend(romaji_paths),
            Err(err) if is_unsupported_dictionary_reading(&err) => continue,
            Err(err) => return Err(err),
        }
    }
    Ok(())
}

fn is_unsupported_dictionary_reading(err: &JaLatticeError) -> bool {
    matches!(
        err,
        JaLatticeError::UnsupportedChar { .. } | JaLatticeError::MissingVariant { .. }
    )
}

fn max_normalized_similarity(left_paths: &[String], right_paths: &[String]) -> f64 {
    let left_paths = char_paths(left_paths);
    let right_paths = char_paths(right_paths);
    let mut workspace = StringDistanceWorkspace::new();
    max_normalized_similarity_chars(&left_paths, &right_paths, &mut workspace)
}

fn char_paths(paths: &[String]) -> Vec<Vec<char>> {
    paths.iter().map(|path| path.chars().collect()).collect()
}

fn max_normalized_similarity_chars(
    left_paths: &[Vec<char>],
    right_paths: &[Vec<char>],
    workspace: &mut StringDistanceWorkspace,
) -> f64 {
    let mut best = 0.0;
    for left in left_paths {
        for right in right_paths {
            let distance = workspace.levenshtein(left, right);
            let similarity = normalized_similarity_from_distance(distance, left.len(), right.len());
            best = f64::max(best, similarity);
            if best == 1.0 {
                return best;
            }
        }
    }
    best
}

fn compare_lattices(
    left: &str,
    right: &str,
    left_lattice: &Lattice,
    right_lattice: &Lattice,
) -> Result<JapaneseDistance, JaLatticeError> {
    let lattice = try_distance(left_lattice, right_lattice)?;
    let lattice_damerau = try_damerau_distance(left_lattice, right_lattice)?;
    let surface_levenshtein = levenshtein_str(left, right);
    let surface_damerau = try_damerau_levenshtein_str(left, right)?;

    Ok(JapaneseDistance {
        surface_levenshtein,
        surface_damerau,
        lattice,
        lattice_damerau,
        combined: surface_damerau.min(lattice),
    })
}

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

    #[test]
    fn combined_takes_the_better_surface_or_lattice_distance() {
        let overrides = OverrideDictionary::from_entries([("印刷", ["インサツ"])]);
        let distances =
            compare_with_overrides("いんさt", "印刷", &overrides).expect("should compare");

        assert_eq!(distances.lattice, 1);
        assert_eq!(distances.lattice_damerau, 1);
        assert!(distances.surface_damerau > distances.lattice);
        assert_eq!(distances.combined, distances.lattice);
    }

    #[test]
    fn lattice_damerau_counts_adjacent_romaji_transposition() {
        let distances = compare_with_overrides("モイネ", "モニエ", &OverrideDictionary::default())
            .expect("kana input should compare");

        assert_eq!(distances.lattice, 2);
        assert_eq!(distances.lattice_damerau, 1);
    }

    #[test]
    fn unidic_index_can_compare_ascii_to_dictionary_reading() {
        let csv = "\
茶,1,2,3,名詞,普通名詞,一般,*,*,*,チャ,茶,茶,チャ,茶,チャ,和
道具,1,2,3,名詞,普通名詞,一般,*,*,*,ドウグ,道具,道具,ドーグ,道具,ドーグ,漢
";
        let index = UnidicReadingIndex::from_lex_csv_reader(csv.as_bytes()).unwrap();
        let distances = compare_with_unidic_index(
            "chadougu",
            "茶道具",
            &index,
            DictionaryReadingOptions {
                longest_match_only: true,
                ..DictionaryReadingOptions::default()
            },
        )
        .expect("should compare");

        assert_eq!(distances.lattice, 0);
        assert!(distances.surface_damerau > distances.lattice);
    }

    #[test]
    fn unidic_lattice_compacts_shared_romaji_branches() {
        let csv = "\
鬼滅,1,2,3,名詞,固有名詞,一般,*,*,*,キメツ,鬼滅,鬼滅,キメツ,鬼滅,キメツ,固
";
        let index = UnidicReadingIndex::from_lex_csv_reader(csv.as_bytes()).unwrap();
        let lattice =
            unidic_or_direct_lattice("鬼滅", &index, DictionaryReadingOptions::default()).unwrap();

        assert!(lattice.node_count() < Lattice::from_paths(["kimetu", "kimetsu"]).node_count());
        assert_eq!(distance(&lattice, &Lattice::from_paths(["kimetsu"])), 0);
    }

    #[test]
    fn unidic_index_combines_direct_ascii_and_dictionary_word_readings() {
        let csv = "\
WHISKY,1,2,3,名詞,普通名詞,一般,*,*,*,ウイスキー,WHISKY,WHISKY,ウイスキー,WHISKY,ウイスキー,外
";
        let index = UnidicReadingIndex::from_lex_csv_reader(csv.as_bytes()).unwrap();
        let options = DictionaryReadingOptions::default();

        let dictionary =
            compare_with_unidic_index("WHISKY", "ウイスキー", &index, options).unwrap();
        let fullwidth_dictionary =
            compare_with_unidic_index("WHISKY", "ウイスキー", &index, options).unwrap();
        let direct = compare_with_unidic_index("WHISKY", "WHISKY", &index, options).unwrap();

        assert_eq!(dictionary.lattice, 0);
        assert_eq!(fullwidth_dictionary.lattice, 0);
        assert_eq!(direct.lattice, 0);
    }

    #[test]
    fn unidic_index_uses_width_aliases_for_whisky_terms() {
        let csv = "\
WHISKY,1,2,3,名詞,普通名詞,一般,*,*,*,ウイスキー,WHISKY,WHISKY,ウイスキー,WHISKY,ウイスキー,外
MALT,1,2,3,名詞,普通名詞,一般,*,*,*,モルト,MALT,MALT,モルト,MALT,モルト,外
";
        let index = UnidicReadingIndex::from_lex_csv_reader(csv.as_bytes()).unwrap();
        let options = DictionaryReadingOptions::default();

        for (left, right) in [
            ("WHISKY", "ウイスキー"),
            ("WHISKY", "ウイスキー"),
            ("MALT", "モルト"),
            ("MALT", "モルト"),
        ] {
            let distances = compare_with_unidic_index(left, right, &index, options).unwrap();
            assert_eq!(distances.lattice, 0, "{left} should match {right}");
        }
    }

    #[test]
    fn unidic_index_skips_unsupported_dictionary_readings_in_fast_lattice() {
        let csv = "\
酒,1,2,3,名詞,普通名詞,一般,*,*,*,サケ,酒,酒,サケ,酒,サケ,和
酒,1,2,3,名詞,普通名詞,一般,*,*,*,シュ,酒,酒,シュ,酒,シュ,漢
酒,1,2,3,名詞,普通名詞,一般,*,*,*,ヷ,酒,酒,ヷ,酒,ヷ,外
";
        let index = UnidicReadingIndex::from_lex_csv_reader(csv.as_bytes()).unwrap();

        let distances =
            compare_with_unidic_index("sake", "", &index, DictionaryReadingOptions::default())
                .expect("unsupported dictionary readings should be skipped");

        assert_eq!(distances.lattice, 0);
    }

    #[test]
    fn unidic_index_keeps_hybrid_paths_for_ascii_whisky_terms() {
        let csv = "\
M,1,2,3,名詞,普通名詞,一般,*,*,*,モー,M,M,モー,M,モー,外
PX,1,2,3,名詞,普通名詞,一般,*,*,*,ピーエックス,PX,PX,ピーエックス,PX,ピーエックス,外
";
        let index = UnidicReadingIndex::from_lex_csv_reader(csv.as_bytes()).unwrap();
        let options = DictionaryReadingOptions::default();

        for (left, right) in [
            ("Mイン", "モーイン"),
            ("PXシェリー", "ピーエックスシェリー"),
        ] {
            let distances = compare_with_unidic_index(left, right, &index, options)
                .expect("ASCII whisky terms should still use dictionary hybrid paths");
            assert_eq!(distances.lattice, 0, "{left} should match {right}");
        }
    }

    #[test]
    fn ascii_mixed_lattice_does_not_expand_direct_whisky_variants() {
        let index = UnidicReadingIndex::default();
        let input = "PXシェリー".repeat(32);

        let lattice = unidic_or_direct_lattice(&input, &index, DictionaryReadingOptions::default())
            .expect("ASCII-mixed direct input should build as a lattice");
        let distances =
            compare_with_unidic_index(&input, &input, &index, DictionaryReadingOptions::default())
                .expect("public distance path should not expand direct variants");

        assert!(lattice.node_count() < 1024);
        assert!(lattice.arcs().len() < 2048);
        assert_eq!(distances.lattice, 0);
    }

    #[test]
    fn unidic_index_keeps_hybrid_paths_for_direct_ascii_with_suffix() {
        let csv = "\
WHISKY,1,2,3,名詞,普通名詞,一般,*,*,*,ウイスキー,WHISKY,WHISKY,ウイスキー,WHISKY,ウイスキー,外
";
        let index = UnidicReadingIndex::from_lex_csv_reader(csv.as_bytes()).unwrap();
        let options = DictionaryReadingOptions::default();

        let distances = compare_with_unidic_index("WHISKYバー", "ウイスキーバー", &index, options)
            .expect("direct ASCII prefix should still use dictionary hybrid paths");

        assert_eq!(distances.lattice, 0);
    }

    #[test]
    fn unidic_index_computes_candidate_pair_similarity() {
        let csv = "\
印刷,1,2,3,名詞,普通名詞,サ変可能,*,*,*,インサツ,印刷,印刷,インサツ,印刷,インサツ,漢
";
        let index = UnidicReadingIndex::from_lex_csv_reader(csv.as_bytes()).unwrap();
        let similarity = normalized_similarity_with_unidic_index(
            "いんさt",
            "印刷",
            &index,
            DictionaryReadingOptions::default(),
        )
        .unwrap();

        assert!((similarity - 6.0 / 7.0).abs() < 1e-12);
    }

    #[test]
    fn artifact_payload_loaders_preserve_comparison_behavior() {
        let csv = "\
印刷,1,2,3,名詞,普通名詞,サ変可能,*,*,*,インサツ,印刷,印刷,インサツ,印刷,インサツ,漢
";
        let index = UnidicReadingIndex::from_lex_csv_reader(csv.as_bytes()).unwrap();
        let yaml_index =
            UnidicReadingIndex::from_artifact_payload(index.artifact_payload()).unwrap();
        let mut binary = Vec::new();
        index.write_artifact_binary_payload(&mut binary).unwrap();
        let binary_index =
            UnidicReadingIndex::from_binary_artifact_payload_reader(binary.as_slice()).unwrap();
        let options = DictionaryReadingOptions::default();

        let csv_distances = compare_with_unidic_index("いんさt", "印刷", &index, options).unwrap();
        let yaml_distances =
            compare_with_unidic_index("いんさt", "印刷", &yaml_index, options).unwrap();
        let binary_distances =
            compare_with_unidic_index("いんさt", "印刷", &binary_index, options).unwrap();

        assert_eq!(yaml_distances, csv_distances);
        assert_eq!(binary_distances, csv_distances);
    }

    #[test]
    fn unidic_comparison_keeps_ascii_as_identity_path() {
        let csv = "\
c,1,2,3,記号,文字,*,*,*,*,シー,c,c,シー,c,シー,外
h,1,2,3,記号,文字,*,*,*,*,エイチ,h,h,エイチ,h,エイチ,外
a,1,2,3,記号,文字,*,*,*,*,エー,a,a,エー,a,エー,外
d,1,2,3,記号,文字,*,*,*,*,ディー,d,d,ディー,d,ディー,外
o,1,2,3,記号,文字,*,*,*,*,オー,o,o,オー,o,オー,外
u,1,2,3,記号,文字,*,*,*,*,ユー,u,u,ユー,u,ユー,外
g,1,2,3,記号,文字,*,*,*,*,ジー,g,g,ジー,g,ジー,外
";
        let index = UnidicReadingIndex::from_lex_csv_reader(csv.as_bytes()).unwrap();
        let lattice =
            unidic_or_direct_lattice("chadougu", &index, DictionaryReadingOptions::default())
                .unwrap();
        let trace = moine_core::distance_with_trace(&lattice, &Lattice::from_paths(["chadougu"]));

        assert_eq!(trace.distance, 0);
    }

    #[test]
    fn hybrid_lattice_allows_dictionary_prefix_and_direct_tail() {
        let csv = "\
印,1,2,3,名詞,普通名詞,一般,*,*,*,イン,印,印,イン,印,イン,漢
";
        let index = UnidicReadingIndex::from_lex_csv_reader(csv.as_bytes()).unwrap();
        let lattice =
            unidic_or_direct_lattice("印さt", &index, DictionaryReadingOptions::default()).unwrap();
        let trace = moine_core::distance_with_trace(&lattice, &Lattice::from_paths(["insat"]));

        assert_eq!(trace.distance, 0);
    }

    #[test]
    fn hybrid_lattice_allows_dictionary_text_with_japanese_punctuation() {
        let csv = "\
印刷,1,2,3,名詞,普通名詞,一般,*,*,*,インサツ,印刷,印刷,インサツ,印刷,インサツ,漢
";
        let index = UnidicReadingIndex::from_lex_csv_reader(csv.as_bytes()).unwrap();
        let lattice =
            unidic_or_direct_lattice("印刷。", &index, DictionaryReadingOptions::default())
                .unwrap();
        let trace = moine_core::distance_with_trace(&lattice, &Lattice::from_paths(["insatu。"]));

        assert_eq!(trace.distance, 0);
    }

    #[test]
    fn hybrid_lattice_allows_direct_prefix_and_dictionary_tail() {
        let csv = "\
具,1,2,3,名詞,普通名詞,一般,*,*,*,グ,具,具,グ,具,グ,漢
";
        let index = UnidicReadingIndex::from_lex_csv_reader(csv.as_bytes()).unwrap();
        let lattice =
            unidic_or_direct_lattice("chadou具", &index, DictionaryReadingOptions::default())
                .unwrap();
        let trace = moine_core::distance_with_trace(&lattice, &Lattice::from_paths(["chadougu"]));

        assert_eq!(trace.distance, 0);
    }

    #[test]
    fn hybrid_lattice_supports_mixed_middle_ascii() {
        let csv = "\
東,1,2,3,名詞,普通名詞,一般,*,*,*,トウ,東,東,トウ,東,トウ,漢
";
        let index = UnidicReadingIndex::from_lex_csv_reader(csv.as_bytes()).unwrap();
        let lattice =
            unidic_or_direct_lattice("東kょう", &index, DictionaryReadingOptions::default())
                .unwrap();
        let trace = moine_core::distance_with_trace(&lattice, &Lattice::from_paths(["toukyou"]));

        assert_eq!(trace.distance, 0);
    }

    #[test]
    fn hybrid_lattice_does_not_guess_unknown_kanji() {
        let index = UnidicReadingIndex::default();
        let err = unidic_or_direct_lattice("未知z", &index, DictionaryReadingOptions::default())
            .unwrap_err();

        assert!(matches!(
            err,
            JaLatticeError::UnsupportedChar {
                ch: '', index: 0
            }
        ));
    }

    #[test]
    fn hybrid_comparison_handles_dictionary_and_ascii_mixture() {
        let csv = "\
鬼滅,1,2,3,名詞,普通名詞,一般,*,*,*,キメツ,鬼滅,鬼滅,キメツ,鬼滅,キメツ,固
刃,1,2,3,名詞,普通名詞,一般,*,*,*,ヤイバ,刃,刃,ヤイバ,刃,ヤイバ,和
";
        let index = UnidicReadingIndex::from_lex_csv_reader(csv.as_bytes()).unwrap();
        let distances = compare_with_unidic_index(
            "鬼滅のyaiba",
            "鬼滅の刃",
            &index,
            DictionaryReadingOptions {
                longest_match_only: true,
                ..DictionaryReadingOptions::default()
            },
        )
        .unwrap();

        assert_eq!(distances.lattice, 0);
    }
}