Skip to content

Commit

Permalink
Improve floating-point precision
Browse files Browse the repository at this point in the history
  • Loading branch information
tabuna committed May 7, 2024
1 parent 962e776 commit 2f2edfe
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/Classifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,24 @@ 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
{
$words = $this->tokenize($statement);

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);
});
}

Expand Down Expand Up @@ -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)
{
Expand All @@ -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();
}

/**
Expand Down

0 comments on commit 2f2edfe

Please sign in to comment.