zhconv 0.4.2

Traditional, Simplified and regional Chinese variants converter powered by MediaWiki & OpenCC rulesets and the Aho-Corasick algorithm 中文简繁及地區詞轉換
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
/// Generates conversion tables and data structures for Chinese character/phrase conversion.
///
/// This build script:
/// - Loads MediaWiki conversion rulesets from `ZhConversion.php`
/// - Optionally merges OpenCC (Open Chinese Convert) rulesets when the "opencc" feature is enabled
/// - Validates file integrity using SHA256 checksums
/// - Sorts conversion pairs by length (longest first) and lexicographically
/// - Deduplicates pairs, retaining only the first rule for each source mapping
/// - Generates three types of output files:
///   - `.from.conv` and `.to.conv`: Compressed pair format for direct lookup
///   - `.daac`: Serialized Aho-Corasick automaton for efficient pattern matching
///
/// The conversion rulesets are processed for the following targets:
/// - `ZH_TO_HANS`: Simplified Chinese
/// - `ZH_TO_HANT`: Traditional Chinese
/// - `ZH_TO_CN`: Mainland China variant (Hans + CN-specific rules)
/// - `ZH_TO_TW`: Taiwan variant (Hant + TW-specific rules)
/// - `ZH_TO_HK`: Hong Kong variant (Hant + HK-specific rules)
/// - `ZH_TO_MO`, `ZH_TO_SG`, `ZH_TO_MY`: Regional variants
///
/// **Note**: Earlier rules take precedence over later ones. When multiple rules apply to the
/// same source string, the first occurrence is retained.
use std::collections::HashMap;

use std::collections::HashSet;
use std::env;
use std::fs::File;
use std::io;
use std::io::Write;
use std::iter;
use std::path::Path;

use daachorse::{CharwiseDoubleArrayAhoCorasickBuilder, MatchKind};
#[cfg(any(feature = "_mediawiki-base", feature = "_opencc-base"))]
use hex_literal::hex;
use vergen::EmitBuilder;

#[cfg(feature = "_opencc-base")]
use self::opencc::load_opencc_to;

// To update upstream rulesets, run `data/update_basic.py and cargo fmt`.
#[cfg(feature = "_mediawiki-base")]
const MEDIAWIKI_COMMIT: &str = "ecf4342132cf089ac0c42436827e9038a738bb6f";
#[cfg(feature = "_mediawiki-base")]
const MEDIAWIKI_SHA256: [u8; 32] =
    hex!("5cb0019b32bb39ec5c6e662029f90bd166f7a844efb3bc877f9be41fdd511bf2");

#[cfg(feature = "_opencc-base")]
const OPENCC_COMMIT: &str = "26753884f1984add422f3b0249ccee8613deaff6";
#[cfg(feature = "_opencc-base")]
const OPENCC_SHA256: [(&str, [u8; 32]); 9] = [
    (
        "HKVariants.txt",
        hex!("e5cd4345303224587102f2c9e4d2b67d2b7e349c6ce9152e4a118f4656cf7302"),
    ),
    (
        "HKVariantsRevPhrases.txt",
        hex!("35352aef4833c2631b2144bc85623cc44d5a09221dda9c32178ea024300d34d3"),
    ),
    (
        "STCharacters.txt",
        hex!("a0ca1601c70648cf48b33c3c6210ccbecc5c7eead4b4c3daf76587ba2c03582b"),
    ),
    (
        "STPhrases.txt",
        hex!("f6eab5e5c6dd7640597878d3dfc6599ee1279d2bc91561eadd8e114194e2925a"),
    ),
    (
        "TSCharacters.txt",
        hex!("737c21c66f55a419dd6956cb3089476cdefc5a36877452631617696df1e5d925"),
    ),
    (
        "TSPhrases.txt",
        hex!("362fa1b9a7d6edd04b462a32e12c9fef3adae822ab1dee9c83561cc37c06cb1f"),
    ),
    (
        "TWPhrases.txt",
        hex!("bcb435b744ee3e522beb9b18fcc5486a36ed4763c6aa642ce18112fb5d604e31"),
    ),
    (
        "TWVariants.txt",
        hex!("e187278e119c427ca561180ac5da5b20e9f8681190458f35c327ce499e95a6a5"),
    ),
    (
        "TWVariantsRevPhrases.txt",
        hex!("5ebfb4bdc938c2b14e01ace378988d5d3dc12462b3496ef1d424951ccd371256"),
    ),
];

const DELIMITER: &str = "|";

fn main() -> io::Result<()> {
    #[cfg(all(
        feature = "opencc-twp",
        not(any(feature = "opencc-tw", feature = "opencc-cn"))
    ))]
    panic!("opencc-twp should only be enabled together with opencc-tw or opencc-cn");

    let mut diagnostics_file =
        File::create(Path::new(&env::var_os("OUT_DIR").unwrap()).join("zhconv-diagnostics.txt"))?;
    macro_rules! log_diag {
        ($fmt:expr $(, $arg:expr)*) => {
            write!(diagnostics_file, $fmt $(, $arg)*)
        };
    }
    log_diag!("=== BUILDING ===\n")?;
    for (k, _v) in std::env::vars() {
        if k.starts_with("CARGO_FEATURE_") {
            log_diag!("feature: {}\n", k.strip_prefix("CARGO_FEATURE_").unwrap())?;
        }
    }
    #[cfg(feature = "_mediawiki-base")]
    log_diag!("MEDIAWIKI_COMMIT={}\n", MEDIAWIKI_COMMIT)?;
    #[cfg(feature = "_opencc-base")]
    log_diag!("OPENCC_COMMIT={}\n", OPENCC_COMMIT)?;
    let start_time = std::time::Instant::now();

    // Load Mediawiki rulesets
    #[cfg(feature = "_mediawiki-base")]
    let mut zhconvs = parse_mediawiki(&read_and_validate_file(
        "data/ZhConversion.php",
        &MEDIAWIKI_SHA256,
    ));
    #[cfg(not(feature = "_mediawiki-base"))]
    let mut zhconvs: HashMap<String, Vec<(String, String)>> = HashMap::new();

    for name in [
        "ZH_TO_HANT",
        "ZH_TO_TW",
        "ZH_TO_HK",
        "ZH_TO_HANS",
        "ZH_TO_CN",
    ] {
        #[allow(unused_mut)]
        let mut pairs = zhconvs.entry(name.to_owned()).or_default();
        log_diag!("Processing {}: MediaWiki.len = {}", name, pairs.len())?;
        // Load and append OpenCC dicts
        // ref: https://github.com/BYVoid/OpenCC/blob/29d33fb8edb8c95e34691c8bd1ef76a50d0b5251/data/config/

        // Note: The conversion of OpenCC takes multi-pass for applying dict groups step by step.
        // For efficiency and reusing the existing implementation, we merge and flatten dict groups
        // in advance.
        // The conversion results may differ from the stock OpenCC implementation considering
        // that some conversion pairs span over the border of several natural phrases while not
        // covering them in whole.
        #[cfg(feature = "_opencc-base")]
        match name {
            // Used when targeting either zh-hans or zh-cn
            #[cfg(any(feature = "opencc-hans", feature = "opencc-cn"))]
            "ZH_TO_HANS" => {
                // config: t2s
                load_opencc_to!(&mut pairs, [TSCharacters, TSPhrases]);

                // OpenCC has rules for de-regionalization when targeting zh-hans/hant,
                // which are not present in https://opencc.byvoid.com.
                // We decide to avoid here, also to keep consistency with Mediawiki's behavior.
                // // config: hk2s & tw2s & t2s
                // load_opencc_to!(
                //     &mut pairs,
                //     [HKVariantsRevPhrases, !HKVariants],
                //     [TSCharacters, TSPhrases]
                // );
                // load_opencc_to!(
                //     &mut pairs,
                //     [TWVariantsRevPhrases, !TWVariants],
                //     [TSCharacters, TSPhrases]
                // );
            }
            // Used when targeting either zh-hant, zh-hk or zh-tw
            #[cfg(any(feature = "opencc-hant", feature = "opencc-tw", feature = "opencc-hk"))]
            "ZH_TO_HANT" => {
                // config: s2t
                load_opencc_to!(&mut pairs, [STCharacters, STPhrases]);

                // ditto
                // // config: hk2t & tw2t
                // load_opencc_to!(&mut pairs, [HKVariantsRevPhrases, !HKVariants]);
                // load_opencc_to!(&mut pairs, [TWVariantsRevPhrases, !TWVariants]);
            }
            #[cfg(feature = "opencc-tw")]
            "ZH_TO_TW" => {
                // twp appears too aggressive for general use, so we make it optional.
                // For example, 电视频段 -> 電影片段 (#8), 雄壮的士兵 -> 雄壮計程車兵.
                if cfg!(feature = "opencc-twp") {
                    // config: s2tw & s2twp & t2tw
                    load_opencc_to!(
                        &mut pairs,
                        [STPhrases, STCharacters],
                        [TWPhrases],
                        [TWVariants]
                    );
                } else {
                    // config: s2tw & t2tw
                    load_opencc_to!(&mut pairs, [STPhrases, STCharacters], [TWVariants]);
                }
            }
            #[cfg(feature = "opencc-hk")]
            "ZH_TO_HK" => {
                // config: s2hk & t2hk
                load_opencc_to!(&mut pairs, [STPhrases, STCharacters], [HKVariants]);
            }
            #[cfg(feature = "opencc-cn")]
            "ZH_TO_CN" => {
                // OpenCC has no dicts for CN-specific phrases, we just do tw2s and hk2s here in
                // addition to t2s when targeting zh-cn.
                if cfg!(feature = "opencc-twp") {
                    // config: tw2sp
                    // "!TWVariants" is deliberately omitted here
                    load_opencc_to!(
                        &mut pairs,
                        [!TWPhrases, TWVariantsRevPhrases],
                        [TSPhrases, TSCharacters]
                    );
                } else {
                    // config: tw2s
                    // "!TWVariants" is deliberately omitted here to prevent misconversions like
                    // `么 -> 幺, 抬 -> 檯, 著 -> 着`.
                    // Since TSCharacters should have covered conversions of character variants,
                    // this is not expected to incur any side effects.
                    load_opencc_to!(
                        &mut pairs,
                        [TWVariantsRevPhrases],
                        [TSPhrases, TSCharacters]
                    );
                }
                // config: hk2s
                // "!HKVariants" is deliberately omitted here.
                load_opencc_to!(
                    &mut pairs,
                    [HKVariantsRevPhrases],
                    [TSPhrases, TSCharacters]
                );
            }
            // "ZH_TO_MO" => {}
            // "ZH_TO_SG" => {}
            // "ZH_TO_MY" => {}
            _ => (),
        }
        log_diag!(", withOpenCC.len = {}", pairs.len())?;

        // Longer phrases and lexicographically smaller phrases appear earlier and hence take
        // precedence in the final conversion table.
        sort_and_dedup(pairs);

        log_diag!(", sortedAndDeduped.len = {}\n", pairs.len())?;
    }

    let hans_pairs = zhconvs.remove("ZH_TO_HANS").unwrap();
    if cfg!(any(
        feature = "mediawiki-hans",
        feature = "opencc-hans",
        feature = "mediawiki-cn",
        feature = "opencc-cn"
    )) {
        write_conv_file("ZH_TO_HANS", &hans_pairs)?;
        write_daac_file("ZH_TO_HANS", &hans_pairs)?;
    }

    let hant_pairs = zhconvs.remove("ZH_TO_HANT").unwrap();
    if cfg!(any(
        feature = "mediawiki-hant",
        feature = "opencc-hant",
        feature = "mediawiki-tw",
        feature = "opencc-tw",
        feature = "mediawiki-hk",
        feature = "opencc-hk"
    )) {
        write_conv_file("ZH_TO_HANT", &hant_pairs)?;
        write_daac_file("ZH_TO_HANT", &hant_pairs)?;
    }

    // The complete table for cn (normalized as hans-cn) are formed by chaining hans table and
    // cn-specific table, so that hans table can be reused for both hans and hans-cn converters,
    // thus reducing the bundled asset size.
    // Chaining does not work for daac, so we have to build complete daac for each target variant,
    // tolerating the redundancy.
    // The same logic applies to tw (hant-tw) and hk (hant-hk).
    let mut cn_pairs = zhconvs.remove("ZH_TO_CN").unwrap();
    if cfg!(any(feature = "mediawiki-cn", feature = "opencc-cn")) {
        let hans_map: HashMap<_, _> = hans_pairs.iter().cloned().collect();
        cn_pairs.retain(|(from, to)| hans_map.get(from.as_str()) != Some(to));
        write_conv_file("ZH_TO_CN", &cn_pairs)?;
        let mut hans_cn_pairs = hans_pairs;
        hans_cn_pairs.extend(cn_pairs);
        write_daac_file("ZH_TO_HANS_CN", &hans_cn_pairs)?;
        log_diag!("ZH_TO_HANS_CN: final.len = {}\n", hans_cn_pairs.len())?;
    }

    // Here, ZH_TO_HANT | ZH_TO_TW => ZH_TO_HANT_TW, etc. In other places, ZH_TO_TW might imply ZH_TO_HANT_TW.

    if cfg!(any(
        feature = "mediawiki-tw",
        feature = "opencc-tw",
        feature = "mediawiki-hk",
        feature = "opencc-hk"
    )) {
        let hant_map: HashMap<_, _> = hant_pairs.iter().cloned().collect();

        let mut tw_pairs = zhconvs.remove("ZH_TO_TW").unwrap();
        if cfg!(any(feature = "mediawiki-tw", feature = "opencc-tw")) {
            tw_pairs.retain(|(from, to)| hant_map.get(from.as_str()) != Some(to));
            write_conv_file("ZH_TO_TW", &tw_pairs)?;
            let mut hant_tw_pairs = hant_pairs.clone();
            hant_tw_pairs.extend(tw_pairs);
            write_daac_file("ZH_TO_HANT_TW", &hant_tw_pairs)?;
            log_diag!("ZH_TO_HANT_TW: final.len = {}\n", hant_tw_pairs.len())?;
        }

        let mut hk_pairs = zhconvs.remove("ZH_TO_HK").unwrap();
        if cfg!(any(feature = "mediawiki-hk", feature = "opencc-hk")) {
            hk_pairs.retain(|(from, to)| hant_map.get(from.as_str()) != Some(to));
            write_conv_file("ZH_TO_HK", &hk_pairs)?;
            let mut hant_hk_pairs = hant_pairs;
            hant_hk_pairs.extend(hk_pairs);
            write_daac_file("ZH_TO_HANT_HK", &hant_hk_pairs)?;
            log_diag!("ZH_TO_HANT_HK: final.len = {}\n", hant_hk_pairs.len())?;
        }
    }

    log_diag!("Built in: {:?}\n=== DONE ===\n", start_time.elapsed())?;

    if std::env::var("DOCS_RS").is_err() {
        // vergen panics in docs.rs. It is only used by wasm.rs for now.
        // So it is ok to disable it in docs.rs.

        // Note: conditional compilation tricks won't be effective since it is cross compiling here.
        // Ref:
        //   https://kazlauskas.me/entries/writing-proper-buildrs-scripts
        //   https://github.com/rust-lang/cargo/issues/4302
        // #[cfg(target_arch = "wasm32")] #[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
        if env::var("CARGO_CFG_TARGET_ARCH") == Ok("wasm32".to_owned()) {
            EmitBuilder::builder()
                .all_build()
                .all_git()
                .emit()
                .unwrap_or_else(|e| println!("cargo:warning=vergen failed: {:?}", e));
        }
    }
    #[cfg(feature = "_mediawiki-base")]
    println!("cargo:rustc-env=MEDIAWIKI_COMMIT_HASH={}", MEDIAWIKI_COMMIT);
    #[cfg(feature = "_opencc-base")]
    println!("cargo:rustc-env=OPENCC_COMMIT_HASH={}", OPENCC_COMMIT);
    println!("cargo:rerun-if-changed=build.rs");
    #[cfg(feature = "_mediawiki-base")]
    println!("cargo:rerun-if-changed=data/ZhConversion.php");
    #[cfg(feature = "_opencc-base")]
    for (opencc, _) in OPENCC_SHA256.iter() {
        println!("cargo:rerun-if-changed=data/{}", opencc);
    }
    println!("cargo:rerun-if-changed=Cargo.toml");

    Ok(())
}

#[cfg(feature = "_mediawiki-base")]
fn parse_mediawiki(text: &str) -> HashMap<String, Vec<(String, String)>> {
    let patb = regex::Regex::new(r"public const (\w+) = \[([^]]+)\]?;").unwrap();
    let patl = regex::Regex::new(r"'(.+?)' *=> *'(.+?)' *,?\n").unwrap();
    let mut res = HashMap::new();

    for block in patb.captures_iter(text) {
        let name = block.get(1).unwrap().as_str();
        let body = block.get(2).unwrap().as_str();
        let mut pairs = vec![];
        for line in patl.captures_iter(body) {
            let from = line.get(1).unwrap().as_str();
            let to = line.get(2).unwrap().as_str();
            pairs.push((from.to_owned(), to.to_owned()));
        }
        assert!(res.insert(name.to_owned(), pairs).is_none());
    }
    for name in [
        "ZH_TO_HANS",
        "ZH_TO_HANT",
        "ZH_TO_CN",
        "ZH_TO_TW",
        "ZH_TO_HK",
    ] {
        assert!(res.contains_key(name));
    }
    res
}

fn write_conv_file(name: &str, pairs: &[(String, String)]) -> io::Result<()> {
    let out_dir = env::var_os("OUT_DIR").unwrap();
    // {from, to}.conv is just DELIMITER-separated list of {source, target} phrases.
    // source phrases, which are coded into daac, are only useful for building new daacs.
    // However, we still bundle it anyway for implementation convenience.
    let dest_path_from = Path::new(&out_dir).join(format!("{}.from.conv", name));
    let dest_path_to = Path::new(&out_dir).join(format!("{}.to.conv", name));

    let mut ffrom = File::create(dest_path_from)?;
    let mut fto = File::create(dest_path_to)?;
    let mut it = pairs.iter().peekable();
    let mut last_from = "";
    while let Some((from, to)) = it.next().map(|(f, t)| (f, t)) {
        debug_assert!(
            !from.contains(DELIMITER) && !to.contains(DELIMITER),
            "Unexpected delimiter {} in pair {} -> {}",
            DELIMITER,
            from,
            to
        );
        debug_assert!(
            !from
                .chars()
                .any(|c| (SURROGATE_START..SURROGATE_END).contains(&c))
                && !to
                    .chars()
                    .any(|c| (SURROGATE_START..SURROGATE_END).contains(&c)),
            "Unexpected surrogate char in pair {} -> {}",
            from,
            to
        );
        for c in pair_reduce(from.chars(), last_from.chars()) {
            write!(ffrom, "{}", c)?;
        }
        for c in pair_reduce(to.chars(), from.chars()) {
            write!(fto, "{}", c)?;
        }
        if it.peek().is_some() {
            write!(ffrom, "{}", DELIMITER)?;
            write!(fto, "{}", DELIMITER)?;
        }
        last_from = from;
    }

    Ok(())
}

fn write_daac_file(name: &str, pairs: &[(String, String)]) -> io::Result<()> {
    let mut seen = HashSet::new();
    let out_dir = env::var_os("OUT_DIR").unwrap();
    let dest_path_daac = Path::new(&out_dir).join(format!("{}.daac", name));
    let daac = CharwiseDoubleArrayAhoCorasickBuilder::new()
        .match_kind(MatchKind::LeftmostLongest)
        .build_with_values::<_, _, u32>(pairs.iter().enumerate().rev().filter_map(
            |(i, (f, _t))| {
                // Note the rev here, which ensures later rules take precedence over earlier ones.
                if seen.contains(f) {
                    None
                } else {
                    seen.insert(f);
                    Some((f, i as u32))
                }
            },
        ))
        .expect(name)
        .serialize();

    #[cfg(feature = "compress")]
    let daac = zstd::bulk::Compressor::new(21)
        .unwrap()
        .compress(&daac)
        .unwrap();

    File::create(dest_path_daac)?.write_all(&daac)
}

const SURROGATE_START: char = '\x00';
const SURROGATE_END: char = '\x20'; // exclusive

// simple but efficient compression
fn pair_reduce<'s>(
    mut s: impl Iterator<Item = char> + 's + Clone,
    mut base: impl Iterator<Item = char> + 's + Clone,
) -> impl Iterator<Item = char> + 's + Clone {
    let mut it = iter::from_fn(move || match (s.next(), base.next()) {
        (Some(a), Some(b)) if a == b => Some(SURROGATE_START),
        (Some(a), _) => Some(a),
        (None, _) => None,
    })
    .peekable();

    iter::from_fn(move || {
        it.next().map(|curr| {
            if curr == SURROGATE_START {
                let mut count = 1;
                while Some(&SURROGATE_START) == it.peek() {
                    if (SURROGATE_START as u32) + (count + 1) >= (SURROGATE_END as u32) {
                        break;
                    }
                    let _ = it.next();
                    count += 1;
                }
                char::from_u32(SURROGATE_START as u32 + count).unwrap()
            } else {
                curr
            }
        })
    })
}

fn sort_and_dedup(pairs: &mut Vec<(String, String)>) {
    // earlier rules take precedence
    pairs.sort_by(|a, b| b.0.len().cmp(&a.0.len()).then(a.0.cmp(&b.0)));
    pairs.dedup_by(|a, b| a.0 == b.0);
}

#[cfg(feature = "_opencc-base")]
mod opencc {

    use daachorse::{
        CharwiseDoubleArrayAhoCorasick, CharwiseDoubleArrayAhoCorasickBuilder, MatchKind,
    };
    // use aho_corasick::{AhoCorasick, AhoCorasickBuilder, MatchKind};
    use std::collections::HashMap;
    use std::sync::LazyLock;

    use super::OPENCC_SHA256;

    pub static OPENCC_SHA256_MAP: LazyLock<HashMap<String, [u8; 32]>> = LazyLock::new(|| {
        OPENCC_SHA256
            .into_iter()
            .map(|(n, s)| (n.to_owned(), s))
            .collect()
    });

    macro_rules! load_opencc_to {
        ( @read_to $out_conv: expr, $out_revconv: expr, $name: ident) => {
            let s = read_and_validate_file(concat!("data/", stringify!($name), ".txt"), crate::opencc::OPENCC_SHA256_MAP.get(stringify!($name.txt)).expect(stringify!($name.txt not found)));
            crate::opencc::parse_opencc_to($out_conv, $out_revconv, &s);
        };
        ( @parse_to $out_conv: expr, $out_revconv: expr, $name: ident, $($remainings: tt)* ) => {
            load_opencc_to!(@read_to $out_conv, $out_revconv, $name);
            load_opencc_to!(@parse_to $out_conv, $out_revconv, $($remainings)*);
        };
        ( @parse_to $out_conv: expr, $out_revconv: expr, $name: ident ) => {
            load_opencc_to!(@read_to $out_conv, $out_revconv, $name);
        };
        ( @parse_to $out_conv: expr, $out_revconv: expr, ! $name: ident, $($remainings: tt)* ) => {
            load_opencc_to!(@read_to $out_revconv, $out_conv, $name);
            load_opencc_to!(@parse_to $out_conv, $out_revconv, $($remainings)*);
        };
        ( @parse_to $out_conv: expr, $out_revconv: expr, ! $name: ident ) => {
            load_opencc_to!(@read_to $out_revconv, $out_conv, $name);
        };
        ( @load_stage $out: expr, $prev_stage: ident, [ $($rule: tt)+ ] ) => {
            let (mut prev_convs, prev_revconvs): (HashMap<String, String>, HashMap<String, String>) = $prev_stage.unwrap_or_else(|| (HashMap::new(), HashMap::new()));
            let mut convs: HashMap<String, String> = HashMap::new();
            let mut revconvs: HashMap<String, String> = HashMap::new();
            load_opencc_to!(@parse_to &mut convs, &mut revconvs, $($rule)*);
            let conver: crate::opencc::SimpleConverter = convs.clone().into();
            let prev_revconver: crate::opencc::SimpleConverter = prev_revconvs.clone().into();
            for (_f, t) in prev_convs.iter_mut() {
                *t = conver.convert(t);
            }
            for (f, t) in convs.iter() {
                prev_convs.insert(f.clone(), t.clone());
                let ff = prev_revconver.convert(f);
                if &ff != f && &ff != t /* ? */ {
                    prev_convs.insert(ff.to_owned(), t.to_owned());
                }
            }
            for (_f, t) in revconvs.iter_mut() {
                *t = prev_revconver.convert(t);
            }
            revconvs.extend(prev_revconvs.iter().map(|(f, t)| (conver.convert(f), t.to_owned())));
            revconvs.extend(prev_revconvs.iter().map(|(f, t)| (f.to_owned(), t.to_owned())));
            $prev_stage = Some((prev_convs, revconvs));
        };
        ( $out: expr, $($stage: tt),+ ) => {
            let mut prev_stage = None;
            $(load_opencc_to!(@load_stage $out, prev_stage, $stage);)*
            let (convs, _) = prev_stage.unwrap();
            $out.extend(convs.into_iter());
        };
    }
    pub(crate) use load_opencc_to;
    pub fn parse_opencc_to(
        out_conv: &mut HashMap<String, String>,
        out_revconv: &mut HashMap<String, String>,
        s: &str,
    ) {
        // Strip BOM if present,
        // matching https://github.com/BYVoid/OpenCC/blob/master/src/Lexicon.cpp#L88
        let s = s.strip_prefix('\u{feff}').unwrap_or(s);
        for line in s
            .lines()
            .map(|l| l.trim())
            // Ignore #-prefixed comment lines, but no trailing comments stripping,
            // matching https://github.com/BYVoid/OpenCC/pull/1016
            .filter(|l| !l.is_empty() && !l.starts_with('#'))
        {
            if let Some((f, ts)) = line.split_once(char::is_whitespace) {
                if f.is_empty() || ts.is_empty() {
                    continue;
                }
                let ts: Vec<_> = ts.split_whitespace().collect();
                if !(ts.len() > 1 && ts.contains(&f)) {
                    // be conservative when converting
                    // e.g. 范 -> 範 范 can be simply eliminated
                    out_conv.insert(f.to_owned(), ts[0].to_owned());
                }
                for t in ts {
                    if !out_revconv.contains_key(t) {
                        out_revconv.insert(t.to_owned(), f.to_owned());
                    }
                }
            }
        }
    }

    /// Simplified `ZhConverter` implementation for pre-processing rulesets from OpenCC
    pub struct SimpleConverter {
        automaton: Option<CharwiseDoubleArrayAhoCorasick<usize>>,
        target_words: Vec<String>,
    }

    impl From<HashMap<String, String>> for SimpleConverter {
        fn from(mapping: HashMap<String, String>) -> Self {
            let mut target_words = Vec::with_capacity(mapping.len());
            let automaton = if mapping.is_empty() {
                None
            } else {
                Some(
                    CharwiseDoubleArrayAhoCorasickBuilder::new()
                        .match_kind(MatchKind::LeftmostLongest)
                        .build(mapping.into_iter().map(|(f, t)| {
                            target_words.push(t);
                            f
                        }))
                        .expect("Conversion table is valid"),
                )
            };
            Self {
                automaton,
                target_words,
            }
        }
    }

    impl SimpleConverter {
        #[allow(dead_code)]
        pub fn build<'s>(pairs: impl Iterator<Item = (&'s str, &'s str)>) -> Self {
            let mapping = HashMap::from_iter(pairs.map(|(a, b)| (a.to_owned(), b.to_owned())));
            mapping.into()
        }

        pub fn convert(&self, text: &str) -> String {
            match &self.automaton {
                Some(automaton) => {
                    let mut output = String::new();
                    let mut last = 0;
                    // leftmost-longest matching
                    for (s, e, ti) in automaton
                        .leftmost_find_iter(text)
                        .map(|m| (m.start(), m.end(), m.value()))
                    {
                        if s > last {
                            output.push_str(&text[last..s]);
                        }
                        output.push_str(&self.target_words[ti]);
                        last = e;
                    }
                    output.push_str(&text[last..]);
                    output
                }
                None => String::from(text),
            }
        }
    }
}

#[cfg(any(feature = "_mediawiki-base", feature = "_opencc-base"))]
fn read_and_validate_file(path: &str, sha256sum: &[u8; 32]) -> String {
    fn sha256(text: &str) -> [u8; 32] {
        use sha2::{Digest, Sha256};

        let mut hasher = Sha256::new();
        hasher.update(text.as_bytes());
        hasher.finalize().into()
    }

    let data_dir = env::var_os("CARGO_MANIFEST_DIR").unwrap();
    let path = Path::new(&data_dir).join(path);
    let content = String::from_utf8(
        std::fs::read(&path).unwrap_or_else(|e| panic!("{} when reading {}", e, path.display())),
    )
    .unwrap_or_else(|e| panic!("{} is not in valid UTF-8 ({})", path.display(), e));
    assert_eq!(
        &sha256(&content),
        sha256sum,
        "Validating the checksum of {}",
        path.display()
    );
    content
}