本文目录一览:
php中文分词 怎么获得评价关键词
代码如下:
<?php
header("Content-Type:text/html; charset=utf-8");
define('APP_ROOT', str_replace('\\', '/', dirname(__FILE__)));
$test = '这里是一段中文测试代码!';
function get_tags_arr($title)
{
require(APP_ROOT.'/pscws4.class.php');
$pscws = new PSCWS4();
$pscws->set_dict(APP_ROOT.'/scws/dict.utf8.xdb');
$pscws->set_rule(APP_ROOT.'/scws/rules.utf8.ini');
$pscws->set_ignore(true);
$pscws->send_text($title);
$words = $pscws->get_tops(5);
$tags = array();
foreach ($words as $val) {
$tags[] = $val['word'];
}
$pscws->close();
return $tags;
}
print_r(get_tags_arr($test));
//============================================================
function get_keywords_str($content){
require(APP_ROOT.'/phpanalysis.class.php');
PhpAnalysis::$loadInit = false;
$pa = new PhpAnalysis('utf-8', 'utf-8', false);
$pa->LoadDict();
$pa->SetSource($content);
$pa->StartAnalysis( false );
$tags = $pa->GetFinallyResult();
return $tags;
}
print(get_keywords_str($test));
coreseek3.2 php 怎样更新索引
php是无法更新 coreseek 的索引的,需要使用coreseek的语法,配合定时任务来自动更新索引。 这个写起来很麻烦,我们的系统正好用了 coreseek ,说一下我的思路吧。
- 首先建立一个 search 表,这个表用来存你要进行搜索的、经过分词的数据,分词系统你们自己选,我使用的是php的pscws4中文分词。
DROP TABLE IF EXISTS `search`;
CREATE TABLE `search` (
`searchid` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`content` text NOT NULL,
`add_time` int(11) NOT NULL,
PRIMARY KEY (`searchid`)
) ENGINE=MyISAM AUTO_INCREMENT=15209 DEFAULT CHARSET=utf8;
- 还需要一个 索引计数表 search_counter,这个表用来存放每次索引更新后的最大一个ID,下次更新索引的时候,就不需要从头更新了,只需要比这个ID大的就可以。
DROP TABLE IF EXISTS `search_counter`;
CREATE TABLE `search_counter` (
`counter_id` int(11) NOT NULL,
`max_doc_id` int(11) NOT NULL,
PRIMARY KEY (`counter_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- 配置 coreseek ,以下是我在windows下 coreseek的配置文件,linux 在服务器上,没去找。这里配置了2个索引源,一个是main,一个是增量索引delta,这样不需要每次重建所有索引,只需要合并 main和delta就可以了。
#源定义
source main
{
type = mysql
sql_host = 192.168.0.10
sql_user = root
sql_pass = root
sql_db = database
sql_port = 3306
sql_query_pre = SET NAMES utf8
sql_query_pre = REPLACE INTO search_counter SELECT 1, MAX(searchid) FROM qhb_search
sql_query = SELECT searchid, title, content, controller_id, controller, add_time FROM search
#sql_attr_uint = searchid
sql_attr_uint = controller_id
sql_attr_uint = controller
sql_attr_timestamp = add_time
sql_query_info_pre = SET NAMES utf8
}
source delta : main
{
sql_query_pre = SET NAMES utf8
sql_query = SELECT searchid, title, content, controller_id, controller, add_time FROM qhb_search WHERE searchid > (SELECT max_doc_id FROM qhb_search_counter WHERE counter_id=1)
sql_query_post = REPLACE INTO qhb_search_counter SELECT 1, MAX(searchid) FROM qhb_search
}
#index定义
index main
{
source = main
path = D:/WebSoft/coreseek/var/data/main
docinfo = extern
mlock = 0
morphology = none
min_word_len = 1
html_strip = 0
charset_dictpath = D:/WebSoft/coreseek/etc/
charset_type = zh_cn.utf-8
}
index delta : main
{
source = delta
path = D:/WebSoft/coreseek/var/data/delta
}
#全局index定义
indexer
{
mem_limit = 128M
}
#searchd服务定义
searchd
{
listen = 9312
read_timeout = 5
max_children = 30
max_matches = 1000
seamless_rotate = 0
preopen_indexes = 0
unlink_old = 1
pid_file = D:/WebSoft/coreseek/var/log/searchd_main.pid
log = D:/WebSoft/coreseek/var/log/searchd_main.log
query_log = D:/WebSoft/coreseek/var/log/query_main.log
}
- 建立索引。必须要先建立索引, coreseek 才能启动。下面是我在Windows的建立索引命令,如何使用命令行我就不赘述了。
D:\WebSoft\coreseek\bin\indexer --all --config d:\WebSoft\coreseek\bin\sphinx.conf
- 配置并启动服务
D:\WebSoft\coreseek\bin\searchd --install --config D:\WebSoft\coreseek\bin\sphinx.conf --servicename coreseek
- Windows创建定时任务,每分钟更新一次索引
D:\WebSoft\coreseek\bin\indexer.exe --config D:\WebSoft\coreseek\bin\sphinx.conf delta --rotate
echo indexing, window will close when complete
- Windows创建定时任务,每天凌晨2点合并索引
D:\WebSoft\coreseek\bin\indexer.exe --config D:\WebSoft\coreseek\bin\sphinx.conf --merge main delta --rotate
echo indexing, window will close when complete
- 附上 创建索引,重建索引,合并索引在windows及linux上的方法,以及一些使用上的小问题 windows:
- 建立索引
D:\WebSoft\coreseek\bin\indexer --all --config d:\WebSoft\coreseek\bin\sphinx.conf
- 重建索引
D:\WebSoft\coreseek\bin\indexer --config D:\WebSoft\coreseek\bin\sphinx.conf main --rotate
- 增量索引
D:\WebSoft\coreseek\bin\indexer --config D:\WebSoft\coreseek\bin\sphinx.conf delta --rotate
- 合并索引
D:\WebSoft\coreseek\bin\indexer --config D:\WebSoft\coreseek\bin\sphinx.conf --merge main delta --rotate
- 配置并启动服务
D:\WebSoft\coreseek\bin\searchd --install --config D:\WebSoft\coreseek\bin\sphinx.conf --servicename coreseek
创建自定义词库方法:
- 先去 搜狗细胞词库下载需要的词库
- 使用 深蓝词库转换 将词库转换为 txt
- 使用PHP程序将 生成的txt转换为 coreseek 所需要的格式
- 附加到 unigram.txt
- 使用命令更新分词词库
cmd 进入 bin目录,执行下面命令
mmseg -u D:\WebSoft\coreseek\etc\unigram.txt
- 将生成的 unigram.txt.uni 改名为:uni.lib
- 重建索引
- 重启coreseek服务 注意: 必须先建立索引,服务才能启动
- coreseek索引或者查询时提示ERROR: invalid token in etc解决办法
该提示表示当前的配置文件的编码不是UTF-8(无BOM头)格式,无法正确解析,请使用编辑软件打开配置文件,另存为UTF-8(无BOM头)格式; - failed to lock .....try --rotate
索引已经建立,使用重建索引命令 - 报警告:failed to scanf pid from
没有启动coreseek服务 - 过滤搜索结果,必须使用数组传递,只支持
- 无符号整数(1-32位宽);
- UNIX 时间戳(timestamps);
- 浮点值(32位,IEEE 754单精度);
- 字符串序列 (尤其是计算出的整数值);
- 多值属性 MVA(multi-value attributes) (32位无符号整型值的变长序列)
$this->shpinx->SetFilter('controller', array(1,2) );
CENTOS 操作方法 开机启动coreseek搜索服务:
vi /etc/rc.d/rc.local
在最后一行添加
/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/bin/sphinx.conf
##如要停止搜索服务,请使用
/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/bin/sphinx.conf --stop
##如要已启动服务,要更新索引,请使用
/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/bin/sphinx.conf --all --rotate
linux下编辑定时任务
crontab -e
#凌晨4点合并索引,其余时间每分钟更新索引
* 0-3 * * * /usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf delta --rotate
* 6-23 * * * /usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf delta --rotate
0 4 * * * /usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf --merge main delta --rotate
启动服务:
/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/bin/sphinx.conf
建立索引
/usr/local/coreseek/bin/indexer --all --config /usr/local/coreseek/bin/sphinx.conf
重建索引
/usr/local/coreseek/bin/indexer --config /usr/local/coreseek/bin/sphinx.conf main --rotate
增量索引
/usr/local/coreseek/bin/indexer --config /usr/local/coreseek/bin/sphinx.conf delta --rotate
合并索引
/usr/local/coreseek/bin/indexer --config /usr/local/coreseek/bin/sphinx.conf --merge main delta --rotate
XDB::Open(dict.xdb) failed这个怎么解决啊
set_dict('./pscws4/etc/dict.xdb')
路径检查下,或者重新下载一个替换。