msime-engine 0.2.0

Input method engine for Chinese and other languages: quanpin, shuangpin, nine-key, wubi, zhuyin, Cantonese, Japanese, Korean and more, ranked over a word lattice with typo correction and learning from what the user picks. The engine of 水杉输入法 (Metasequoia IME).
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
//! `E` and `M` modes and the mixed emoji / kaomoji rows (emoji_query.cpp:59-140, kaomoji_query.cpp). Shuangpin also queries the code normalised to quanpin.
//!
//! 混排(26 键和九宫格)用 `query_mixed_*` / `query_*_readings`:行带上目录里的关键词,混排按关键词把它接在描绘的那个候选词后面。九宫格的数字串没有唯一的拼音,按几种可能的读法依次查。

use std::borrow::Cow;
use std::path::Path;

use rusqlite::Connection;

use super::database::{lock, open_local_database};
use super::LocalQueryResult;
use crate::diagnostics;
use crate::shuangpin::query::normalize_input;
use crate::shuangpin::ShuangpinProfile;
use crate::types::{CandidateSource, SchemeType, WordItem};

pub const MODE_RESULT_LIMIT: usize = 10;
/// 混排时每组(emoji、颜文字)最多留几行接不上任何候选词的行,它们排在列表末尾。
pub const MIXED_RESULT_LIMIT: usize = 3;
/// 混排时每种读法最多取几行:编码与读法完全相同的排在前面,再按目录顺序。行要靠关键词接到候选词后面,取得太少会漏掉候选里那个词的 emoji(`ji` 有两百多行,🐔 按目录顺序排在九十几位),取得太多又拖慢每次按键。
pub const MIXED_FETCH_PER_READING: usize = 16;
/// 混排时每组最多取几行,各种读法合计。不超过 64,混排去重用一个 `u64` 记每组的去留。
pub const MIXED_FETCH_LIMIT: usize = 48;

/// 混排的一行 emoji 或颜文字,带着它在目录里的关键词(`emoji.keywords` / `kaomoji_catalog.keywords`,空格分隔,中文词在前)。
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ExpressiveRow {
    pub item: WordItem,
    pub keywords: String,
}

/// The one difference between the two files of the reference: the table, the source and the diagnostics.
struct Catalog {
    sql: &'static str,
    /// 混排用的语句:取回文字、命中的两列编码(emoji 只有 `key`,第二列为空)和目录里的关键词;编码与读法完全相同的在前,再按目录顺序。不带 `LIMIT`,由调用方核对编码后取够为止。
    mixed_sql: &'static str,
    /// 资源里没有目录表(`emoji` / `kaomoji_catalog`)时退回的语句:同样的行,关键词为空,混排时都接不上候选词,排在末尾。
    mixed_sql_without_keywords: &'static str,
    source: CandidateSource,
    unavailable: &'static str,
    query_failed: &'static str,
}

const EMOJI: Catalog = Catalog {
    sql: "SELECT emoji,sort_order FROM emoji_pinyin WHERE key>=?1 AND key<?2 ORDER BY sort_order LIMIT ?3",
    mixed_sql: "SELECT p.emoji,p.key,'',COALESCE(e.keywords,'') FROM emoji_pinyin p LEFT JOIN emoji e ON e.emoji=p.emoji WHERE p.key>=?1 AND p.key<?2 ORDER BY p.key<>?1,p.sort_order",
    mixed_sql_without_keywords: "SELECT emoji,key,'','' FROM emoji_pinyin WHERE key>=?1 AND key<?2 ORDER BY key<>?1,sort_order",
    source: CandidateSource::Emoji,
    unavailable: diagnostics::EMOJI_UNAVAILABLE,
    query_failed: diagnostics::EMOJI_QUERY_FAILED,
};

const KAOMOJI: Catalog = Catalog {
    sql: "SELECT kaomoji,sort_order FROM kaomoji WHERE (pinyin>=?1 AND pinyin<?2) OR (jianpin>=?1 AND jianpin<?2) ORDER BY sort_order LIMIT ?3",
    mixed_sql: "SELECT k.kaomoji,k.pinyin,k.jianpin,COALESCE(c.keywords,'') FROM kaomoji k LEFT JOIN kaomoji_catalog c ON c.kaomoji=k.kaomoji WHERE (k.pinyin>=?1 AND k.pinyin<?2) OR (k.jianpin>=?1 AND k.jianpin<?2) ORDER BY k.pinyin<>?1 AND k.jianpin<>?1,k.sort_order",
    mixed_sql_without_keywords: "SELECT kaomoji,pinyin,jianpin,'' FROM kaomoji WHERE (pinyin>=?1 AND pinyin<?2) OR (jianpin>=?1 AND jianpin<?2) ORDER BY pinyin<>?1 AND jianpin<>?1,sort_order",
    source: CandidateSource::Kaomoji,
    unavailable: diagnostics::KAOMOJI_UNAVAILABLE,
    query_failed: diagnostics::KAOMOJI_QUERY_FAILED,
};

struct QueryPrefixes<'a> {
    lower: &'a str,
    normalized: Option<String>,
}

impl<'a> QueryPrefixes<'a> {
    fn new(lower: &'a str, scheme: SchemeType, profile: &ShuangpinProfile) -> Self {
        let normalized = if scheme == SchemeType::Shuangpin {
            let quanpin = normalize_input(lower, profile);
            (!quanpin.is_empty() && quanpin != lower).then_some(quanpin)
        } else {
            None
        };
        Self { lower, normalized }
    }

    fn len(&self) -> usize {
        1 + usize::from(self.normalized.is_some())
    }

    fn iter(&self) -> impl Iterator<Item = &str> + Clone {
        std::iter::once(self.lower).chain(self.normalized.iter().map(String::as_str))
    }
}

pub fn query_emoji(
    code: &str,
    scheme: SchemeType,
    others_db: &Path,
    limit: usize,
    profile: &ShuangpinProfile,
) -> LocalQueryResult {
    query(&EMOJI, code, scheme, others_db, limit, profile)
}

pub fn query_kaomoji(
    code: &str,
    scheme: SchemeType,
    others_db: &Path,
    limit: usize,
    profile: &ShuangpinProfile,
) -> LocalQueryResult {
    query(&KAOMOJI, code, scheme, others_db, limit, profile)
}

/// 26 键混排:按输入码查 emoji(双拼另查归一成全拼的码),见 `query_readings`。输入码只收字母和 `'`,双拼方案使用分号键时还收作为 `ing` 韵母的 `;`。
pub fn query_mixed_emoji(
    code: &str,
    scheme: SchemeType,
    others_db: &Path,
    profile: &ShuangpinProfile,
) -> Vec<ExpressiveRow> {
    query_mixed(&EMOJI, code, scheme, others_db, profile)
}

/// 26 键混排:按输入码查颜文字,见 `query_mixed_emoji`。
pub fn query_mixed_kaomoji(
    code: &str,
    scheme: SchemeType,
    others_db: &Path,
    profile: &ShuangpinProfile,
) -> Vec<ExpressiveRow> {
    query_mixed(&KAOMOJI, code, scheme, others_db, profile)
}

fn query_mixed(
    catalog: &Catalog,
    code: &str,
    scheme: SchemeType,
    others_db: &Path,
    profile: &ShuangpinProfile,
) -> Vec<ExpressiveRow> {
    if code.is_empty()
        || !code.bytes().all(|byte| {
            byte.is_ascii_alphabetic()
                || byte == b'\''
                || (byte == b';' && scheme == SchemeType::Shuangpin && profile.uses_semicolon_key())
        })
    {
        return Vec::new();
    }
    let lower = if code
        .bytes()
        .all(|byte| byte.is_ascii_lowercase() || byte == b'\'')
    {
        Cow::Borrowed(code)
    } else {
        Cow::Owned(code.to_ascii_lowercase())
    };
    let prefixes = QueryPrefixes::new(lower.as_ref(), scheme, profile);
    query_readings(catalog, prefixes.iter(), others_db, &|_| true)
}

/// 九宫格混排:依次按 `readings`(小写全拼,可能性高的在前)查 emoji,见 `query_readings`。
pub fn query_emoji_readings(
    readings: &[String],
    others_db: &Path,
    accept: &dyn Fn(&str) -> bool,
) -> Vec<ExpressiveRow> {
    query_readings(
        &EMOJI,
        readings.iter().map(String::as_str),
        others_db,
        accept,
    )
}

/// 九宫格混排:依次按 `readings` 查颜文字,见 `query_readings`。
pub fn query_kaomoji_readings(
    readings: &[String],
    others_db: &Path,
    accept: &dyn Fn(&str) -> bool,
) -> Vec<ExpressiveRow> {
    query_readings(
        &KAOMOJI,
        readings.iter().map(String::as_str),
        others_db,
        accept,
    )
}

/// 读法逐个查,每种读法内编码与读法完全相同的在前、再按目录顺序;命中的编码(emoji 的 `key`,颜文字的 `pinyin` 或 `jianpin`)以这个读法开头、并且通过 `accept`,这一行才算。每种读法最多 `MIXED_FETCH_PER_READING` 行,跨读法按文字去重,合计取够 `MIXED_FETCH_LIMIT` 行就不再查。行的 `pinyin` 是命中的读法,权重从行数往下数。混排不报诊断:资源打不开或查询失败时只是没有这些行。
fn query_readings<'a, I>(
    catalog: &Catalog,
    readings: I,
    others_db: &Path,
    accept: &dyn Fn(&str) -> bool,
) -> Vec<ExpressiveRow>
where
    I: IntoIterator<Item = &'a str> + Clone,
{
    if readings.clone().into_iter().next().is_none() {
        return Vec::new();
    }
    let Some(database) = open_local_database(others_db) else {
        return Vec::new();
    };
    let connection = lock(&database);
    let mut entries = match read_readings(
        &connection,
        catalog.mixed_sql,
        readings.clone(),
        accept,
        catalog.source,
    ) {
        Ok(entries) => entries,
        // 目录表缺失只是接不上候选词,行照样给;其他失败由退回的语句再报一次,同样没有行。
        Err(_) => read_readings(
            &connection,
            catalog.mixed_sql_without_keywords,
            readings,
            accept,
            catalog.source,
        )
        .unwrap_or_default(),
    };
    let count = entries.len();
    for (index, row) in entries.iter_mut().enumerate() {
        row.item.weight = (count - index) as i64;
    }
    entries
}

fn read_readings<'a, I>(
    connection: &Connection,
    sql: &str,
    readings: I,
    accept: &dyn Fn(&str) -> bool,
    source: CandidateSource,
) -> rusqlite::Result<Vec<ExpressiveRow>>
where
    I: IntoIterator<Item = &'a str>,
{
    let mut statement = connection.prepare_cached(sql)?;
    let mut entries: Vec<ExpressiveRow> = Vec::new();
    for reading in readings {
        let upper_bound = prefix_upper_bound(reading);
        let mut rows = statement.query(rusqlite::params![reading, upper_bound])?;
        let mut taken = 0;
        while let Some(row) = rows.next()? {
            let Some(text) = row.get::<_, Option<String>>(0)? else {
                continue;
            };
            // `OR` 的另一列可能才是命中的那一列,两列各自核对。
            let mut matched = false;
            for column in 1..=2 {
                let key = row.get::<_, Option<String>>(column)?.unwrap_or_default();
                if key.starts_with(reading) && accept(&key) {
                    matched = true;
                    break;
                }
            }
            if !matched
                || entries
                    .iter()
                    .any(|entry: &ExpressiveRow| entry.item.word == text)
            {
                continue;
            }
            let keywords = row.get::<_, Option<String>>(3)?.unwrap_or_default();
            if entries.is_empty() {
                entries.reserve_exact(MIXED_FETCH_PER_READING);
            }
            entries.push(ExpressiveRow {
                item: WordItem::new(reading, text, 0, source, ""),
                keywords,
            });
            if entries.len() == MIXED_FETCH_LIMIT {
                return Ok(entries);
            }
            taken += 1;
            if taken == MIXED_FETCH_PER_READING {
                break;
            }
        }
    }
    Ok(entries)
}

fn query(
    catalog: &Catalog,
    code: &str,
    scheme: SchemeType,
    others_db: &Path,
    limit: usize,
    profile: &ShuangpinProfile,
) -> LocalQueryResult {
    if code.is_empty()
        || limit == 0
        || !code
            .bytes()
            .all(|byte| byte.is_ascii_alphabetic() || byte == b'\'')
    {
        return LocalQueryResult::default();
    }
    let lower = code.to_ascii_lowercase();
    let prefixes = QueryPrefixes::new(&lower, scheme, profile);
    let Some(database) = open_local_database(others_db) else {
        return LocalQueryResult::failure(catalog.unavailable);
    };
    let entries = match read(
        &lock(&database),
        catalog.sql,
        prefixes.iter(),
        prefixes.len(),
        limit,
    ) {
        Ok(entries) => entries,
        // Reported without SQLite's text, which could name the path or the typed code (emoji_query.cpp:98-127).
        Err(_) => return LocalQueryResult::failure(catalog.query_failed),
    };
    let count = entries.len().min(limit);
    let candidates = entries
        .into_iter()
        .take(count)
        .enumerate()
        .map(|(index, (text, _))| {
            WordItem::new(
                lower.clone(),
                text,
                (count - index) as i64,
                catalog.source,
                "",
            )
        })
        .collect();
    LocalQueryResult {
        candidates,
        diagnostic: None,
    }
}

/// Every prefix's rows, the first occurrence of a text kept, then stably ordered by `sort_order` so the raw and normalised shuangpin matches interleave in catalog order.
fn read<'a, I>(
    connection: &Connection,
    sql: &str,
    prefixes: I,
    prefix_count: usize,
    limit: usize,
) -> rusqlite::Result<Vec<(String, i64)>>
where
    I: IntoIterator<Item = &'a str>,
{
    let mut statement = connection.prepare_cached(sql)?;
    let capacity = limit.saturating_mul(prefix_count);
    let mut entries = Vec::new();
    for prefix in prefixes {
        let upper_bound = prefix_upper_bound(prefix);
        let rows = statement.query_map(
            rusqlite::params![prefix, upper_bound, super::sql_limit(limit)],
            |row| {
                Ok((
                    row.get::<_, Option<String>>(0)?,
                    row.get::<_, Option<i64>>(1)?,
                ))
            },
        )?;
        for row in rows {
            if let (Some(text), sort_order) = row? {
                // The result page is deliberately small (normally ten rows, at most one
                // page per spelling). Scanning the collected rows avoids allocating a
                // second owned copy of every emoji/kaomoji just to deduplicate it.
                if !entries.iter().any(|(entry, _)| entry == &text) {
                    if entries.is_empty() {
                        entries.reserve_exact(capacity);
                    }
                    entries.push((text, sort_order.unwrap_or(0)));
                }
            }
        }
    }
    entries.sort_by_key(|(_, sort_order)| *sort_order);
    Ok(entries)
}

fn prefix_upper_bound(prefix: &str) -> String {
    let mut upper_bound = String::with_capacity(prefix.len() + 1);
    upper_bound.push_str(prefix);
    upper_bound.push('\x7f');
    upper_bound
}

#[cfg(test)]
fn contains_text(entries: &[(String, i64)], text: &str) -> bool {
    entries.iter().any(|(entry, _)| entry == text)
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use super::*;
    use crate::types::ShuangpinProfileKind;

    const QUANPIN_ONLY: ShuangpinProfile = ShuangpinProfile {
        kind: ShuangpinProfileKind::Xiaohe,
        initials: &[],
        zero_initials: &[],
        finals: &[],
    };

    fn fixture(dir: &Path) -> PathBuf {
        let path = dir.join("msime-others.db");
        Connection::open(&path)
            .unwrap()
            .execute_batch(
                "CREATE TABLE emoji_pinyin(key TEXT,emoji TEXT,sort_order INTEGER);
                 INSERT INTO emoji_pinyin VALUES('xiaolian','😀',10);
                 INSERT INTO emoji_pinyin VALUES('xiaolian','😄',20);
                 INSERT INTO emoji_pinyin VALUES('xiaolian','😀',30);
                 INSERT INTO emoji_pinyin VALUES('xl','😀',10);
                 INSERT INTO emoji_pinyin VALUES('xiao''lian','😄',20);
                 INSERT INTO emoji_pinyin VALUES('laugh','😀',10);
                 INSERT INTO emoji_pinyin VALUES('xnlm','raw shuangpin match',40);
                 CREATE TABLE kaomoji(pinyin TEXT,jianpin TEXT,kaomoji TEXT,sort_order INTEGER);
                 INSERT INTO kaomoji VALUES('haixiu','hx','(*/ω\*)',10);
                 INSERT INTO kaomoji VALUES('haixiu','hx','(^_^)',20);
                 INSERT INTO kaomoji VALUES('haixiu','hx','(*/ω\*)',30);
                 INSERT INTO kaomoji VALUES('kiss','','( ˘ ³˘)♥',40);
                 INSERT INTO kaomoji VALUES('kind','','single prefix',50);",
            )
            .unwrap();
        path
    }

    fn words(result: &LocalQueryResult) -> Vec<&str> {
        result
            .candidates
            .iter()
            .map(|row| row.word.as_str())
            .collect()
    }

    #[test]
    fn duplicate_text_lookup_scans_existing_entries() {
        let entries = vec![("😀".to_owned(), 1)];
        assert!(contains_text(&entries, "😀"));
        assert!(!contains_text(&entries, "😄"));
    }

    #[test]
    fn quanpin_prefixes_borrow_the_lowercase_input_without_allocating() {
        let (_, allocations) = crate::ime::personal_rerank::allocations::count(|| {
            QueryPrefixes::new("ni", SchemeType::Quanpin, &QUANPIN_ONLY)
        });
        assert_eq!(allocations, 0);
    }

    #[test]
    fn prefix_upper_bound_allocates_only_result_bytes() {
        let prefix = "xiao'lian";
        let upper_bound = prefix_upper_bound(prefix);
        assert_eq!(upper_bound, "xiao'lian\x7f");
        assert_eq!(upper_bound.capacity(), upper_bound.len());
    }

    #[test]
    fn empty_catalog_prefix_does_not_reserve_result_page() {
        let dir = tempfile::tempdir().unwrap();
        let path = fixture(dir.path());
        let connection = Connection::open(path).unwrap();
        let entries = read(&connection, EMOJI.sql, std::iter::once("missing"), 1, 10).unwrap();
        assert!(entries.is_empty());
        assert_eq!(entries.capacity(), 0);
    }

    /// test_local_modes.cpp:269-296, the quanpin half.
    #[test]
    fn emoji_rows() {
        let dir = tempfile::tempdir().unwrap();
        let path = fixture(dir.path());
        let emoji = query_emoji("XIAOLIAN", SchemeType::Quanpin, &path, 10, &QUANPIN_ONLY);
        assert_eq!(emoji.diagnostic, None);
        assert_eq!(words(&emoji), ["😀", "😄"]);
        assert_eq!(emoji.candidates[0].pinyin, "xiaolian");
        assert_eq!(emoji.candidates[0].source, CandidateSource::Emoji);
        assert_eq!(emoji.candidates[0].weight, 2);
        assert_eq!(emoji.candidates[1].weight, 1);

        let quanpin = SchemeType::Quanpin;
        // The local_emoji_kaomoji golden types the apostrophe, which is part of the key.
        let separated = query_emoji("xiao'lian", quanpin, &path, 10, &QUANPIN_ONLY);
        assert_eq!(words(&separated), ["😄"]);
        assert_eq!(separated.candidates[0].pinyin, "xiao'lian");
        assert_eq!(
            words(&query_emoji("xl", quanpin, &path, 10, &QUANPIN_ONLY))[0],
            "😀"
        );
        assert_eq!(
            words(&query_emoji("laugh", quanpin, &path, 10, &QUANPIN_ONLY))[0],
            "😀"
        );
        assert_eq!(
            words(&query_emoji("xiao", quanpin, &path, 10, &QUANPIN_ONLY)),
            ["😀", "😄"]
        );
        assert_eq!(
            query_emoji("xiaolian", quanpin, &path, 1, &QUANPIN_ONLY)
                .candidates
                .len(),
            1
        );
        for code in ["", "bad1", "xiao lian", "表情"] {
            assert_eq!(
                query_emoji(code, quanpin, &path, 10, &QUANPIN_ONLY),
                LocalQueryResult::default()
            );
        }
        assert_eq!(
            query_emoji("x", quanpin, &path, 0, &QUANPIN_ONLY),
            LocalQueryResult::default()
        );
    }

    /// test_local_modes.cpp:298-317, the quanpin half.
    #[test]
    fn kaomoji_rows() {
        let dir = tempfile::tempdir().unwrap();
        let path = fixture(dir.path());
        let quanpin = SchemeType::Quanpin;
        let kaomoji = query_kaomoji("HAIXIU", quanpin, &path, 10, &QUANPIN_ONLY);
        assert_eq!(kaomoji.diagnostic, None);
        assert_eq!(words(&kaomoji), ["(*/ω\*)", "(^_^)"]);
        assert_eq!(kaomoji.candidates[0].pinyin, "haixiu");
        assert_eq!(kaomoji.candidates[0].source, CandidateSource::Kaomoji);
        assert_eq!(
            words(&query_kaomoji("hx", quanpin, &path, 10, &QUANPIN_ONLY))[0],
            "(*/ω\*)"
        );
        assert_eq!(
            words(&query_kaomoji("kiss", quanpin, &path, 10, &QUANPIN_ONLY))[0],
            "( ˘ ³˘)♥"
        );
        assert_eq!(
            words(&query_kaomoji("k", quanpin, &path, 10, &QUANPIN_ONLY)),
            ["( ˘ ³˘)♥", "single prefix"]
        );
    }

    /// test_local_modes.cpp:285-291, 311-314: the raw code and its Xiaohe normalisation are merged in catalog order.
    #[test]
    fn shuangpin_merges_both_prefixes() {
        let dir = tempfile::tempdir().unwrap();
        let path = fixture(dir.path());
        let xiaohe = crate::shuangpin::profile::profile(ShuangpinProfileKind::Xiaohe).unwrap();
        let emoji = query_emoji("xnlm", SchemeType::Shuangpin, &path, 10, xiaohe);
        assert_eq!(words(&emoji), ["😀", "😄", "raw shuangpin match"]);
        assert!(emoji.candidates.iter().all(|row| row.pinyin == "xnlm"));
        let kaomoji = query_kaomoji("hx", SchemeType::Shuangpin, &path, 10, xiaohe);
        assert_eq!(words(&kaomoji), ["(*/ω\*)", "(^_^)"]);
    }

    fn readings(list: &[&str]) -> Vec<String> {
        list.iter().map(|reading| (*reading).to_owned()).collect()
    }

    fn mixed_words(rows: &[ExpressiveRow]) -> Vec<&str> {
        rows.iter().map(|row| row.item.word.as_str()).collect()
    }

    /// 带目录表的混排夹具:`ji` 前缀下 🐔 的编码正好是 `ji`,但目录顺序排在二十行前缀命中的后面;🇺🇲、🇺🇸 的编码和次序与随包数据相同。
    fn mixed_fixture(dir: &Path) -> PathBuf {
        let path = dir.join("msime-others.db");
        let connection = Connection::open(&path).unwrap();
        connection
            .execute_batch(
                "CREATE TABLE emoji_pinyin(key TEXT,emoji TEXT,sort_order INTEGER);
                 CREATE TABLE emoji(emoji TEXT PRIMARY KEY,keywords TEXT);
                 INSERT INTO emoji_pinyin VALUES('meiguobentuwaixiaodaoyu','🇺🇲',1891),('meiguo','🇺🇸',1893),('ji','🐔',626),('jiba','🐔',626),('jitou','🐔',626),('jinggao','⚠️',1433);
                 INSERT INTO emoji VALUES('🇺🇲','美国本土外小岛屿 flag: u.s. outlying islands'),('🇺🇸','美国 美利坚 星条旗'),('🐔','鸡 鸡头 chicken'),('⚠️','警告 注意 危险');
                 CREATE TABLE kaomoji(pinyin TEXT,jianpin TEXT,kaomoji TEXT,sort_order INTEGER);
                 CREATE TABLE kaomoji_catalog(kaomoji TEXT PRIMARY KEY,keywords TEXT);
                 INSERT INTO kaomoji VALUES('haixiu','hx','(*/ω\*)',10),('kaixin','kx','(^_^)',20),('jinggao','jg','(゚Д゚≡゚д゚)!?',30);
                 INSERT INTO kaomoji_catalog VALUES('(*/ω\*)','hai xiu 害羞'),('(゚Д゚≡゚д゚)!?','jing gao 警告');",
            )
            .unwrap();
        for index in 0..20 {
            connection
                .execute(
                    "INSERT INTO emoji_pinyin VALUES(?1,?2,?3)",
                    rusqlite::params![format!("jiqi{index:02}"), format!("机{index:02}"), index],
                )
                .unwrap();
        }
        path
    }

    /// 混排的查询:读法按给定的先后查,读法内编码完全相同的在前、再按目录顺序;行带着目录里的关键词,行的 `pinyin` 是命中的读法。
    #[test]
    fn mixed_rows_put_exact_keys_first_and_carry_their_keywords() {
        let dir = tempfile::tempdir().unwrap();
        let path = mixed_fixture(dir.path());
        let all = |_: &str| true;
        // 🇺🇲 目录顺序在前,但只是前缀命中;🇺🇸 的编码就是 meiguo(#5907)。
        let flags = query_emoji_readings(&readings(&["meiguo"]), &path, &all);
        assert_eq!(mixed_words(&flags), ["🇺🇸", "🇺🇲"]);
        assert_eq!(flags[0].keywords, "美国 美利坚 星条旗");
        assert_eq!(
            flags[1].keywords,
            "美国本土外小岛屿 flag: u.s. outlying islands"
        );
        assert_eq!(flags[0].item.pinyin, "meiguo");
        assert_eq!(flags[0].item.source, CandidateSource::Emoji);
        assert_eq!((flags[0].item.weight, flags[1].item.weight), (2, 1));

        // ji 有二十行目录顺序更靠前的前缀命中,🐔 的编码完全相同,排第一;每种读法最多取 `MIXED_FETCH_PER_READING` 行。
        let ji = query_emoji_readings(&readings(&["ji"]), &path, &all);
        assert_eq!(ji[0].item.word, "🐔");
        assert_eq!(ji.len(), MIXED_FETCH_PER_READING);
        // 读法按给定的先后,跨读法按文字去重。
        let both = query_emoji_readings(&readings(&["meiguo", "ji", "jiba"]), &path, &all);
        assert_eq!(mixed_words(&both)[..3], ["🇺🇸", "🇺🇲", "🐔"]);
        assert_eq!(both[2].item.pinyin, "ji");
        assert_eq!(both.len(), 2 + MIXED_FETCH_PER_READING);
        assert!(query_emoji_readings(&[], &path, &all).is_empty());

        // 26 键按输入码查,输入码里有字母和 `'` 以外的字符时没有行。
        let quanpin = SchemeType::Quanpin;
        assert_eq!(
            query_mixed_emoji("MeiGuo", quanpin, &path, &QUANPIN_ONLY),
            flags
        );
        assert!(query_mixed_emoji("mei guo", quanpin, &path, &QUANPIN_ONLY).is_empty());

        let kaomoji = query_mixed_kaomoji("hx", quanpin, &path, &QUANPIN_ONLY);
        assert_eq!(mixed_words(&kaomoji), ["(*/ω\*)"]);
        assert_eq!(kaomoji[0].keywords, "hai xiu 害羞");
        assert_eq!(kaomoji[0].item.source, CandidateSource::Kaomoji);
        // 目录里没有的颜文字照样给,只是没有关键词。
        let kaixin = query_mixed_kaomoji("kaixin", quanpin, &path, &QUANPIN_ONLY);
        assert_eq!(mixed_words(&kaixin), ["(^_^)"]);
        assert_eq!(kaixin[0].keywords, "");
    }

    #[test]
    fn microsoft_semicolon_final_reaches_mixed_emoji_and_kaomoji() {
        let dir = tempfile::tempdir().unwrap();
        let path = mixed_fixture(dir.path());
        let microsoft =
            crate::shuangpin::profile::profile(ShuangpinProfileKind::Microsoft).unwrap();
        assert_eq!(normalize_input("j;gk", microsoft), "jinggao");
        assert_eq!(
            mixed_words(&query_mixed_emoji(
                "j;gk",
                SchemeType::Shuangpin,
                &path,
                microsoft
            )),
            ["⚠️"]
        );
        assert_eq!(
            mixed_words(&query_mixed_kaomoji(
                "j;gk",
                SchemeType::Shuangpin,
                &path,
                microsoft
            )),
            ["(゚Д゚≡゚д゚)!?"]
        );
        let xiaohe = crate::shuangpin::profile::profile(ShuangpinProfileKind::Xiaohe).unwrap();
        assert!(query_mixed_emoji("j;gk", SchemeType::Shuangpin, &path, xiaohe).is_empty());
        assert!(query_mixed_kaomoji("j;gk", SchemeType::Quanpin, &path, microsoft).is_empty());
    }

    #[test]
    fn mixed_quanpin_queries_do_not_allocate_temporary_readings() {
        let dir = tempfile::tempdir().unwrap();
        let path = mixed_fixture(dir.path());
        let readings = readings(&["meiguo"]);
        let expected = query_emoji_readings(&readings, &path, &|_| true);
        for (code, lowercase_allocations) in [("meiguo", 0), ("MeiGuo", 1)] {
            let mut minimum_allocations = usize::MAX;
            // 其他测试可能关闭全局连接缓存,取多次查询中已预热的分配数。
            for _ in 0..8 {
                let (rows, allocations) = crate::ime::personal_rerank::allocations::count(|| {
                    query_mixed_emoji(code, SchemeType::Quanpin, &path, &QUANPIN_ONLY)
                });
                assert_eq!(rows, expected);
                minimum_allocations = minimum_allocations.min(allocations);
            }
            // 两行的读音和词面各多一个 `SharedString` 的 `Arc`,比原来的 12 多 4 次。
            assert!(
                minimum_allocations <= 16 + lowercase_allocations,
                "混排查询不应复制读法列表:{code} 至少分配了 {minimum_allocations} 次"
            );
        }
    }

    #[test]
    fn mixed_readings_are_built_in_the_result_buffer() {
        let dir = tempfile::tempdir().unwrap();
        let path = mixed_fixture(dir.path());
        let connection = Connection::open(&path).unwrap();
        let load = || {
            read_readings(
                &connection,
                EMOJI.mixed_sql,
                std::iter::once("meiguo"),
                &|_| true,
                CandidateSource::Emoji,
            )
            .unwrap()
        };
        let expected = load();
        let (rows, allocations) = crate::ime::personal_rerank::allocations::count(load);
        assert_eq!(rows, expected);
        assert_eq!(mixed_words(&rows), ["🇺🇸", "🇺🇲"]);
        // 两行的读音和词面各多一个 `SharedString` 的 `Arc`,比原来的 11 多 4 次。
        assert!(allocations <= 15, "混排读取分配了 {allocations} 次");
    }

    #[test]
    fn empty_mixed_readings_do_not_reserve_result_page() {
        let dir = tempfile::tempdir().unwrap();
        let path = mixed_fixture(dir.path());
        let connection = Connection::open(path).unwrap();
        let rows = read_readings(
            &connection,
            EMOJI.mixed_sql,
            std::iter::once("missing"),
            &|_| true,
            CandidateSource::Emoji,
        )
        .unwrap();
        assert!(rows.is_empty());
        assert_eq!(rows.capacity(), 0);
    }

    /// 合计最多取 `MIXED_FETCH_LIMIT` 行。
    #[test]
    fn mixed_rows_stop_at_the_fetch_limit() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("msime-others.db");
        let connection = Connection::open(&path).unwrap();
        connection
            .execute_batch("CREATE TABLE emoji_pinyin(key TEXT,emoji TEXT,sort_order INTEGER);")
            .unwrap();
        let mut keys = Vec::new();
        for reading in 0..8 {
            let key = format!("k{}", char::from(b'a' + reading));
            for index in 0..MIXED_FETCH_PER_READING {
                connection
                    .execute(
                        "INSERT INTO emoji_pinyin VALUES(?1,?2,?3)",
                        rusqlite::params![key, format!("{key}{index}"), index as i64],
                    )
                    .unwrap();
            }
            keys.push(key);
        }
        let rows = query_emoji_readings(&keys, &path, &|_| true);
        assert_eq!(rows.len(), MIXED_FETCH_LIMIT);
    }

    /// 资源里只有编码表、没有目录表时行照样给,关键词为空;`accept` 核对的是命中的那一列编码,不通过的行跳过,不占名额。
    #[test]
    fn mixed_rows_without_catalog_tables_are_checked_against_the_matched_key() {
        let dir = tempfile::tempdir().unwrap();
        let path = fixture(dir.path());
        let all = |_: &str| true;
        let emoji = query_emoji_readings(&readings(&["laugh", "xiao"]), &path, &all);
        assert_eq!(mixed_words(&emoji), ["😀", "😄"]);
        assert_eq!(emoji[0].item.pinyin, "laugh");
        assert_eq!(emoji[1].item.pinyin, "xiao");
        assert!(emoji.iter().all(|row| row.keywords.is_empty()));

        let not_xiaolian = |key: &str| key != "xiaolian";
        let emoji = query_emoji_readings(&readings(&["xiao"]), &path, &not_xiaolian);
        // `xiaolian` 的两行被拒,`xiao'lian` 的 😄 还在。
        assert_eq!(mixed_words(&emoji), ["😄"]);

        // 颜文字的 `jianpin` 列命中时核对 `jianpin`。
        let only_jianpin = |key: &str| key == "hx";
        let kaomoji = query_kaomoji_readings(&readings(&["hx"]), &path, &only_jianpin);
        assert_eq!(mixed_words(&kaomoji), ["(*/ω\*)", "(^_^)"]);
        let none = |_: &str| false;
        assert!(query_kaomoji_readings(&readings(&["hx"]), &path, &none).is_empty());

        // 资源打不开时没有行,混排不报诊断。
        let missing = dir.path().join("private-others-missing.db");
        assert!(query_emoji_readings(&readings(&["xiao"]), &missing, &all).is_empty());
    }

    /// test_local_modes.cpp:319-331.
    #[test]
    fn missing_and_corrupt_databases_have_private_diagnostics() {
        let dir = tempfile::tempdir().unwrap();
        let missing = dir.path().join("private-others-missing.db");
        let result = query_emoji(
            "privatecode",
            SchemeType::Quanpin,
            &missing,
            10,
            &QUANPIN_ONLY,
        );
        assert!(result.candidates.is_empty());
        assert_eq!(
            result.diagnostic.as_deref(),
            Some(diagnostics::EMOJI_UNAVAILABLE)
        );
        let result = query_kaomoji(
            "privatecode",
            SchemeType::Quanpin,
            &missing,
            10,
            &QUANPIN_ONLY,
        );
        assert_eq!(
            result.diagnostic.as_deref(),
            Some(diagnostics::KAOMOJI_UNAVAILABLE)
        );

        let corrupt = dir.path().join("private-corrupt.db");
        std::fs::write(&corrupt, "not a sqlite database").unwrap();
        let result = query_kaomoji(
            "privatecode",
            SchemeType::Quanpin,
            &corrupt,
            10,
            &QUANPIN_ONLY,
        );
        assert!(result.candidates.is_empty());
        assert_eq!(
            result.diagnostic.as_deref(),
            Some(diagnostics::KAOMOJI_QUERY_FAILED)
        );
        let result = query_emoji(
            "privatecode",
            SchemeType::Quanpin,
            &corrupt,
            10,
            &QUANPIN_ONLY,
        );
        assert_eq!(
            result.diagnostic.as_deref(),
            Some(diagnostics::EMOJI_QUERY_FAILED)
        );
    }
}