Esempio n. 1
0
 /**
  * Classify a string using the results of a sampling process.
  *
  * @param string $string String to classify
  *
  * @return Classifier\Result
  */
 public function classify($string)
 {
     $stemedString = $this->stemer->process($string);
     $words = explode(' ', $string);
     $positiveProbs = array();
     $negativeProbs = array();
     foreach ($words as $word) {
         $probs = $this->resultOb->getWordProbability($word);
         if ($probs['p'] != 0) {
             $positiveProbs[] = $probs['p'];
         }
         if ($probs['n'] != 0) {
             $negativeProbs[] = $probs['n'];
         }
     }
     if (count($positiveProbs) > 0) {
         $posProbs = $this->calculateProbability($positiveProbs);
     } else {
         $posProbs = 0;
     }
     if (count($negativeProbs) > 0) {
         $negProbs = $this->calculateProbability($negativeProbs);
     } else {
         $negProbs = 0;
     }
     return new Classifier\Result($string, $posProbs, $negProbs);
 }
Esempio n. 2
0
 /**
  * Counts the words in a text sample.
  *
  * @param array $sample An array of text samples.
  *
  * @return array
  */
 private function wordCountSample(array $sample)
 {
     foreach ($sample as $string) {
         $stemedString = $this->stemer->process($string);
         $this->counter->addToSample($stemedString);
     }
     $counts = $this->counter->getWordCounts();
     $this->counter->reset();
     return $counts;
 }