use std::io::{stdin, BufRead};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use sapi_lite::stt::{EventfulContext, Recognizer, Rule};
const INSTRUCTIONS: &'static str = r#"
Choose a text and read it out loud. For example:
"I don't know half of you half as well as I should like;
and I like less than half of you half as well as you deserve."
When you're done, press ENTER to see how many times you said the word "half".
"#;
fn main() {
sapi_lite::initialize().unwrap();
println!("{}", INSTRUCTIONS);
let counter = Arc::new(AtomicUsize::new(0));
count_halves(&counter);
let count = counter.load(Ordering::Relaxed);
println!(
"You said the word \"half\" exactly {} {}.",
count,
if count == 1 { "time" } else { "times" }
);
sapi_lite::finalize();
}
fn count_halves(counter: &Arc<AtomicUsize>) {
let handler = {
let counter = counter.clone();
move |_| {
counter.fetch_add(1, Ordering::Relaxed);
}
};
let recog = Recognizer::new().unwrap();
let ctx = EventfulContext::new(&recog, handler).unwrap();
let grammar = ctx
.grammar_builder()
.add_rule(&Rule::text("half"))
.build()
.unwrap();
grammar.set_enabled(true).unwrap();
stdin().lock().lines().next();
}