本文目錄一覽:
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 ,說一下我的思路吧。
1、首先建立一個 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;
2、還需要一個 索引計數表 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;
3、配置 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_query第一列id需為整數
#title、content作為字元串/文本欄位,被全文索引
#sql_attr_uint = searchid #從SQL讀取到的值必須為整數
sql_attr_uint = controller_id # 資料庫ID過濾
sql_attr_uint = controller # 控制器過濾
sql_attr_timestamp = add_time #從SQL讀取到的值必須為整數,作為時間屬性
sql_query_info_pre = SET NAMES utf8 #命令行查詢時,設置正確的字符集
#sql_query_info = SELECT * FROM qhb_search WHERE searchid=$searchid #命令行查詢時,從資料庫讀取原始數據信息
}
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 #對應的source名稱
path = D:/WebSoft/coreseek/var/data/main #請修改為實際使用的絕對路徑,例如:/usr/local/coreseek/var/…
docinfo = extern
mlock = 0
morphology = none
min_word_len = 1
html_strip = 0
#中文分詞配置,詳情請查看:
#charset_dictpath = /usr/local/mmseg3/etc/ #BSD、Linux環境下設置,/符號結尾
charset_dictpath = D:/WebSoft/coreseek/etc/ #Windows環境下設置,/符號結尾,最好給出絕對路徑,例如:C:/usr/local/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 #請修改為實際使用的絕對路徑,例如:/usr/local/coreseek/var/…
log = D:/WebSoft/coreseek/var/log/searchd_main.log #請修改為實際使用的絕對路徑,例如:/usr/local/coreseek/var/…
query_log = D:/WebSoft/coreseek/var/log/query_main.log #請修改為實際使用的絕對路徑,例如:/usr/local/coreseek/var/…
}
4、建立索引。必須要先建立索引, coreseek 才能啟動。下面是我在Windows的建立索引命令,如何使用命令行我就不贅述了。
D:\WebSoft\coreseek\bin\indexer –all –config d:\WebSoft\coreseek\bin\sphinx.conf
5、配置並啟動服務
D:\WebSoft\coreseek\bin\searchd –install –config
D:\WebSoft\coreseek\bin\sphinx.conf –servicename coreseek
6、Windows創建定時任務,每分鐘更新一次索引
D:\WebSoft\coreseek\bin\indexer.exe –config D:\WebSoft\coreseek\bin\sphinx.conf delta –rotate
echo indexing, window will close when complete
7、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
8、附上 創建索引,重建索引,合併索引在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
創建自定義詞庫方法:
1、先去 搜狗細胞詞庫下載需要的詞庫
2、使用 深藍詞庫轉換 將詞庫轉換為 txt
3、使用PHP程序將 生成的txt轉換為 coreseek 所需要的格式
4、附加到 unigram.txt
5、使用命令更新分詞詞庫
cmd 進入 bin目錄,執行下面命令
mmseg -u D:\WebSoft\coreseek\etc\unigram.txt
6、將生成的 unigram.txt.uni 改名為:uni.lib
7、重建索引
8、重啟coreseek服務
注意:
必須先建立索引,服務才能啟動
1、coreseek索引或者查詢時提示ERROR: invalid token in etc解決辦法
該提示表示當前的配置文件的編碼不是UTF-8(無BOM頭)格式,無法正確解析,請使用編輯軟體打開配置文件,另存為UTF-8(無BOM頭)格式;
2、failed to lock …..try –rotate
索引已經建立,使用重建索引命令
3、報警告:failed to scanf pid from
沒有啟動coreseek服務
4、過濾搜索結果,必須使用數組傳遞,只支持
無符號整數(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’)
路徑檢查下,或者重新下載一個替換。
原創文章,作者:ZFOA,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/134913.html