diff --git a/src/Classifier.php b/src/Classifier.php index fd3cb7c..d7b5c74 100644 --- a/src/Classifier.php +++ b/src/Classifier.php @@ -81,7 +81,7 @@ public function learn(string $statement, string $type): self } /** - * Guesses the type of a given statement using Naive Bayes classification. + * Guesses the type of given statement using Naive Bayes classification. */ public function guess(string $statement): Collection { @@ -89,16 +89,16 @@ public function guess(string $statement): Collection return collect($this->documents) ->map(function ($count, string $type) use ($words) { - $likelihood = $this->pTotal($type); + $likelihood = BigDecimal::of($this->pTotal($type)); foreach ($words as $word) { - $likelihood *= $this->p($word, $type); + $likelihood = $likelihood->multipliedBy($this->p($word, $type)); } - return (string) BigDecimal::of($likelihood); + return $likelihood; }) ->sort(function ($a, $b) { - return BigDecimal::of($a)->compareTo($b); + return $a->compareTo($b); }); } @@ -151,7 +151,7 @@ private function incrementWord(string $type, string $word): void * @param string $word The word to calculate probability for. * @param string $type The type to calculate probability in. * - * @return int The calculated probability. + * @return \Brick\Math\BigInteger The calculated probability. */ private function p(string $word, string $type) { @@ -163,8 +163,7 @@ private function p(string $word, string $type) return BigDecimal::of($count) ->dividedBy(array_sum($this->words[$type]), PHP_INT_SIZE, RoundingMode::HALF_UP) - ->getUnscaledValue() - ->toInt(); + ->getUnscaledValue(); } /**