py: yield score from segment() method

This commit is contained in:
Dirkjan Ochtman 2024-12-11 10:43:03 +01:00
parent 78b37cf7c2
commit 7d29eb27e1
2 changed files with 6 additions and 4 deletions

View File

@ -90,11 +90,12 @@ impl Segmenter {
/// iterate over the `Search` to get the resulting words.
///
/// For best performance, reusing `Search` objects is recommended.
fn segment(&self, s: &str, search: &mut Search) -> PyResult<()> {
fn segment(&self, s: &str, search: &mut Search) -> PyResult<f64> {
match self.inner.segment(s, &mut search.inner) {
Ok(_) => {
Ok(iter) => {
let score = iter.score();
search.cur = Some(0);
Ok(())
Ok(score)
}
Err(_) => Err(PyValueError::new_err(
"only lowercase ASCII letters allowed",

View File

@ -16,7 +16,8 @@ def bigrams():
def main():
segmenter = instant_segment.Segmenter(unigrams(), bigrams())
search = instant_segment.Search()
segmenter.segment('thisisatest', search)
score = segmenter.segment('thisisatest', search)
print(f"{score=}")
print([word for word in search])
if __name__ == '__main__':