简单的中文分词算法,二元分词的PHP代码:
$str = '苏杭,人间的天堂paradise!'; //$str = iconv('GB2312','UTF-8',$str); $result = spStr($str); print_r($result); /** * UTF-8版 中文二元分词 */ function spStr($str) { $cstr = array(); $search = array(",", "/", "\\", ".", ";", ":", "\"", "!", "~", "`", "^", "(", ")", "?", "-", "\t", "\n", "'", "<", ">", "\r", "\r\n", "{1}quot;", "&", "%", "#", "@", "+", "=", "{", "}", "[", "]", ":", ")", "(", ".", "。", ",", "!", ";", "“", "”", "‘", "’", "[", "]", "、", "—", " ", "《", "》", "-", "…", "【", "】",); $str = str_replace($search, " ", $str); preg_match_all("/[a-zA-Z]+/", $str, $estr); preg_match_all("/[0-9]+/", $str, $nstr); $str = preg_replace("/[0-9a-zA-Z]+/", " ", $str); $str = preg_replace("/\s{2,}/", " ", $str); $str = explode(" ", trim($str)); foreach ($str as $s) { $l = strlen($s); $bf = null; for ($i= 0; $i< $l; $i=$i+3) { $ns1 = $s{$i}.$s{$i+1}.$s{$i+2}; if (isset($s{$i+3})) { $ns2 = $s{$i+3}.$s{$i+4}.$s{$i+5}; if (preg_match("/[\x80-\xff]{3}/",$ns2)) $cstr[] = $ns1.$ns2; } else if ($i == 0) { $cstr[] = $ns1; } } } $estr = isset($estr[0])?$estr[0]:array(); $nstr = isset($nstr[0])?$nstr[0]:array(); return array_merge($nstr,$estr,$cstr); }
"苏杭,人间的天堂paradise“经过二元分词之后,转变为:
Array ( [0] => paradise [1] => 苏杭 [2] => 人间 [3] => 间的 [4] => 的天 [5] => 天堂 )接下来,是将以上结果转换为单字节的,可以用md5,base64,sha1等,但是用这些转换后的字符都太长,占用太多的存储空间,我们可以用区位码来表示汉字,汉字转区位码的PHP代码:
foreach ($result as $s) { $s = iconv('UTF-8','GB2312',$s); $code[] = gbCode($s); } $code = implode(" ", $code); echo $code; function gbCode($str) { $return = null; if (!preg_match("/^[\x80-\xff]{2,}$/",$str)) return $str; $len = strlen($str); for ($i= 0; $i< $len; $i=$i+2) { $return .= sprintf("%02d%02d",ord($str{$i})-160,ord($str{$i+1})-160); } return $return; }
paradise 43532628 40432868 28682136 21364476 44764435
最后将得到的结果入库,插入全文索引表,结果应该插入索引表的fulltext索引字段。