from:https://code.google.com/p/xsplit/
一點(diǎn)歷史
這個(gè)project大約是09年初開始做的,很快就形成了現(xiàn)在版本的樣子,后陸續(xù)做了一些小修改和修復(fù)了一些小bug,現(xiàn)整理了一下決定發(fā)布。把它open source的主要原因,就是希望這個(gè)小工具能對(duì)大家有用,并且能夠參與進(jìn)來一起把它做的更好
20101025
Change Log
20130509 0.0.9 released fixed an issue when compiler uses strict type checking
20101108 0.0.8 beta,加入xs_simhash和xs_hdist函數(shù),分別計(jì)算simhash和漢明距離。
關(guān)于xsplit的交流,請(qǐng)到xsplit貼吧: http://tieba.baidu.com/f?kw=xsplit 參與討論。
xsplit是一個(gè)PHP擴(kuò)展,提供基于MMSEG算法的分詞功能。目前只在linux下測(cè)試并部署過,希望有朋友可以幫忙編譯提供windows下的dll。
xsplit只處理UTF8編碼格式,如果是其他編碼格式,請(qǐng)?jiān)谑褂们白孕修D(zhuǎn)換
xsplit主要有以下幾個(gè)函數(shù):
bool xs_build ( array $words, string $dict_file )
resource xs_open (string $dict_file [, bool $persistent])
array xs_split ( string $text [, int $split_method = 1 [, resource $dictionary_identifier ] ] )
mixed xs_search ( string $text [, int $search_method [, resource $dictionary_identifer ] ] )
string xs_simhash( string $text [, bool $isBinary] ) 
int xs_hdist( string $simhash1, string $simhash2 )
安裝過程與一般的PHP擴(kuò)展安裝一樣
$phpize
$./configure --with-php-config=/path/to/php-config
$make
$make install
php.ini中可以設(shè)置以下參數(shù):
xsplit.allow_persisten = On
xsplit.max_dicts = 5
xsplit.max_persistent = 3
xsplit.default_dict_file = /home/xdict
xsplit.allow_persistent 是否允許加載持久詞典
xsplit.max_dicts 允許同時(shí)打開的最大詞典數(shù)目
xsplit.max_persistent 允許同時(shí)打開的最大持久詞典數(shù)目
xsplit.default_dict_file 默認(rèn)的詞典,沒有指定詞典時(shí)會(huì)調(diào)用此詞典
源碼中有一個(gè)utils目錄,包含
make_dict.php 提供命令行方式創(chuàng)建詞典
xsplit.php 一個(gè)簡(jiǎn)單的示例文件
xdict_example.txt 一個(gè)文本詞庫的格式示例
make_dict.php的使用例子如下:
$php make_dict.php ./xdict_example.txt ./xdict.db
文本詞庫的格式請(qǐng)參考xdict_example.txt
bool xs_build (array $words, string $dict_file)
從$words數(shù)組建立名稱為$dict_file的詞典,若成功則返回true。$words數(shù)組的格式請(qǐng)參考示例,key為詞語,value為詞頻。
例子如下:
<?php
$dict_file='dict.db';
$dwords['美麗']=100;
$dwords['蝴蝶']=100;
$dwords['永遠(yuǎn)']=100;
$dwords['心中']=100;
$dwords['翩翩']=100;
$dwords['飛舞']=100;
$dwords['翩翩飛舞']=10;
if(!xs_build($dwords, $dict_file)) {
    die('建立詞典失敗!');
}
resource xs_open (string $dict_file [, bool $persistent])
打開一個(gè)詞典文件,并返回一個(gè)resource類型的identifier。$persistent可以指定是否是持久化詞典,持久化詞典在這里可以理解為詞典資源生命周期的不同,一般情況下$persistent=true或者默認(rèn)缺省即可。在進(jìn)行分詞的時(shí)候,可以指定不同的詞典。
$dict_file_1 = 'xdcit.db';
$dict_file_2 = 'mydict.db';
$dict1 = xs_open($dict_file);
xs_open($dict_file); 
array xs_split ( string $text [, int $split_method = 1 [, resource $dictionary_identifier ] ] )
對(duì)文本進(jìn)行分詞,可以指定分詞方法和詞典。分詞方法目前有兩種,一個(gè)是MMSEG算法(默認(rèn)),一個(gè)是正向最大匹配,分別用常量XS_SPLIT_MMSEG和XS_SPLIT_MMFWD表示。返回值是一個(gè)數(shù)組,包含所有切分好的詞語。如果不指定詞典,最后一次打開的詞典將被使用。
<?php
$text="那只美麗的蝴蝶永遠(yuǎn)在我心中翩翩飛舞著。";
$dict_file = 'xdict.db';
$dict_res = xs_open($dict_file);
$words = xs_split($text);  /* 此處沒有指定詞典資源,默認(rèn)使用最后一次打開的詞典 */
$words1 = xs_split($text, XS_SPLIT_MMSEG, $dict_res);
mixed xs_search ( string $text [, int $search_method [, $dictionary_identifer ] ] ) 基于雙數(shù)組trie樹提供的一些功能,$search_method有四個(gè)常量表示:
XS_SEARCH_CP : darts的commonPrefixSearch封裝,如果沒有找到,返回false。
XS_SEARCH_EM : darts的exactMatchSearch封裝,如果沒有找到,返回false。
XS_SEARCH_ALL_SIMPLE : 按照詞典返回所有詞語詞頻總和,一個(gè)INT型數(shù)值。
XS_SEARCH_ALL_DETAIL : 按照詞典返回所有詞典的詞頻,并以數(shù)組形式返回每一個(gè)詞語的詳細(xì)統(tǒng)計(jì)。
如果不指定詞典,最后一次打開的詞典將被使用。
<?php
xs_open($dict_file);
$text="那只美麗的蝴蝶永遠(yuǎn)在我心中翩翩飛舞著。";
$word='翩翩飛舞';
$result=xs_search($word, XS_SEARCH_CP); /* common prefix search */
var_dump($result);
$result=xs_search($word, XS_SEARCH_EM); /* exact match search */
var_dump($result);
$result=xs_search($text, XS_SEARCH_ALL_SIMPLE);
var_dump($result);
$result=xs_search($text, XS_SEARCH_ALL_DETAIL);
var_dump($result);
string xs_simhash( array $tokens [, bool $rawoutput] )
計(jì)算simhash。這里所有token權(quán)重都是1,$tokens的例子如array('在', '這個(gè)', '世界')。$rawoput默認(rèn)為0,即返回simhash的hex string形式,如md5, sha1函數(shù)一樣;如過$rawoput為真,返回一個(gè)8字節(jié)的字符串,這個(gè)字符串實(shí)際上是一個(gè)64 bits的整型數(shù),uint64_t,在一些特殊情況下可以用到。
int xs_hdist( string $simhash1, $string $simhash2)
計(jì)算漢明距離。
<?php
xs_open('xdict');
$text1="那只美麗的蝴蝶永遠(yuǎn)在我心中翩翩飛舞著。";
$text2="那只美麗的蝴蝶永遠(yuǎn)在我心中翩翩飛舞。";
$tokens1=xs_search($text1, XS_SEARCH_ALL_INDICT); /* 去掉標(biāo)點(diǎn)等特殊符號(hào),經(jīng)過實(shí)驗(yàn),計(jì)算simhash時(shí),一些標(biāo)點(diǎn)、換行、特殊符號(hào)等對(duì)效果影響較大 */
$tokens2=xs_search($text2, XS_SEARCH_ALL_INDICT);
$simhash1=xs_simhash($tokens1);
$simhash2=xs_simhash($tokens2);
echo "simhash1 is {$simhash1}\n";
echo "simhash2 is {$simhash2}\n";
$hamming_dist=xs_hdist($simhash1, $simhash2);
echo "bit-wise format:\n";
echo decbin(hexdec($simhash1)), "\n";
echo decbin(hexdec($simhash2)), "\n";
echo "hamming distance is {$hamming_dist}\n";
Terms - Privacy