diff --git a/instant-segment/src/lib.rs b/instant-segment/src/lib.rs index 0c10cdb..13e1a3a 100644 --- a/instant-segment/src/lib.rs +++ b/instant-segment/src/lib.rs @@ -77,13 +77,17 @@ impl Segmenter { &self, input: &str, search: &'a mut Search, - ) -> Result, InvalidCharacter> { + ) -> Result, InvalidCharacter> { let state = SegmentState::new(Ascii::new(input)?, self, search); - if !input.is_empty() { - state.run(); - } + let score = match input { + "" => 0.0, + _ => state.run(), + }; - Ok(search.result.iter().map(|v| v.as_str())) + Ok(Segments { + iter: search.result.iter(), + score, + }) } /// Returns the sentence's score @@ -142,6 +146,32 @@ impl Segmenter { } } +pub struct Segments<'a> { + iter: std::slice::Iter<'a, String>, + score: f64, +} + +impl Segments<'_> { + /// Returns the score of the segmented text + pub fn score(&self) -> f64 { + self.score + } +} + +impl<'a> Iterator for Segments<'a> { + type Item = &'a str; + + fn next(&mut self) -> Option { + self.iter.next().map(|v| v.as_str()) + } +} + +impl ExactSizeIterator for Segments<'_> { + fn len(&self) -> usize { + self.iter.len() + } +} + struct SegmentState<'a> { data: &'a Segmenter, text: Ascii<'a>, diff --git a/instant-segment/src/test_cases.rs b/instant-segment/src/test_cases.rs index 8fbf207..20a3a11 100644 --- a/instant-segment/src/test_cases.rs +++ b/instant-segment/src/test_cases.rs @@ -3,7 +3,9 @@ use crate::{Search, Segmenter}; /// Run a segmenter against the built-in test cases pub fn run(segmenter: &Segmenter) { let mut search = Search::default(); - assert_eq!(segmenter.segment("", &mut search).unwrap().len(), 0); + let segments = segmenter.segment("", &mut search).unwrap(); + assert_eq!(segments.len(), 0); + assert_eq!(segments.score(), 0.0); let mut success = true; for test in TEST_CASES.iter().copied() {