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
use super::*;
use crate::ime::personal_rerank::allocations::count;
use crate::quanpin::fixture::Fixture;

// 用固定回写层隔离解析投影收益;真实生产回写与容量由 online_cache_update_tests 独立对照。

// 固定 67f70bc3e690e2d57f4523f704d57a9cd214faea 的完整在线回写、首字母展开和解析方法,只改名、转接旧解析并移除注释。
impl QuanpinDictionary {
    fn legacy_slot_insert_online_words(
        &mut self,
        raw: &str,
        segmentation: &str,
        autocorrect_types: u32,
        words: &[String],
        source: CandidateSource,
    ) -> bool {
        if raw.is_empty() || words.is_empty() {
            return false;
        }
        let segments = self.resolve_segments(raw, segmentation);
        let resolution =
            self.legacy_slot_resolution(raw, segmentation, &segments, autocorrect_types);
        let key = self.series_slot(&resolution.cache_key);
        let mut list = self.series_cache.get(&key).unwrap_or_default();
        if !replace_online_candidate_batch(&mut list, raw, words, source) {
            return false;
        }
        self.series_cache.insert(key, list);
        true
    }

    fn legacy_slot_expand_initial_candidates(
        &mut self,
        raw: &str,
        segmentation: &str,
        autocorrect_types: u32,
        code: &str,
        candidates: &mut Vec<WordItem>,
    ) -> bool {
        if code.len() != 1 {
            return false;
        }
        let is_limited_initial =
            |item: &WordItem| item.source == CandidateSource::Database && item.pinyin == code;
        let limited_count = candidates
            .iter()
            .filter(|item| is_limited_initial(item))
            .count();
        if limited_count != INITIAL_CANDIDATE_LIMIT {
            return false;
        }
        let expanded = matched_items(self.database.query_initial(code, UNLIMITED_ROWS), code);
        if expanded.len() <= limited_count {
            return false;
        }
        let mut merged = Vec::with_capacity(candidates.len() - limited_count + expanded.len());
        let mut inserted = false;
        for item in candidates.drain(..) {
            if is_limited_initial(&item) {
                if !inserted {
                    merged.extend(expanded.iter().cloned());
                    inserted = true;
                }
                continue;
            }
            merged.push(item);
        }
        *candidates = merged;
        self.cache.insert(code.to_string(), expanded.into());
        let segments = self.resolve_segments(raw, segmentation);
        let resolution =
            self.legacy_slot_resolution(raw, segmentation, &segments, autocorrect_types);
        let key = self.series_slot(&series_cache_key(
            raw,
            &resolution.segmentation,
            autocorrect_types,
        ));
        self.series_cache.insert(key, candidates.clone());
        true
    }

    fn legacy_slot_resolution(
        &mut self,
        raw: &str,
        segmentation: &str,
        segments: &[String],
        types: u32,
    ) -> SeriesResolution {
        let hash = resolution_cache_hash(types, raw, segmentation);
        if let Some(cached) = self.resolution_cache.get_ref(&hash) {
            if resolution_cache_key_matches(&cached.key, types, raw, segmentation) {
                return cached.resolution.clone();
            }
        }
        let resolution = resolve_series_query(raw, segments, types);
        let key = format!("{types}\u{1f}{raw}\u{1f}{segmentation}");
        self.resolution_cache.insert(
            hash,
            CachedResolution {
                key,
                resolution: resolution.clone(),
            },
        );
        resolution
    }
}

fn fixture() -> Fixture {
    let fixture = Fixture::new();
    for index in 0..32 {
        fixture.insert("ni", &format!("合成首字母{index:02}"), 1000 - index);
    }
    for (key, word) in [
        ("ni'hao", "合成整词"),
        ("shang", "合成换位"),
        ("quan'li", "合成删首"),
        ("nve", "合成别名"),
        ("gua", "合成甲"),
        ("gai", "合成乙"),
    ] {
        fixture.insert(key, word, 100);
    }
    fixture
}

fn query(
    dictionary: &mut QuanpinDictionary,
    raw: &str,
    segmentation: &str,
    types: u32,
) -> Vec<WordItem> {
    dictionary.query(raw, segmentation, types, FuzzyPinyinOptions { rules: 0 })
}

fn clone_cost(
    dictionary: &mut QuanpinDictionary,
    raw: &str,
    segmentation: &str,
    types: u32,
) -> (SeriesResolution, usize, usize) {
    let (segments, segment_copies) = count(|| dictionary.resolve_segments(raw, segmentation));
    let (resolution, copies) =
        count(|| dictionary.legacy_slot_resolution(raw, segmentation, &segments, types));
    (resolution, copies, segment_copies)
}

fn assert_layout(actual: &SeriesResolution, expected: &SeriesResolution) {
    assert_eq!(actual, expected);
    assert_eq!(
        actual.segmentation.capacity(),
        expected.segmentation.capacity()
    );
    assert_eq!(actual.cache_key.capacity(), expected.cache_key.capacity());
    assert_eq!(actual.corrected.capacity(), expected.corrected.capacity());
    for (actual, expected) in actual.corrected.iter().zip(&expected.corrected) {
        assert_eq!(actual.capacity(), expected.capacity());
    }
    for (actual, expected) in [
        (
            &actual.alternative_corrected_cuts,
            &expected.alternative_corrected_cuts,
        ),
        (
            &actual.costlier_corrected_cuts,
            &expected.costlier_corrected_cuts,
        ),
    ] {
        assert_eq!(actual.capacity(), expected.capacity());
        for (actual, expected) in actual.iter().zip(expected) {
            assert_eq!(actual.capacity(), expected.capacity());
            for (actual, expected) in actual.iter().zip(expected) {
                assert_eq!(actual.capacity(), expected.capacity());
            }
        }
    }
}

fn assert_slot(
    before: &QuanpinDictionary,
    after: &QuanpinDictionary,
    raw: &str,
    segmentation: &str,
    types: u32,
) {
    let hash = resolution_cache_hash(types, raw, segmentation);
    let expected = before.resolution_cache.get_ref(&hash).unwrap();
    let actual = after.resolution_cache.get_ref(&hash).unwrap();
    assert_eq!(actual.key, expected.key);
    assert_eq!(actual.key.capacity(), expected.key.capacity());
    assert_layout(&actual.resolution, &expected.resolution);
    let slot = after.series_slot(&actual.resolution.cache_key);
    let actual = after.series_cache.get_ref(&slot);
    let expected = before.series_cache.get_ref(&slot);
    assert_eq!(actual, expected);
    if let (Some(actual), Some(expected)) = (actual, expected) {
        assert_eq!(actual.capacity(), expected.capacity());
    }
}

#[test]
fn online_updates_copy_only_the_hot_resolution_key() {
    let fixture = fixture();
    let transposition = autocorrect_type::TRANSPOSITION;
    let neighbor = autocorrect_type::NEIGHBOR;
    let deletion = autocorrect_type::DELETION;
    let words = vec!["合成云甲".to_owned()];
    for (raw, segmentation, types) in [
        ("nihao", "ni'hao", 0),
        ("nihao", "", 0),
        ("ni'hao", "ni'hao", 0),
        ("nue", "nue", 0),
        ("nue", "", 0),
        ("gau", "", transposition | neighbor),
        ("sahng", "", transposition),
        ("uanli", "", deletion),
    ] {
        for source in [
            CandidateSource::CloudSuggestion,
            CandidateSource::AiSuggestion,
        ] {
            for empty_segments_cache in [false, true] {
                let mut before = QuanpinDictionary::new(&fixture.paths);
                let mut after = QuanpinDictionary::new(&fixture.paths);
                assert_eq!(
                    query(&mut after, raw, segmentation, types),
                    query(&mut before, raw, segmentation, types)
                );
                if empty_segments_cache {
                    before.segmentation_cache.clear();
                    after.segmentation_cache.clear();
                }
                let (resolution, copies, segment_copies) =
                    clone_cost(&mut before, raw, segmentation, types);
                if empty_segments_cache {
                    before.segmentation_cache.clear();
                }
                assert!(copies > 1);
                let (expected, old_count) = count(|| {
                    before.legacy_slot_insert_online_words(raw, segmentation, types, &words, source)
                });
                let (actual, new_count) = count(|| {
                    after.frozen_insert_online_words(raw, segmentation, types, &words, source)
                });
                assert_eq!(actual, expected);
                assert!(actual);
                assert_slot(&before, &after, raw, segmentation, types);
                println!("在线回写 {raw}/{types}/{source:?}:申请 {old_count}→{new_count},完整解析复制 {copies},音节构造 {segment_copies},空切分缓存={empty_segments_cache}");
                assert_eq!(
                    new_count + copies - 1 + segment_copies,
                    old_count,
                    "只需复制一个拥有型缓存键,不构造未消费的音节"
                );
                if empty_segments_cache && segmentation.is_empty() {
                    assert!(after.segmentation_cache.get_ref_by(raw).is_none());
                }
                assert_eq!(
                    query(&mut after, raw, segmentation, types),
                    query(&mut before, raw, segmentation, types)
                );
                assert!(after
                    .series_cache
                    .get_ref(&after.series_slot(&resolution.cache_key))
                    .unwrap()
                    .iter()
                    .any(|item| item.word == "合成云甲"));
            }
        }
    }
}

#[test]
fn cold_online_updates_keep_the_original_allocation_and_capacity_policy() {
    let fixture = fixture();
    let words = vec!["合成冷回写".to_owned()];
    for (raw, segmentation, types) in [
        ("nihao", "ni'hao", 0),
        ("nihao", "", 0),
        ("nue", "", 0),
        (
            "gau",
            "",
            autocorrect_type::TRANSPOSITION | autocorrect_type::NEIGHBOR,
        ),
        ("uanli", "", autocorrect_type::DELETION),
    ] {
        // 先初始化进程共享的纠错表,再用两个新词典比较各自尚未命中的解析缓存。
        let mut warm = QuanpinDictionary::new(&fixture.paths);
        assert!(warm.legacy_slot_insert_online_words(
            raw,
            segmentation,
            types,
            &words,
            CandidateSource::CloudSuggestion
        ));
        let mut before = QuanpinDictionary::new(&fixture.paths);
        let mut after = QuanpinDictionary::new(&fixture.paths);
        let (expected, old_count) = count(|| {
            before.legacy_slot_insert_online_words(
                raw,
                segmentation,
                types,
                &words,
                CandidateSource::CloudSuggestion,
            )
        });
        let (actual, new_count) = count(|| {
            after.frozen_insert_online_words(
                raw,
                segmentation,
                types,
                &words,
                CandidateSource::CloudSuggestion,
            )
        });
        assert_eq!(actual, expected);
        assert_eq!(new_count, old_count, "冷解析不得多复制一次结果或键");
        assert_slot(&before, &after, raw, segmentation, types);
    }
}

#[test]
fn initial_expansion_projects_the_unmarked_slot_and_keeps_cold_cost() {
    let fixture = fixture();
    for (raw, segmentation, types) in [
        ("n", "n", 0),
        ("n", "", 0),
        ("sahng", "", autocorrect_type::TRANSPOSITION),
        (
            "gau",
            "",
            autocorrect_type::TRANSPOSITION | autocorrect_type::NEIGHBOR,
        ),
    ] {
        for hot in [false, true] {
            for empty_segments_cache in [false, true] {
                let mut before = QuanpinDictionary::new(&fixture.paths);
                let mut after = QuanpinDictionary::new(&fixture.paths);
                let mut expected = query(&mut before, "n", "n", 0);
                let mut actual = query(&mut after, "n", "n", 0);
                let _ = before.database.query_initial("n", UNLIMITED_ROWS);
                let _ = after.database.query_initial("n", UNLIMITED_ROWS);
                let segments = before.resolve_segments(raw, segmentation);
                let _ = after.resolve_segments(raw, segmentation);
                let resolution = before.legacy_slot_resolution(raw, segmentation, &segments, types);
                if raw == "gau" {
                    assert!(resolution.corrected_input);
                    assert!(resolution.cache_key.starts_with("C:"));
                }
                let segments = after.resolve_segments(raw, segmentation);
                let _ = after.legacy_slot_resolution(raw, segmentation, &segments, types);
                if empty_segments_cache {
                    before.segmentation_cache.clear();
                    after.segmentation_cache.clear();
                }
                let (_, copies, segment_copies) = clone_cost(&mut before, raw, segmentation, types);
                if empty_segments_cache {
                    before.segmentation_cache.clear();
                }
                if !hot {
                    before.resolution_cache.clear();
                    after.resolution_cache.clear();
                }
                let (old_result, old_count) = count(|| {
                    before.legacy_slot_expand_initial_candidates(
                        raw,
                        segmentation,
                        types,
                        "n",
                        &mut expected,
                    )
                });
                let (new_result, new_count) = count(|| {
                    after.expand_initial_candidates(raw, segmentation, types, "n", &mut actual)
                });
                assert_eq!(new_result, old_result);
                assert!(new_result);
                assert_eq!(actual, expected);
                assert_eq!(actual.capacity(), expected.capacity());
                assert_slot(&before, &after, raw, segmentation, types);
                let key =
                    after.series_slot(&series_cache_key(raw, &resolution.segmentation, types));
                assert!(!key.starts_with("C:"));
                assert_eq!(after.series_cache.get_ref(&key), Some(&actual));
                assert_eq!(before.series_cache.get_ref(&key), Some(&expected));
                println!("首字母回写 {raw}/{hot}:申请 {old_count}→{new_count},音节构造 {segment_copies},空切分缓存={empty_segments_cache}");
                assert_eq!(
                    new_count + if hot { copies + segment_copies } else { 0 },
                    old_count
                );
                if hot && empty_segments_cache && segmentation.is_empty() {
                    assert!(after.segmentation_cache.get_ref_by(raw).is_none());
                }
            }
        }
    }
}

#[test]
fn projection_keeps_collisions_fifo_rejected_batches_and_independent_outputs() {
    let fixture = fixture();
    let mut before = QuanpinDictionary::new(&fixture.paths);
    let mut after = QuanpinDictionary::new(&fixture.paths);
    before.resolution_cache = FifoCache::new(2);
    after.resolution_cache = FifoCache::new(2);
    let words = vec!["合成云行".to_owned()];
    for raw in ["ni", "hao", "ni", "ma"] {
        assert_eq!(
            before.legacy_slot_insert_online_words(
                raw,
                raw,
                0,
                &words,
                CandidateSource::CloudSuggestion
            ),
            after.frozen_insert_online_words(raw, raw, 0, &words, CandidateSource::CloudSuggestion)
        );
        assert_slot(&before, &after, raw, raw, 0);
    }
    let oldest = resolution_cache_hash(0, "ni", "ni");
    assert!(before.resolution_cache.get_ref(&oldest).is_none());
    assert!(after.resolution_cache.get_ref(&oldest).is_none());
    for dictionary in [&mut before, &mut after] {
        dictionary.resolution_cache.insert(
            resolution_cache_hash(0, "nihao", "ni'hao"),
            CachedResolution {
                key: "0\u{1f}wrong\u{1f}ni'hao".to_owned(),
                resolution: SeriesResolution {
                    segmentation: "错误切分".to_owned(),
                    cache_key: "错误槽".to_owned(),
                    ..Default::default()
                },
            },
        );
    }
    assert_eq!(
        before.legacy_slot_insert_online_words(
            "nihao",
            "ni'hao",
            0,
            &words,
            CandidateSource::CloudSuggestion
        ),
        after.frozen_insert_online_words(
            "nihao",
            "ni'hao",
            0,
            &words,
            CandidateSource::CloudSuggestion
        )
    );
    assert_slot(&before, &after, "nihao", "ni'hao", 0);
    for (raw, words, source) in [
        (
            "",
            vec!["合成空键".to_owned()],
            CandidateSource::CloudSuggestion,
        ),
        ("nihao", vec![], CandidateSource::AiSuggestion),
        (
            "nihao",
            vec!["坏\u{7}".to_owned()],
            CandidateSource::AiSuggestion,
        ),
        (
            "nihao",
            vec!["合成错误来源".to_owned()],
            CandidateSource::Database,
        ),
    ] {
        let expected = before.legacy_slot_insert_online_words(raw, "ni'hao", 0, &words, source);
        let actual = after.frozen_insert_online_words(raw, "ni'hao", 0, &words, source);
        assert_eq!(actual, expected);
        assert!(!actual);
    }
    let mut owned = query(&mut after, "nihao", "ni'hao", 0);
    assert_eq!(owned, query(&mut before, "nihao", "ni'hao", 0));
    owned[0].word.push_str("修改副本");
    assert_ne!(owned, query(&mut after, "nihao", "ni'hao", 0));
    after.reset_cache();
    drop(after);
    assert!(owned[0].word.ends_with("修改副本"));
}

#[test]
fn hot_rejected_batches_skip_segments_without_changing_cached_answers() {
    let fixture = fixture();
    for (words, source) in [
        (vec!["坏\u{7}".to_owned()], CandidateSource::AiSuggestion),
        (vec!["合成错误来源".to_owned()], CandidateSource::Database),
    ] {
        let mut before = QuanpinDictionary::new(&fixture.paths);
        let mut after = QuanpinDictionary::new(&fixture.paths);
        assert_eq!(
            query(&mut after, "nihao", "", 0),
            query(&mut before, "nihao", "", 0)
        );
        before.segmentation_cache.clear();
        after.segmentation_cache.clear();
        let (_, copies, segment_copies) = clone_cost(&mut before, "nihao", "", 0);
        before.segmentation_cache.clear();
        let (expected, old_count) =
            count(|| before.legacy_slot_insert_online_words("nihao", "", 0, &words, source));
        let (actual, new_count) =
            count(|| after.frozen_insert_online_words("nihao", "", 0, &words, source));
        assert_eq!(actual, expected);
        assert!(!actual);
        assert_eq!(new_count + copies - 1 + segment_copies, old_count);
        assert!(after.segmentation_cache.get_ref_by("nihao").is_none());
        assert_slot(&before, &after, "nihao", "", 0);
        assert_eq!(
            query(&mut after, "nihao", "", 0),
            query(&mut before, "nihao", "", 0)
        );
    }
}