aboutsummaryrefslogtreecommitdiff
path: root/src/post_proc/pinyin.rs
blob: c06b040f91b6e1c982059c9ba210d87edad695e6 (plain) (blame)
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
use crate::post_proc::TextPostProcesser;

/// Converts Chinese characters to pinyin (romanized phonetic representation).
///
/// # Styles
/// - `plain` — ni hao (default)
/// - `tone` — nǐ hǎo (with tone marks, requires `with_tone` feature)
/// - `tone-num` — ni3 hao3 (tone number at end)
/// - `first-letter` — n h (first letter only)
pub struct PinyinPostProcesser;

impl TextPostProcesser for PinyinPostProcesser {
    fn processer_name() -> &'static str {
        "pinyin"
    }

    fn process(param: &[&str], text: &str) -> String {
        let style = param.first().copied().unwrap_or("plain");
        let result: Vec<&'static str> = match style {
            "tone" => pinyin::to_pinyin_vec(text, pinyin::Pinyin::with_tone),
            "tone-num" | "tone_num" => {
                pinyin::to_pinyin_vec(text, pinyin::Pinyin::with_tone_num_end)
            }
            "first-letter" | "first_letter" => {
                pinyin::to_pinyin_vec(text, pinyin::Pinyin::first_letter)
            }
            _ => pinyin::to_pinyin_vec(text, pinyin::Pinyin::plain),
        };
        result.join(" ")
    }
}