diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-06-21 22:45:28 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-06-21 22:45:28 +0800 |
| commit | c6885ae2312a1be589e945129bdd9d268f14c370 (patch) | |
| tree | 919e0fe49331b533de312a39548f17b133ef4975 /src/post_proc | |
| parent | 291c01fd98d44e42bb0a65468e3b6cee3dc25044 (diff) | |
feat: replace `--pinyin` flag with generic `--post="+pinyin"` option
Diffstat (limited to 'src/post_proc')
| -rw-r--r-- | src/post_proc/pinyin.rs | 31 | ||||
| -rw-r--r-- | src/post_proc/trait.rs | 12 |
2 files changed, 43 insertions, 0 deletions
diff --git a/src/post_proc/pinyin.rs b/src/post_proc/pinyin.rs new file mode 100644 index 0000000..c06b040 --- /dev/null +++ b/src/post_proc/pinyin.rs @@ -0,0 +1,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(" ") + } +} diff --git a/src/post_proc/trait.rs b/src/post_proc/trait.rs new file mode 100644 index 0000000..7fced5b --- /dev/null +++ b/src/post_proc/trait.rs @@ -0,0 +1,12 @@ +/// A text post-processor that transforms transcribed text. +pub trait TextPostProcesser { + /// Name used in `--post="+name(args)"`. + fn processer_name() -> &'static str; + + /// Execute the post-processing. + /// + /// # Arguments + /// * `param` — arguments parsed from `--post="+name(arg1, arg2)"` + /// * `text` — the transcribed text to process + fn process(param: &[&str], text: &str) -> String; +} |
