不信网恋 发表于 2016-1-4 14:01:08

社区全文检索引擎Hyper Estraier 学习笔记[3]

经过摸索,我决定给HyperEstraier加上由Hightman写的scws支持,使之支持中文分词。
实地看了代码之后,发现Hyperestraier的结构划分并不好,这可能决定了他只能是某些Hacker的心血之作,而不适用多人协作开发,大规模推广应用。这是旁话。
既然找到分词默认是使用的estraier.c的est_break_text函数,那我的目标就是改造这个函数,现在这个函数被我改成了:

   1. scws_t seg;
   2. /* Break a sentence of text and extract words. */
   3. void est_break_text(const char *text, CBLIST *list, int norm, int tail){
   4. // hack by renlu.xu
   5.   CBLIST *words;
   6.   const unsigned char *word, *next;
   7.   unsigned char *utext;
   8.   char *tmp;
   9.   int i, j, k, size, cc, wsiz, nsiz, tsiz;
10.   assert(text && list);
11. if(seg==NULL)
12. {
13. seg=scws_new();
14. scws_set_charset(seg,"UTF-8");
15. scws_set_rule(seg,"/home/y/etc/rules.utf8.ini");
16. int xmode=0;
17. scws_set_dict(seg,"/home/y/etc/dict.xdb",(xmode & XMODE_DICT_MEM) ? SCWS_XDICT_MEM : SCWS_XDICT_XDB);
18. if(seg->d == NULL && !(xmode & XMODE_NO_TIME))
19.         fprintf(stderr, "WARNING: input dict file load failed. /home/y/etc/dict.xdb\n");
20.
21. }
22. int fsize;
23.   fsize=strlen(text);
24.   scws_send_text(seg,text,fsize);
25. scws_res_t res,cur;
26. while ((cur = res = scws_get_result(seg))!= NULL)
27. {                                                         
28. while (cur != NULL)                                    
29. {            
30. cblistpush(list,text+cur->off,cur->len);
31. cur = cur->next;                                 
32. }                                                      
33. scws_free_result(res);                                 
34. }
35. int iter_i;
36. int wsize=0;
37. fprintf(stderr,"\nsegmented words:\n");
38. for(iter_i=0;iter_i<cblistnum(list);iter_i++){
39. word=CB_LISTVAL2(list,iter_i,wsize);
40. fprintf(stderr,"%s\t",word);
41. }
42. return ;
43.}

Ok,其中函数中最后一个for语句是为了调试,看看分词结果如何。
这里用到的scws_send_text等函数是hightman的scws中定义的,因此需要在estraier.c的文件头加上:

   1. #include "scws.h"

并修改Hyperestraier的Makefile:

   1. LIBS = -lqdbm -lz -lm -lc-lscws

然后make && make install
下面作个测试:

   1. #!/bin/sh
   2. #file:test.sh
   3. rm -rf test_db
   4. estcmd create test_db
   5. find ./ -name "1.txt" -type f |estcmd gather -cl -fm -cm test_db -
   6. estcmd search -vx -max 10 test_db '索引'
页: [1]
查看完整版本: 社区全文检索引擎Hyper Estraier 学习笔记[3]