use svara::phoneme::Phoneme;
use super::PronunciationDict;
pub struct LookupStream<'d, I> {
dict: &'d PronunciationDict,
words: I,
}
impl<'d, I, S> Iterator for LookupStream<'d, I>
where
I: Iterator<Item = S>,
S: AsRef<str>,
{
type Item = (S, Option<&'d [Phoneme]>);
fn next(&mut self) -> Option<Self::Item> {
let word = self.words.next()?;
let phonemes = self.dict.lookup(word.as_ref());
Some((word, phonemes))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.words.size_hint()
}
}
impl PronunciationDict {
pub fn lookup_stream<I, S>(&self, words: I) -> LookupStream<'_, I::IntoIter>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
LookupStream {
dict: self,
words: words.into_iter(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_stream_basic() {
let dict = PronunciationDict::english_minimal();
let results: alloc::vec::Vec<_> = dict
.lookup_stream(["hello", "world"].iter().copied())
.collect();
assert_eq!(results.len(), 2);
assert!(results[0].1.is_some());
assert!(results[1].1.is_some());
}
#[test]
fn test_stream_with_miss() {
let dict = PronunciationDict::english_minimal();
let results: alloc::vec::Vec<_> = dict
.lookup_stream(["hello", "xyzzy"].iter().copied())
.collect();
assert!(results[0].1.is_some());
assert!(results[1].1.is_none());
}
#[test]
fn test_stream_empty() {
let dict = PronunciationDict::english_minimal();
let empty: &[&str] = &[];
let results: alloc::vec::Vec<_> = dict.lookup_stream(empty.iter().copied()).collect();
assert!(results.is_empty());
}
#[test]
fn test_stream_with_strings() {
let dict = PronunciationDict::english_minimal();
let words = alloc::vec![
alloc::string::String::from("hello"),
alloc::string::String::from("the"),
];
let results: alloc::vec::Vec<_> = dict.lookup_stream(words.iter()).collect();
assert_eq!(results.len(), 2);
assert!(results[0].1.is_some());
assert!(results[1].1.is_some());
}
#[test]
fn test_stream_preserves_words() {
let dict = PronunciationDict::english_minimal();
let results: alloc::vec::Vec<_> = dict
.lookup_stream(["hello", "world"].iter().copied())
.collect();
assert_eq!(results[0].0, "hello");
assert_eq!(results[1].0, "world");
}
#[test]
fn test_stream_size_hint() {
let dict = PronunciationDict::english_minimal();
let stream = dict.lookup_stream(["a", "b", "c"].iter().copied());
assert_eq!(stream.size_hint(), (3, Some(3)));
}
}