Ejemplo n.º 1
0
 /**
  * @param string $rule
  * @return string
  * @throws Exceptions\ParserException
  */
 public function parse($rule)
 {
     $this->output = '';
     $this->operator = \null;
     $this->values = \null;
     $this->operatorRequired = \false;
     foreach (new AST($this->tokenizer->tokenize($rule), $this->variables) as $token) {
         switch ($token->getGroup()) {
             case Constants::GROUP_VALUE:
                 $this->assignVariableValueFromToken($token);
                 break;
             case Constants::GROUP_LOGICAL:
                 $this->assignLogicalToken($token);
                 continue 2;
             case Constants::GROUP_PARENTHESES:
                 $this->assignParentheses($token);
                 continue 2;
             case Constants::GROUP_OPERATOR:
                 $this->assignOperator($token);
                 continue 2;
             case Constants::GROUP_COMMENT:
             case Constants::GROUP_SPACE:
                 continue 2;
             default:
                 throw new Exceptions\ParserException(sprintf('Unknown token "%s" at position %d on line %d', $token->getValue(), $token->getPosition(), $token->getLine()));
         }
         $this->parseExpression();
     }
     $this->assertSyntaxSeemsOkay();
     return $this->output;
 }
Ejemplo n.º 2
0
 /**
  * Classifies a text and returns the probability (score) per label
  *
  * @param  string $text
  * @return array
  */
 public function classify($text)
 {
     $totalDocCount = array_sum($this->docs);
     $tokens = $this->tokenizer->tokenize($text);
     $scores = array();
     foreach ($this->labels as $label => $labelCount) {
         $logSum = 0;
         $docCount = $this->docs[$label];
         $inversedDocCount = $totalDocCount - $docCount;
         if (0 === $inversedDocCount) {
             continue;
         }
         foreach ($tokens as $token) {
             $totalTokenCount = isset($this->tokens[$token]) ? $this->tokens[$token] : 0;
             if (0 === $totalTokenCount) {
                 continue;
             }
             $tokenCount = isset($this->data[$label][$token]) ? $this->data[$label][$token] : 0;
             $inversedTokenCount = $this->inversedTokenCount($token, $label);
             $tokenProbabilityPositive = $tokenCount / $docCount;
             $tokenProbabilityNegative = $inversedTokenCount / $inversedDocCount;
             $probability = $tokenProbabilityPositive / ($tokenProbabilityPositive + $tokenProbabilityNegative);
             $probability = (1 * 0.5 + $totalTokenCount * $probability) / (1 + $totalTokenCount);
             if (0 === $probability) {
                 $probability = 0.01;
             } elseif (1 === $probability) {
                 $probability = 0.99;
             }
             $logSum += log(1 - $probability) - log($probability);
         }
         $scores[$label] = 1 / (1 + exp($logSum));
     }
     arsort($scores, SORT_NUMERIC);
     return $scores;
 }
Ejemplo n.º 3
0
 /**
  * @param string $string
  * @return string
  * @throws Exceptions\HighlighterException
  */
 public function highlightString($string)
 {
     try {
         $tokens = $this->tokenizer->tokenize($string);
     } catch (Exceptions\TokenizerException $e) {
         throw new Exceptions\HighlighterException('Unable to highlight string', 0, $e);
     }
     return $this->highlightTokens($tokens);
 }
Ejemplo n.º 4
0
 /**
  * @param string $class
  * @param string $regex
  * @param int    $priority
  */
 public function registerToken($class, $regex, $priority = null)
 {
     $this->tokenizer->registerToken($class, $regex, $priority);
 }