答案:下面是两个分词算法,一个是二元分词,一个是词典分词:
<?php
function _tokenizer ($text)
{
// UTF8_only
// 2-Base Cut
$len = strlen ($text);
$mbc = '';
$last_mbc = '';
$tmp = '';
$tokens = array ();
for ($i = 0; $i < $len; $i++) {
$c = $text[$i];
$v = ord ($c);
if ($v > 0xe0) {
// 3-bytes chars
$tmp = '';
$mbc = $c . $text[$i + 1] . $text[$i + 2];
$i += 2;
}
elseif ($v > 0xc0) {
// 2-bytes chars
$tmp = '';
$mbc = $c . $text[$i + 1];
$i ++;
}
else {
$mbc = '';
if ($c == ' ') {
if ($tmp) {
$p = $i - strlen ($tmp);
$tokens[$p] = $tmp;
}
$tmp = '';
}
else {
$tmp .= $c;
}
}
if ($mbc) {
if ($last_mbc) {
$p = $i - strlen ($last_mbc . $mbc) + 1;
$tokens[$p] = $last_mbc . $mbc;
}
$last_mbc = $mbc;
}
else {
$last_mbc = '';
}
}
return $tokens;
}
function _tokenizer_dict ($text, 上一个:走进全文搜索(PHP+SQLite) 三
下一个:走进全文搜索(PHP+SQLite) 一