在进行分词的时候,我采用了 Zend_Search_Lucene 如何支持中文检索 这篇文章中提到的二元分词法。真心感谢无私风险的网络英雄们。我在使用的过程中遇到了一点小问题,最后进行了修正,过程如下:
我们来看看这段程序:
$stopWords = array('a', 'an', 'at', 'the', 'and', 'or', 'is', 'am');
$stopWordsFilter = new Zend_Search_Lucene_Analysis_TokenFilter_StopWords($stopWords); $analyzer = new Phpbean_Lucene_Analyzer();
$cnStopWords = array('的');
$analyzer->setCnStopWords($cnStopWords);
$analyzer->addFilter($stopWordsFilter);
$value = '我的drupal建站过程';
$analyzer->setInput($value, 'utf-8');
$position = 0;
$tokenCounter = 0;
while (($token = $analyzer->nextToken()) !== null) {
$tokenCounter++;
$tokens[] = $token;
JString::str_ireplace($token,'<font color="CC0033">'.$token."</font>",$content);
}
print_r($tokens);
结果输出的,除了"我的"分词正常外,其余都是乱码,经过跟踪,问题出在 Zend_Search_Lucene 如何支持中文检索 63-66行代码上,竟然问题在在于 ctype_alpha ,最后,将63-66代码修改如下:
}elseif(64 < ord($temp_char) && ord($temp_char) < 123){
while ($this->_position < $this->_inputLength && (64 < ord($this->_input[$this->_position]) && ord($this->_input[$this->_position]) < 123) ) {
$this->_position++;
}
}else{
while ($this->_position < $this->_inputLength && ctype_alpha( $this->_input[$this->_position] )) {
$this->_position++;
}
}
也就是判断完全为英文字符先处理一下,问题解决了。
Attatchments: 您还没有登录,登录后方能下载,如果您还没有注册,请点击
免费注册