add transcription structs

This commit is contained in:
Andrey Tkachenko 2020-08-19 17:54:09 +04:00
parent 2920b61828
commit 7ec77f7fac
5 changed files with 38 additions and 13 deletions

View File

@ -10,10 +10,12 @@ build = "build.rs"
[dependencies]
cblas = "0.2.0"
openblas-src = { version = "0.9.0", features = ["cache", "static"] }
serde = {version = "1.0", features = ["derive"]}
serde_json = "1.0"
[build-dependencies]
bindgen = "0.54.1"
cc = { version = "1.0.58", features = ["parallel"] }
bindgen = "0.54"
cc = { version = "1.0", features = ["parallel"] }
[dev-dependencies]
audrey = "0.2.0"
audrey = "0.2"

View File

@ -40,13 +40,13 @@ pub fn main() {
println!("feed {}", buff.len());
if model.feed(&mut sess, buff.as_slice()) {
println!("{}", model.get_result(&mut sess));
println!("{:?}", model.get_result(&mut sess));
} else {
println!("{}", model.get_partial_result(&mut sess));
println!("{:?}", model.get_partial_result(&mut sess));
}
}
println!("{}", model.get_final_result(&mut sess));
println!("{:?}", model.get_final_result(&mut sess));
// let audio_buf :Vec<_> = if desc.sample_rate() == SAMPLE_RATE {
// .map(|s| s.unwrap()).collect()

View File

@ -17,7 +17,9 @@ mod ffi {
mod model;
mod session;
mod speaker;
mod transcription;
pub use model::VoskModel;
pub use session::{VoskSession, VoskSessionConfig, VoskSessionConfigBuilder};
pub use speaker::SpeakerModel;
pub use speaker::SpeakerModel;
pub use transcription::{TranscriptionResult, TranscriptionPartialResult, TranscriptionWord};

View File

@ -28,24 +28,24 @@ impl VoskModel {
}
#[inline]
pub fn get_result(&self, sess: &mut VoskSession) -> String {
pub fn get_result(&self, sess: &mut VoskSession) -> crate::TranscriptionResult {
let cstr = unsafe { CStr::from_ptr(ffi::vosk_recognizer_result(sess.inner)) };
cstr.to_string_lossy().to_owned().to_string()
serde_json::from_str(cstr.to_str().unwrap()).unwrap()
}
#[inline]
pub fn get_partial_result(&self, sess: &mut VoskSession) -> String {
pub fn get_partial_result(&self, sess: &mut VoskSession) -> crate::TranscriptionPartialResult {
let cstr = unsafe { CStr::from_ptr(ffi::vosk_recognizer_partial_result(sess.inner)) };
cstr.to_string_lossy().to_owned().to_string()
serde_json::from_str(cstr.to_str().unwrap()).unwrap()
}
#[inline]
pub fn get_final_result(&self, sess: &mut VoskSession) -> String {
pub fn get_final_result(&self, sess: &mut VoskSession) -> crate::TranscriptionResult {
let cstr = unsafe { CStr::from_ptr(ffi::vosk_recognizer_final_result(sess.inner)) };
cstr.to_string_lossy().to_owned().to_string()
serde_json::from_str(cstr.to_str().unwrap()).unwrap()
}
}

21
src/transcription.rs Normal file
View File

@ -0,0 +1,21 @@
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct TranscriptionWord {
pub conf: f32,
pub end: f32,
pub start: f32,
pub word: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct TranscriptionResult {
pub text: String,
#[serde(default = "Vec::new")]
pub result: Vec<TranscriptionWord>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct TranscriptionPartialResult {
pub partial: String,
}