当前位置: 首页>>代码示例>>PHP>>正文


PHP Rule::addPattern方法代码示例

本文整理汇总了PHP中Rule::addPattern方法的典型用法代码示例。如果您正苦于以下问题:PHP Rule::addPattern方法的具体用法?PHP Rule::addPattern怎么用?PHP Rule::addPattern使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Rule的用法示例。


在下文中一共展示了Rule::addPattern方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: getRules

 /**
  * @inheritdoc
  */
 public function getRules()
 {
     $rules = new Rules();
     if (!array_key_exists('rules', $this->data)) {
         throw new ConfigurationException(".INI Configuration must contain 'rules' section.");
     }
     foreach ($this->data['rules'] as $patternStr => $probability) {
         $pattern = new RulePattern((int) $probability);
         $startToken = '';
         foreach (explode(' ', $patternStr) as $tokenStr) {
             if (strlen($tokenStr) <= 2) {
                 throw new ConfigurationException("Pattern {$patternStr}: Token {$tokenStr} must exceed 2 characters.");
             }
             $isStartToken = false;
             if ($tokenStr[0] === '<' && substr($tokenStr, -1) === '>') {
                 $isStartToken = true;
                 $tokenStr = substr($tokenStr, 1, -1);
                 $startToken = $tokenStr;
             }
             $token = new RulePatternToken($tokenStr, $isStartToken);
             $pattern->addToken($token);
         }
         if (empty($startToken)) {
             throw new ConfigurationException("Pattern {$patternStr}: Must contain start token.");
         }
         $rule = new Rule($startToken);
         $rule->addPattern($pattern);
         $rules->addRule($rule);
     }
     return $rules;
 }
开发者ID:bigwhoop,项目名称:sentence-breaker,代码行数:34,代码来源:IniConfiguration.php

示例2: getRules

 /**
  * @inheritdoc
  */
 public function getRules()
 {
     $data = $this->data;
     $rules = new Rules();
     foreach ($data->rule as $ruleData) {
         $rule = new Rule((string) $ruleData->token);
         $patternIdx = 0;
         foreach ($ruleData->patterns->pattern as $patternData) {
             $patternName = "Pattern {$rule->getTokenName()}#{$patternIdx}";
             $hasStartToken = false;
             $potentialStartTokenIdxs = [];
             $tokens = [];
             foreach ($patternData->token as $tokenData) {
                 $tokenName = (string) $tokenData;
                 $isStartToken = (bool) $tokenData['is_start_token'];
                 $tokens[] = ['name' => $tokenName, 'is_start_token' => $isStartToken];
                 if ($isStartToken) {
                     if ($tokenName !== $rule->getTokenName()) {
                         throw new ConfigurationException("{$patternName}: Only {$rule->getTokenName()} tokens can have the 'is_start_token' attribute.");
                     }
                     if ($hasStartToken) {
                         throw new ConfigurationException("{$patternName}: Multiple {$rule->getTokenName()} tokens with 'is_start_token' attribute found. Only one is allowed.");
                     } else {
                         $hasStartToken = true;
                     }
                 }
                 if ($tokenName === $rule->getTokenName()) {
                     $potentialStartTokenIdxs[] = count($tokens) - 1;
                 }
             }
             if (!$hasStartToken) {
                 if (count($potentialStartTokenIdxs) === 0) {
                     throw new ConfigurationException("Pattern {$rule->getTokenName()}/#{$patternIdx} must have unambiguous start token. No {$rule->getTokenName()} token found.");
                 } elseif (count($potentialStartTokenIdxs) > 1) {
                     throw new ConfigurationException("Pattern {$rule->getTokenName()}/#{$patternIdx} must have unambiguous start token. Multiple {$rule->getTokenName()} tokens found.");
                 } else {
                     $potentialStartTokenIdx = $potentialStartTokenIdxs[0];
                     $tokens[$potentialStartTokenIdx]['is_start_token'] = true;
                 }
             }
             $pattern = new RulePattern((int) $patternData['probability']);
             foreach ($tokens as $token) {
                 $pattern->addToken(new RulePatternToken($token['name'], $token['is_start_token']));
             }
             $rule->addPattern($pattern);
             $patternIdx++;
         }
         $rules->addRule($rule);
     }
     return $rules;
 }
开发者ID:bigwhoop,项目名称:sentence-breaker,代码行数:54,代码来源:XMLConfiguration.php


注:本文中的Rule::addPattern方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。