From c571996925ae3abe3ba45930b1d6a9702eee9f52 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Mon, 7 Dec 2020 14:24:33 +0100 Subject: [PATCH] Simplify bigram scoring algorithm --- src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a934b8f..ca2d76b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,12 +42,12 @@ impl Segmenter { fn score(&self, word: &str, previous: Option<&str>) -> f64 { if let Some(prev) = previous { - if let Some(pb) = self.bigrams.get(&(prev.into(), word.into())) { - if self.unigrams.get(prev).is_some() { + if let Some(bi) = self.bigrams.get(&(prev.into(), word.into())) { + if let Some(uni) = self.unigrams.get(prev) { // Conditional probability of the word given the previous // word. The technical name is "stupid backoff" and it's // not a probability distribution but it works well in practice. - return pb / self.total / self.score(prev, None); + return (bi / self.total) / (uni / self.total); } } }