php+sphinx+scws 中文无效英文有效

<?php

//我也搞不懂是怎么回事,直接上代码:

header("Content-type: text/html; charset=utf-8");
$b_time = microtime(true);
$key = trim(I("key"));
$index = "test1";
//========================================分词

$so = scws_new();
$so->set_charset('utf-8');

// //默认词库
$so->add_dict(ini_get('scws.default.fpath') . '/dict.utf8.xdb');
//自定义词库
// $so->add_dict('./dd.txt',SCWS_XDICT_TXT);
//默认规则
$so->set_rule(ini_get('scws.default.fpath') . '/rules.utf8.ini');

//设定分词返回结果时是否去除一些特殊的标点符号
$so->set_ignore(true);

//设定分词返回结果时是否复式分割,如“中国人”返回“中国+人+中国人”三个词。
// 按位异或的 1 | 2 | 4 | 8 分别表示: 短词 | 二元 | 主要单字 | 所有单字
//1,2,4,8 分别对应常量 SCWS_MULTI_SHORT SCWS_MULTI_DUALITY SCWS_MULTI_ZMAIN SCWS_MULTI_ZALL
$so->set_multi(false);

//设定是否将闲散文字自动以二字分词法聚合
$so->set_duality(false);

//设定搜索词
$so->send_text($key);
$words_array = $so->get_result();
$words1 = "";
foreach ($words_array as $v) {
$words1 = $words1 . '|(' . $v['word'] . ')';
}

//加入全词
$words1 = trim($words1, '|');
$so->close();
echo '<p>输入:' . $key . '</p>';
echo '<p>分词:' . $words1 . '</p>';
//========================================搜索
$sc = new SphinxClient();
$sc->SetServer('127.0.0.1', 9312);
$sc->SetMatchMode(SPH_MATCH_EXTENDED);
$sc->SetArrayResult(true);
$res = $sc->Query($words1, $index);
print_r($res);
$e_time = microtime(true);
$time = $e_time - $b_time;
print_r("总耗时:" . $time);

?>

阅读 1.8k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题