mirror of
https://github.com/instant-labs/instant-segment.git
synced 2025-01-19 15:29:05 +00:00
Expose score via concrete iterator type
This commit is contained in:
parent
0102a81fc8
commit
78b37cf7c2
@ -77,13 +77,17 @@ impl Segmenter {
|
||||
&self,
|
||||
input: &str,
|
||||
search: &'a mut Search,
|
||||
) -> Result<impl ExactSizeIterator<Item = &'a str>, InvalidCharacter> {
|
||||
) -> Result<Segments<'a>, 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::Item> {
|
||||
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>,
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user