use super::VowelClassifier;
#[derive(Debug, Clone, Copy, Default)]
pub struct HanziClassifier;
impl HanziClassifier {
pub fn new() -> Self {
Self
}
}
impl VowelClassifier for HanziClassifier {
fn is_vowel(&self, c: char) -> bool {
let code = c as u32;
match code {
0x0041 | 0x0045 | 0x0049 | 0x004F | 0x0055 => true, 0x0061 | 0x0065 | 0x0069 | 0x006F | 0x0075 => true,
0x0101 | 0x00E1 | 0x01CE | 0x00E0 => true, 0x0113 | 0x00E9 | 0x011B | 0x00E8 => true, 0x012B | 0x00ED | 0x01D0 | 0x00EC => true, 0x014D | 0x00F3 | 0x01D2 | 0x00F2 => true, 0x016B | 0x00FA | 0x01D4 | 0x00F9 => true, 0x01D6 | 0x01D8 | 0x01DA | 0x01DC | 0x00FC => true,
0x4E00..=0x9FFF => false, 0x3400..=0x4DBF => false, 0x20000..=0x2A6DF => false, 0x2A700..=0x2B73F => false, 0x2B740..=0x2B81F => false, 0x2B820..=0x2CEAF => false, 0x2CEB0..=0x2EBEF => false, 0x30000..=0x3134F => false, 0xF900..=0xFAFF => false,
_ => false,
}
}
fn script_name(&self) -> &'static str {
"Hanzi"
}
fn is_consonant(&self, c: char) -> bool {
let code = c as u32;
match code {
0x0042..=0x0044
| 0x0046..=0x0048
| 0x004A..=0x004E
| 0x0050..=0x0054
| 0x0056..=0x005A => true, 0x0062..=0x0064
| 0x0066..=0x0068
| 0x006A..=0x006E
| 0x0070..=0x0074
| 0x0076..=0x007A => true,
0x4E00..=0x9FFF => true, 0x3400..=0x4DBF => true, 0x20000..=0x2A6DF => true,
_ => false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cjk_ideographs_not_vowels() {
let c = HanziClassifier::new();
assert!(!c.is_vowel('我')); assert!(!c.is_vowel('你')); assert!(!c.is_vowel('好')); assert!(!c.is_vowel('中')); assert!(!c.is_vowel('国')); }
#[test]
fn test_pinyin_vowels() {
let c = HanziClassifier::new();
assert!(c.is_vowel('a'));
assert!(c.is_vowel('e'));
assert!(c.is_vowel('i'));
assert!(c.is_vowel('o'));
assert!(c.is_vowel('u'));
}
#[test]
fn test_pinyin_tone_vowels() {
let c = HanziClassifier::new();
assert!(c.is_vowel('ā')); assert!(c.is_vowel('á')); assert!(c.is_vowel('ǎ')); assert!(c.is_vowel('à')); assert!(c.is_vowel('ü')); }
#[test]
fn test_pinyin_consonants() {
let c = HanziClassifier::new();
assert!(c.is_consonant('b'));
assert!(c.is_consonant('p'));
assert!(c.is_consonant('m'));
assert!(c.is_consonant('f'));
assert!(c.is_consonant('z'));
assert!(c.is_consonant('c'));
assert!(c.is_consonant('s'));
}
#[test]
fn test_cjk_as_consonant() {
let c = HanziClassifier::new();
assert!(c.is_consonant('我'));
assert!(c.is_consonant('你'));
assert!(c.is_consonant('他'));
}
}