因为项目需要使用中文搜索,觉得使用coreseek实现,其中使用mmseg 分词库
但是mmseg原词库不适合电商项目,所以决定自定义分词库。
1.下载搜狗词库,例如有淘宝专用词库 http://pinyin.sogou.com/dict/detail/index/22416,得到一个.scel文件
2.将下载的文件是scel文件,转换为txt文件。这里使用“深蓝词库转换”软件,下载地址http://down1.downxia.com/down/slckzh.rar?vspublic=1403b1e16f8f47d465dae7516ce3c1f9.exe
使用此软件,获取txt文件
3.将txt文件,转换为mmseg对应格式的txt文件。
在转换,可以查看mmseg 文件原本的unigram.txt
文件
1
| head -10 /usr/local/mmseg3/etc/unigram.txt
|
查看mmseg原本的词库文件,我们可以看到格式如下,我们就是需要把新词库文件转化成这种格式
1 2 3 4 5 6
| 爱宝疗 1 x:1 爱宝氏鱼肝油 1 x:1 艾贝 1 x:1
|
转换过程,其实也就是 文字后面加 制表符 + 1 换行 + x:1 + 换行“\t1\r\nx:1\r\n”。
有些编辑器就可以在实现匹配转换的功能。
本人使用php脚本来实现:原文件命名为1.txt,产生的新文件为words_new.txt。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| <?php
set_time_limit(0); $n = 28; $arrNew = array(); for($i=1; $i<=$n; $i++){ $filename = './data/' . $i . ".txt"; $handle = fopen ($filename, "r"); $content = fread ($handle, filesize ($filename)); fclose ($handle); $arr1 = array(); $content=trim($content); $arr1 = explode( "\r\n" ,$content ); $arr1=array_flip(array_flip($arr1)); foreach($arr1 as $key=>$value){ if(!empty($value)){ $arrNew[] = trim($value); } } } $res = array_unique($arrNew); echo 'sum:' . count($res); $words=''; foreach($res as $k=>$word){ $words.=$word."\t1\r\nx:1\r\n"; } file_put_contents('words_new.txt',$words,FILE_APPEND); function dealChinese($str,$join=''){ preg_match_all('/[\x{4e00}-\x{9fff}]+/u', $str, $matches); $str = join($join, $matches[0]); return $str; }
|
4.替换uni.lib
文件
首先,使用 /usr/local/mmseg3/bin/mmseg -u words_new.txt
产生一个words_new.txt.uni
文件
然后将words_new.txt.uni
重命名为uni.lib
1 2 3
| /usr/local/mmseg3/bin/mmseg -u words_new.txt rm uni.lib mv words_new.txt.uni uni.lib
|
5.测试
1 2 3 4 5 6 7 8 9 10 11 12 13
| echo "做衣服" > whatever.txt /usr/local/mmseg3/bin/mmseg -d /usr/local/mmseg3/etc/ whatever.txt
做衣服/x Word Splite took: 28 ms.
做/x 衣服/x Word Splite took: 0 ms.
|