public function classify($document)
{
$results = array();
if ($this->documentNormalizer) {
$document = $this->documentNormalizer->normalize($document);
}
$tokens = $this->tokenizer->tokenize($document);
if ($this->tokenNormalizer) {
$tokens = $this->tokenNormalizer->normalize($tokens);
}
$tokens = array_count_values($tokens);
$weights = $this->preparedModel()->getModel();
foreach (array_keys($weights) as $category) {
$results[$category] = 0;
foreach ($tokens as $token => $count) {
if (array_key_exists($token, $weights[$category])) {
$results[$category] += $count * $weights[$category][$token];
}
}
}
asort($results, SORT_NUMERIC);
$category = key($results);
$value = array_shift($results);
if ($value === array_shift($results)) {
return false;
} else {
return $category;
}
}
<?php require_once __DIR__ . '/../vendor/autoload.php'; // Using a plain data array source for simplicity use Camspiers\StatisticalClassifier\DataSource\DataArray; use Camspiers\StatisticalClassifier\Classifier\ComplementNaiveBayes; $source = new DataArray(array(array('category' => 'spam', 'document' => 'Some spam document'), array('category' => 'spam', 'document' => 'Another spam document'), array('category' => 'ham', 'document' => 'Some ham document'), array('category' => 'ham', 'document' => 'Another ham document'))); $source->addDocument('spam', 'Another spam document'); $source->addDocument('ham', 'Another ham document'); $c = new ComplementNaiveBayes($source); echo $c->classify("Some ham document"), PHP_EOL;