IKanalyzer 分词器(???)

//今天看了看IKanalyzer 扩充词汇看得我一头雾水    分词器的使用还没理解直接搞扩充词汇有点知识脱节

//谁能举个例看看怎么扩充????

网上提供的方法是:

.基于api 我的想法如下 利用一个数据库表保存实时动态添加的词元,如果对应的实体类有更新,就执行添加词元的操作 具体的话就是从数据库读取词元,然后存在List<String> termList,执行 Dictionary.loadExtendWords(termList); 其实就是监听,也可以利用线程池实时扫描,目的是知道系统正在添加词元到数据库表,然后就是加载词典了

2.IK的文档这么写道3.1基于 API的词典扩充  
IK 分词器支持使用 API 编程模型扩充您的词典和停止词典。如果您的个性化词典
储于数据库中,这个方式应该对您适用。API 如下:
?  类org.wltea.analyzer.dic.Dictionary
说明: IK 分词器的词典对象。它负责中文词汇的加载,内存管理和匹配检索。
?  public static void addWords(Collection<String> words)
说明:加载用户扩展的词汇列表到 IK 的主词典中,增加分词器的可识别词语
参数1:Collection<String> words , 扩展的词汇列表
返回值:无
 
?  public static void disableWords(Collection<String> words)
说明:屏蔽词典中的词元
参数1:Collection<String> words, 待删除的词列表
返回值:无
 
3.2基于配置的词典扩充  
IK 分词器还支持通过配置 IKAnalyzer.cfg.xml 文件来扩充您的专有词典以及停止
(过滤词典)。
1.  部署IKAnalyzer.cfg.xml
IKAnalyzer.cfg.xml 部 署 在 代 码 根 目 录 下 ( 对 于 web 项 目 , 通 常
WEB-INF/classes 目录)同 hibernate、log4j 等配置文件相同。
2.  词典文件的编辑与部署

选择ikanalyzer作分词组件,它的词典是自带的,然后用户可以在配置文件中加入扩展词典,也可以用api实现往里面加入新词  比如利用

              Dictionary.loadExtendWords(List<String> l);
不过这个好像只是加载一次,下次再启动词典的时候就没有这些词了。    所以我改成从配置文件添加,但是词典加载之后好像并没有这些词。用
              Dictionary.matchInMainDict("甲流".toCharArray());
,查看输出false.
  然后把它写在扩展词典文件中(采用无boom的utf8格式),然后重新建索引
                Analyzer a = new IKAnalyzer();
              writer = new IndexWriter(index, a, this.create,new IndexWriter.MaxFieldLength(1000000));
    再查询 甲流 的时候,用 
                keyword = "甲流";
                IKQueryParser.parse("content", keyword);
然后搜索结果用
                System.out.println("this document's score is " + docs[i].score);
     System.out.println(search.searcher.explain(q, docs[i].doc));
输出的结果是这样的(以第一个结果为例):this document's score is NaN
           2.5944433 = (MATCH) sum of:
                 1.1501259 = (MATCH) weight(content:甲 in 806), product of:
                      0.6658104 = queryWeight(content:甲), product of:
                      3.454815 = idf(docFreq=401, maxDocs=4681)
                      0.19271956 = queryNorm
                1.7274075 = (MATCH) fieldWeight(content:甲 in 806), product of:
                      1.0 = tf(termFreq(content:甲)=1)
                      3.454815 = idf(docFreq=401, maxDocs=4681)
                      0.5 = fieldNorm(field=content, doc=806)
                 1.4443176 = (MATCH) weight(content:流 in 806), product of:
                       0.746121 = queryWeight(content:流), product of:
                       3.8715372 = idf(docFreq=264, maxDocs=4681)
                        0.19271956 = queryNorm
1.9357686 = (MATCH) fieldWeight(content:流 in 806), product of:
                       1.0 = tf(termFreq(content:流)=1)
                       3.8715372 = idf(docFreq=264, maxDocs=4681)
                       0.5 = fieldNorm(field=content, doc=806)

原文地址:https://www.cnblogs.com/lixingle/p/3313040.html