Skip to content

Commit

Permalink
Changed SpamDetector
Browse files Browse the repository at this point in the history
  • Loading branch information
tabuna committed Mar 20, 2024
1 parent c5f9092 commit 801d769
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions app/Services/SpamDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ public function checkByClassifier(): bool
$classifier = new Classifier();

// Train the classifier with spam and ham messages
$this->trainClassifier($classifier, 'classifiers/spam.json', static::SPAM);
$this->trainClassifier($classifier, 'classifiers/ham.json', static::HAM);
$this
->trainClassifier($classifier, 'classifiers/spam.json', static::SPAM)
->trainClassifier($classifier, 'classifiers/ham.json', static::HAM);

return $classifier->guess($this->message) === static::SPAM;
}
Expand All @@ -87,14 +88,17 @@ public function checkByClassifier(): bool
* @param string $fileName The path to the JSON file containing messages
* @param string $label The label to assign to the messages (spam or ham)
*
* @return void
* @return self
*/
private function trainClassifier(Classifier $classifier, string $fileName, string $label): void
private function trainClassifier(Classifier $classifier, string $fileName, string $label): self
{
$messages = json_decode(file_get_contents(storage_path($fileName)));

foreach ($messages as $message) {
$classifier->learn($message, $label);
}

return $this;
}

/**
Expand Down

0 comments on commit 801d769

Please sign in to comment.