Skip to content

Commit

Permalink
Merge pull request #7 from fieg/removed-error-suppression
Browse files Browse the repository at this point in the history
Removed error suppression
  • Loading branch information
fieg committed Jun 2, 2016
2 parents 22210f4 + ff287a4 commit fb90b5b
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/Fieg/Bayes/Classifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,26 @@ public function train($label, $text)
{
$tokens = $this->tokenizer->tokenize($text);

if (!isset($this->labels[$label])) {
$this->labels[$label] = 0;
$this->data[$label] = [];
$this->docs[$label] = 0;
}

foreach ($tokens as $token) {
@$this->labels[$label]++;
@$this->tokens[$token]++;
@$this->data[$label][$token]++;
if (!isset($this->tokens[$token])) {
$this->tokens[$token] = 0;
}
if (!isset($this->data[$label][$token])) {
$this->data[$label][$token] = 0;
}

$this->labels[$label]++;
$this->tokens[$token]++;
$this->data[$label][$token]++;
}

@$this->docs[$label]++;
$this->docs[$label]++;
}

/**
Expand All @@ -83,21 +96,21 @@ public function classify($text)
foreach ($this->labels as $label => $labelCount) {
$logSum = 0;

$docCount = intval(@$this->docs[$label]);
$docCount = $this->docs[$label];
$inversedDocCount = $totalDocCount - $docCount;

if (0 === $inversedDocCount) {
continue;
}

foreach ($tokens as $token) {
$totalTokenCount = intval(@$this->tokens[$token]);
$totalTokenCount = isset($this->tokens[$token]) ? $this->tokens[$token] : 0;

if (0 === $totalTokenCount) {
continue;
}

$tokenCount = intval(@$this->data[$label][$token]);
$tokenCount = isset($this->data[$label][$token]) ? $this->data[$label][$token] : 0;
$inversedTokenCount = $this->inversedTokenCount($token, $label);

$tokenProbabilityPositive = $tokenCount / $docCount;
Expand Down Expand Up @@ -146,7 +159,7 @@ protected function inversedTokenCount($token, $label)

$totalTokenCount = $this->tokens[$token];

$totalLabelTokenCount = intval(@$data[$label][$token]);
$totalLabelTokenCount = isset($data[$label][$token]) ? $data[$label][$token] : 0;

$retval = $totalTokenCount - $totalLabelTokenCount;

Expand Down

0 comments on commit fb90b5b

Please sign in to comment.