From 37fad7cd305e5cd48e7203632fb3796d3bfab9e6 Mon Sep 17 00:00:00 2001 From: Kan-Ru Chen Date: Sat, 29 Jun 2024 20:59:07 +0900 Subject: [PATCH 1/2] fix: Hanyu Pinyin empty rime handling --- src/editor/zhuyin_layout/pinyin.rs | 63 +++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/src/editor/zhuyin_layout/pinyin.rs b/src/editor/zhuyin_layout/pinyin.rs index f5e7f8ee6..60b2e053c 100644 --- a/src/editor/zhuyin_layout/pinyin.rs +++ b/src/editor/zhuyin_layout/pinyin.rs @@ -180,7 +180,13 @@ impl SyllableEditor for Pinyin { let mut medial = fina.and_then(|f| f.medial); let mut rime = fina.and_then(|f| f.rime); - if let Some(Bopomofo::I) = rime { + /* Hanyu empty rime + * ㄓ/ㄔ/ㄕ/ㄖ/ㄗ/ㄘ/ㄙ + -i, -i is empty rime, not ㄧ + * */ + if matches!( + (medial, rime), + (Some(Bopomofo::I), None) | (None, Some(Bopomofo::I)) + ) { match initial { Some(Bopomofo::ZH) | Some(Bopomofo::CH) | Some(Bopomofo::SH) | Some(Bopomofo::R) | Some(Bopomofo::Z) | Some(Bopomofo::C) | Some(Bopomofo::S) => { @@ -191,6 +197,11 @@ impl SyllableEditor for Pinyin { } } + /* Hanyu uan/un/u : + * ㄐ/ㄑ/ㄒ + -uan, -uan is ㄩㄢ, not ㄨㄢ + * ㄐ/ㄑ/ㄒ + -un, -un is ㄩㄣ, not ㄨㄣ + * ㄐ/ㄑ/ㄒ + -u, -u is ㄧ, not ㄨ + */ match initial { Some(Bopomofo::J) | Some(Bopomofo::Q) | Some(Bopomofo::X) => { match (medial, rime) { @@ -205,6 +216,13 @@ impl SyllableEditor for Pinyin { _ => (), } + /* THL/MPS2 s/sh/c/ch/j : + * s- + ー/ㄩ, s- is ㄒ, not ㄙ (THL/Tongyong) + * sh- + ー/ㄩ, sh- is ㄒ, not ㄕ (MPS2) + * c- + ー/ㄩ, c- is ㄑ, not ㄘ (Tongyong) + * ch- + ㄧ/ㄩ, ch- is ㄑ, not ㄔ (THL) + * j- + other than ー/ㄩ, j- is ㄓ, not ㄐ (MPS2) + */ match medial { Some(Bopomofo::I) | Some(Bopomofo::IU) => { match initial { @@ -224,6 +242,10 @@ impl SyllableEditor for Pinyin { } } + /* THL supplemental set + * ㄅ/ㄆ/ㄇ/ㄈ + -ㄨㄥ, -ㄨㄥ is another reading of -ㄥ + * ㄅ/ㄆ/ㄇ/ㄈ + -ㄨㄛ, -ㄨㄛ is another reading of -ㄛ + */ match initial { Some(Bopomofo::B) | Some(Bopomofo::P) | Some(Bopomofo::M) | Some(Bopomofo::F) => { match (medial, rime) { @@ -505,3 +527,42 @@ mod table { fin!("z", None, None), ]; } + +#[cfg(test)] +mod tests { + use crate::{ + editor::{ + keyboard::{AnyKeyboardLayout, KeyCode, KeyboardLayout}, + zhuyin_layout::SyllableEditor, + }, + syl, + zhuyin::Bopomofo, + }; + + use super::Pinyin; + + #[test] + fn hanyu_empty_rime_zi() { + let keyboard = AnyKeyboardLayout::qwerty(); + let mut hanyu = Pinyin::hanyu(); + + hanyu.key_press(keyboard.map(KeyCode::Z)); + hanyu.key_press(keyboard.map(KeyCode::I)); + hanyu.key_press(keyboard.map(KeyCode::N1)); + + assert_eq!(syl![Bopomofo::Z], hanyu.read()); + } + + #[test] + fn hanyu_empty_rime_zhi() { + let keyboard = AnyKeyboardLayout::qwerty(); + let mut hanyu = Pinyin::hanyu(); + + hanyu.key_press(keyboard.map(KeyCode::Z)); + hanyu.key_press(keyboard.map(KeyCode::H)); + hanyu.key_press(keyboard.map(KeyCode::I)); + hanyu.key_press(keyboard.map(KeyCode::N1)); + + assert_eq!(syl![Bopomofo::ZH], hanyu.read()); + } +} From e9517b29e20361f5eb203d4e18b9df3bebdefb47 Mon Sep 17 00:00:00 2001 From: Kan-Ru Chen Date: Sat, 29 Jun 2024 21:30:42 +0900 Subject: [PATCH 2/2] test: Add hanyu uan/un/u test --- src/editor/zhuyin_layout/pinyin.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/editor/zhuyin_layout/pinyin.rs b/src/editor/zhuyin_layout/pinyin.rs index 60b2e053c..9351ee7cd 100644 --- a/src/editor/zhuyin_layout/pinyin.rs +++ b/src/editor/zhuyin_layout/pinyin.rs @@ -565,4 +565,33 @@ mod tests { assert_eq!(syl![Bopomofo::ZH], hanyu.read()); } + + #[test] + fn hanyu_uan_un_u() { + let keyboard = AnyKeyboardLayout::qwerty(); + let mut hanyu = Pinyin::hanyu(); + + hanyu.key_press(keyboard.map(KeyCode::J)); + hanyu.key_press(keyboard.map(KeyCode::U)); + hanyu.key_press(keyboard.map(KeyCode::A)); + hanyu.key_press(keyboard.map(KeyCode::N)); + hanyu.key_press(keyboard.map(KeyCode::N1)); + + assert_eq!(syl![Bopomofo::J, Bopomofo::IU, Bopomofo::AN], hanyu.read()); + + hanyu.clear(); + hanyu.key_press(keyboard.map(KeyCode::Q)); + hanyu.key_press(keyboard.map(KeyCode::U)); + hanyu.key_press(keyboard.map(KeyCode::N)); + hanyu.key_press(keyboard.map(KeyCode::N1)); + + assert_eq!(syl![Bopomofo::Q, Bopomofo::IU, Bopomofo::EN], hanyu.read()); + + hanyu.clear(); + hanyu.key_press(keyboard.map(KeyCode::X)); + hanyu.key_press(keyboard.map(KeyCode::U)); + hanyu.key_press(keyboard.map(KeyCode::N1)); + + assert_eq!(syl![Bopomofo::X, Bopomofo::IU], hanyu.read()); + } }