Skip to content

Commit

Permalink
新增热更新方法 CoreDictionary.reload() fix #1594
Browse files Browse the repository at this point in the history
  • Loading branch information
hankcs committed Dec 22, 2020
1 parent 66e328d commit ae845b6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/main/java/com/hankcs/hanlp/dictionary/CoreDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,18 @@ public static int getWordID(String a)
{
return CoreDictionary.trie.exactMatchSearch(a);
}

/**
* 热更新核心词典<br>
* 集群环境(或其他IOAdapter)需要自行删除缓存文件
*
* @return 是否成功
*/
public static boolean reload()
{
String path = CoreDictionary.path;
IOUtil.deleteFile(path + ".table" + Predefine.BIN_EXT);

return load(path);
}
}
2 changes: 2 additions & 0 deletions src/test/java/com/hankcs/book/ch03/DemoNgramSegment.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public static Segment loadBigram(String modelPath, boolean verbose, boolean vite
// HanLP.Config.enableDebug();
HanLP.Config.CoreDictionaryPath = modelPath + ".txt";
HanLP.Config.BiGramDictionaryPath = modelPath + ".ngram.txt";
CoreDictionary.reload();
CoreBiGramTableDictionary.reload();
// 以下部分为兼容新标注集,不感兴趣可以跳过
HanLP.Config.CoreDictionaryTransformMatrixDictionaryPath = modelPath + ".tr.txt";
if (!modelPath.equals(MSR_MODEL_PATH))
Expand Down

0 comments on commit ae845b6

Please sign in to comment.