diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-06-20 20:11:52 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-06-20 20:11:52 +0800 |
| commit | dbeac25149b52a6a069ad29dd75204c8953b365c (patch) | |
| tree | 51deb16eafde53273b6b89f4a0da06c87982c4f5 /src | |
| parent | 90595bbed2b0fdfda45980dc7ebff8c4eb11dcbd (diff) | |
feat: add pinyin conversion for Chinese output
Diffstat (limited to 'src')
| -rw-r--r-- | src/args.rs | 14 | ||||
| -rw-r--r-- | src/main.rs | 12 |
2 files changed, 21 insertions, 5 deletions
diff --git a/src/args.rs b/src/args.rs index 679d8e1..11a1f11 100644 --- a/src/args.rs +++ b/src/args.rs @@ -117,6 +117,10 @@ pub struct DMVOPArguments { require_equals = true )] pub subnet_mask: String, + + // Convert text output to pinyin (Chinese romanization) + #[arg(long = "pinyin")] + pub use_pinyin: bool, } #[derive(Clone, Debug)] @@ -178,6 +182,16 @@ pub fn format_output(pattern: &str, word: &str, confidence: f32, volume: f32) -> result } +/// Convert transcribed text to pinyin if the `use_pinyin` flag is set. +/// Otherwise returns the original text as a String. +pub fn maybe_to_pinyin(text: &str, use_pinyin: bool) -> String { + if use_pinyin { + pinyin::to_pinyin_vec(text, pinyin::Pinyin::plain).join(" ") + } else { + text.to_string() + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/main.rs b/src/main.rs index fabf16c..e3a5b84 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,7 +31,7 @@ impl OutputChannel { } } - async fn send(&self, message: &str) { + async fn send_to_channel(&self, message: &str) { match self { OutputChannel::Stdout(p) => p.clone().send(message).await, OutputChannel::Stderr(p) => p.clone().send(message).await, @@ -252,17 +252,19 @@ async fn main() { match rx.recv().await { Ok(event) => match event { vtx_engine::EngineEvent::TranscriptionComplete(result) => { - let formatted = format_output(&pattern, &result.text, 0.0, last_volume_db); + let raw = maybe_to_pinyin(&result.text, args.use_pinyin); + let formatted = format_output(&pattern, &raw, 0.0, last_volume_db); for ch in &channels { - ch.send(&formatted).await; + ch.send_to_channel(&formatted).await; } } vtx_engine::EngineEvent::TranscriptionSegment(segment) => { - let formatted = format_output(&pattern, &segment.text, 0.0, last_volume_db); + let raw = maybe_to_pinyin(&segment.text, args.use_pinyin); + let formatted = format_output(&pattern, &raw, 0.0, last_volume_db); for ch in &channels { - ch.send(&formatted).await; + ch.send_to_channel(&formatted).await; } } vtx_engine::EngineEvent::VisualizationData(viz) => { |
