Coreseek自定义中文词库

来源:互联网 发布:机器手编程语言 编辑:程序博客网 时间:2024/06/03 09:31

进入/usr/local/mmseg3/etc,在这里能看到这几个文件:mmseg.ini unigram.txt uni.lib
unigram.txt是词库的文本文件, uni.lib是mmseg真正使用的词库字典

我们可以在unigram.txt中增加我们想要的分词,然后用mmseg -u命令去重新生成新的lib文件

vim unigram.txt

词库文本内容格式如下:

词库文本内容格式

分词库的一条记录要分为两行来写,第一行为 [词条]\t[词频率],(注意此处的\t是tab键)。当词条的字数是两个字或更多时,词频率必须是1。(此处词频的含义本人暂时也不是十分理解,欢迎各位在评论区讨论)
第二行直接写x:1,只是起到占位的作用,没有任何实际作用。

示例:

我的单词    1x:1

使用PHP代码添加分词

<?php/** * 需要原文件命名为words.txt,产生的新文件为words_new.txt */ini_set('max_execution_time','6000');$buffer=ini_get('output_buffering');if($buffer)ob_end_flush();echo '处理新词库...';flush();$filename = "words.txt";$handle = fopen ($filename, "r");$content = fread ($handle, filesize ($filename));fclose ($handle);$content=trim($content);$arr1 = explode( "\r\n" ,$content );$arr1=array_flip(array_flip($arr1));foreach($arr1 as $key=>$value){    $value=dealchinese($value);    if(!empty($value)){        $arr1[$key] = $value;    }    else{        unset($arr1[$key]);    }}echo '处理原来词库...';flush();$filename2 = "unigram.txt";$handle2 = fopen ($filename2, "r");$content2 = fread ($handle2, filesize ($filename2));fclose ($handle2);$content2=dealchinese($content2,"\r\n");$arr2 = explode( "\r\n" ,$content2 );echo '删除相同词条...';flush();$array_diff=array_diff($arr1,$arr2);echo '格式化词库...';flush();$words='';foreach($array_diff as $k=>$word){    $words.=$word."\t1\r\nx:1\r\n";}//echo $words;file_put_contents('words_new.txt',$words,FILE_APPEND);echo 'done!';function dealChinese($str,$join=''){    preg_match_all('/[\x{4e00}-\x{9fff}]+/u', $str, $matches); //将中文字符全部匹配出来    $str = join($join, $matches[0]); //从匹配结果中重新组合    return $str;}?>

先备份原来的uni.lib,再重新生成lib字典

cp uni.lib uni.lib.pak/usr/local/mmseg3/bin/mmseg -u /usr/local/mmseg3/etc/unigram.txt mv unigram.txt.uni uni.lib

测试

echo "买衣服" > whatever.txt  #新建一个文件/usr/local/mmseg3/bin/mmseg -d /usr/local/mmseg3/etc/ whatever.txt #测试新文件的分词#得到下面的结果买衣服/xWord Splite took: 28 ms.#如果没有新词典,可能得到的是这样的结果买/x 衣服/xWord Splite took: 0 ms.

最后重新创建索引

/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft_mysql.conf --all --rotate