Maison > développement back-end > tutoriel php > 最大正向匹配算法 PHP实现

最大正向匹配算法 PHP实现

WBOY
Libérer: 2016-07-29 08:56:43
original
1413 Les gens l'ont consulté
<pre class="brush:php;toolbar:false"><span>字符串最大正向匹配算法说明可以参考其他文章,这里只做实现。</span>
Copier après la connexion
<span>三个参数:查询词 $query,词典$</span><span>dict</span><span>,最大长度,这里设置为15</span>
Copier après la connexion
<span>词典示例:</span>
Copier après la connexion
<pre class="brush:php;toolbar:false"><span>$dict = </span><span>array</span><span>(
</span><span>'测试字符'</span><span>=></span><span>'</span><span>测试字符</span><span>'</span><span>,</span><span>'测试'</span><span>=></span><span>'测试'</span><span>,</span><span>)</span>
Copier après la connexion

<span>public function </span><span>extractWords(</span><span>$query</span><span>,</span><span>$</span><span>dict</span><span>,</span><span>$max_len</span><span>=</span><span>15</span><span>){</span><span><em></em></span><span>$feature </span><span>= </span><span>""</span><span>;
</span><span>$slen</span><span>=</span><span><em>mb_strlen</em></span><span>(</span><span>$query</span><span>,</span><span>'UTF8'</span><span>);
</span><span>$c_bg </span><span>= </span><span>0</span><span>;
</span><span>while</span><span>(</span><span>$c_bg</span><span><span>$slen</span><span>){
</span><span>$matched </span><span>= </span><span>false</span><span>;
</span><span>$c_len </span><span>=((</span><span>$slen</span><span>-</span><span>$c_bg</span><span>)></span><span>$max_len</span><span>)?</span><span>$max_len</span><span>:(</span><span>$slen</span><span>-</span><span>$c_bg</span><span>);
</span><span>$t_str </span><span>= </span><span><em>mb_substr</em></span><span>(</span><span>$query</span><span>, </span><span>$c_bg</span><span>,</span><span>$c_len</span><span>,</span><span>'UTF8'</span><span>);
</span><span>for</span><span>(</span><span>$i</span><span>=</span><span>$c_len</span><span>;</span><span>$i</span><span>></span><span>1</span><span>;</span><span>$i</span><span>--){
</span><span>$ttts </span><span>= </span><span><em>mb_substr</em></span><span>(</span><span>$t_str</span><span>, </span><span>0</span><span>,</span><span>$i</span><span>,</span><span>'UTF8'</span><span>);</span><span>if</span><span>(!</span><span>empty</span><span>(</span><span>$dict</span><span>[</span><span>$ttts</span><span>])){
</span><span><em>//                    echo 'matched = '.$ttts.PHP_EOL;
</em></span><span><em></em></span><span>$matched </span><span>= </span><span>true</span><span>;
</span><span>$c_bg </span><span>+= </span><span>$i</span><span>;
</span><span>if</span><span>(!</span><span>empty</span><span>(</span><span>$feature</span><span>)){
</span><span>$feature</span><span>.=</span><span>","</span><span>;
</span><span>                        }
</span><span>$feature</span><span>.=</span><span>$ttts</span><span>;
</span><span>break</span><span>;
</span><span>                    }</span><span>            }
</span><span>if</span><span>(!</span><span>$matched</span><span>){
</span><span>$c_bg</span><span>++;
</span><span>            }
</span><span>        }
</span><span><em>        echo $feature.PHP_EOL;</em></span><span>    }</span></span>
Copier après la connexion

以上就介绍了最大正向匹配算法 PHP实现,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal