網上對於ik分詞器的自定義詞庫大可能是採用配置文件配置自定義詞庫,這裏直接將須要添加的詞庫加到ik的系統詞庫中。數據庫
需求:項目中用到了ik分詞器,ik自帶有本身的系統詞庫,如下爲ik獲取各個詞庫路徑的方法;.net
System.out.println(cfg.getExtDictionarys());
System.out.println(cfg.getMainDictionary()); // 系統默認詞庫
System.out.println(cfg.getQuantifierDicionary());線程
如今公司有本身的詞庫,好幾張表,這幾張表用戶是能夠操做的,好比增長,這時候就須要將公司詞庫動態的加入到系統詞庫中,設定了一個計劃線程,如下是具體作法:ci
private static ScheduledExecutorService ex = Executors.newSingleThreadScheduledExecutor();
private static Boolean wordDBLoadScheduleFlag = false;
private static Set<String> wordDBSet = new HashSet<String>();get
/**
* 用於更新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);
}it
}
}
}
//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();
}
}
}
io