网上对于ik分词器的自定义词库大多是采用配置文件配置自定义词库,这里直接将需要添加的词库加到ik的系统词库中。
需求:项目中用到了ik分词器,ik自带有自己的系统词库,以下为ik获取各个词库路径的方法;
System.out.println(cfg.getExtDictionarys());
System.out.println(cfg.getMainDictionary()); // 系统默认词库
System.out.println(cfg.getQuantifierDicionary());
现在公司有自己的词库,好几张表,这几张表用户是可以操作的,比如增加,这时候就需要将公司词库动态的加入到系统词库中,设定了一个计划线程,以下是具体做法:
private static ScheduledExecutorService ex = Executors.newSingleThreadScheduledExecutor();
private static Boolean wordDBLoadScheduleFlag = false;
private static Set<String> wordDBSet = new HashSet<String>();
/**
* 用于更新ik词库
* @throws InterruptedException
*/
public PcDocumentService(){
if (wordDBLoadScheduleFlag == false) {
synchronized (wordDBLoadScheduleFlag) {
if (wordDBLoadScheduleFlag == false) {
WordThread wt=new WordThread();
wordDBLoadScheduleFlag = true;
ex.scheduleWithFixedDelay(wt, 10, 600, TimeUnit.SECONDS);
}
}
}
}
//ik词库更新线程类
public static class WordThread extends Thread{
public void run() {
//取出词库数据存入set
try {
System.out.println("开始更新词库======================="+Thread.currentThread().getName());
/**/
Map<String, Object> params= new HashMap<String, Object>();
WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
ZlAreaAliasService zlAreaAliasService=(ZlAreaAliasService) context.getBean("zlAreaAliasService");
List<ZlAreaAliasEntity> zAreaList= zlAreaAliasService.selectForList(params);
for(ZlAreaAliasEntity zArea:zAreaList){
wordDBSet.add(zArea.getAreaName());
}
ZlLexiconService zlLexiconService=(ZlLexiconService) context.getBean("zlLexiconService");
List<ZlLexiconEntity> zLexiList=zlLexiconService.selectForList(params);
for(ZlLexiconEntity zLexi:zLexiList){
wordDBSet.add(zLexi.getLexName());
}
ZlProdAliasService zlProdAliasService=(ZlProdAliasService) context.getBean("zlProdAliasService");
List<ZlProdAliasEntity> zProdList=zlProdAliasService.selectForList(params);
for(ZlProdAliasEntity zProd:zProdList){
wordDBSet.add(zProd.getProdName());
}
ZlLexiconRelationService zlLexiconRelationService=(ZlLexiconRelationService) context.getBean("zlLexiconRelationService");
List<ZlLexiconRelationEntity> zLeReList=zlLexiconRelationService.selectForList(params);
for(ZlLexiconRelationEntity zLeRe:zLeReList){
wordDBSet.add(zLeRe.getLexicon1());
wordDBSet.add(zLeRe.getLexicon2());
}
ZlCompAliasService zlCompAliasService=(ZlCompAliasService) context.getBean("zlCompAliasService");
List<ZlCompAliasEntity> zCompList=zlCompAliasService.selectForList(params);
for(ZlCompAliasEntity zComp:zCompList){
wordDBSet.add(zComp.getCompName());
}
Dictionary.initial(DefaultConfig.getInstance()).addWords(wordDBSet);
System.out.println("数据库词库大小:======================="+wordDBSet.size());
System.out.println("================词库更新完成================");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}