Permalink
Browse files

自定义词典支持热更新:#563

  • Loading branch information...
hankcs committed Jun 20, 2017
1 parent 10bf486 commit dca825b7976c8f6415b375c94d35ddb66d2b1d34
@@ -41,14 +41,11 @@
*/
public static BinTrie<CoreDictionary.Attribute> trie;
public static DoubleArrayTrie<CoreDictionary.Attribute> dat = new DoubleArrayTrie<CoreDictionary.Attribute>();
/**
* 第一个是主词典,其他是副词典
*/
public final static String path[] = HanLP.Config.CustomDictionaryPath;
// 自动加载词典
static
{
String path[] = HanLP.Config.CustomDictionaryPath;
long start = System.currentTimeMillis();
if (!loadMainDictionary(path[0]))
{
@@ -69,6 +66,7 @@ private static boolean loadMainDictionary(String mainPath)
LinkedHashSet<Nature> customNatureCollector = new LinkedHashSet<Nature>();
try
{
String path[] = HanLP.Config.CustomDictionaryPath;
for (String p : path)
{
Nature defaultNature = Nature.n;
@@ -525,4 +523,17 @@ public static void parseText(String text, AhoCorasickDoubleArrayTrie.IHit<CoreDi
processor.hit(searcher.begin, searcher.begin + searcher.length, searcher.value);
}
}
/**
* 热更新(重新加载)<br>
* 集群环境(或其他IOAdapter)需要自行删除缓存文件(路径 = HanLP.Config.CustomDictionaryPath[0] + Predefine.BIN_EXT)
* @return 是否加载成功
*/
public static boolean reload()
{
String path[] = HanLP.Config.CustomDictionaryPath;
if (path == null || path.length == 0) return false;
new File(path[0] + Predefine.BIN_EXT).delete(); // 删掉缓存
return loadMainDictionary(path[0]);
}
}
@@ -0,0 +1,12 @@
package com.hankcs.hanlp.dictionary;
import junit.framework.TestCase;
public class CustomDictionaryTest extends TestCase
{
public void testReload() throws Exception
{
assertEquals(true, CustomDictionary.reload());
assertEquals(true, CustomDictionary.contains("中华白海豚"));
}
}

2 comments on commit dca825b

@xwj0813

This comment has been minimized.

xwj0813 replied Aug 28, 2018

使用python调API方式,有直接添加自定义字典的方法吗?比如load_userdict("dict.txt")这种吗?

@hankcs

This comment has been minimized.

Owner

hankcs replied Aug 28, 2018

HanLP.Config. CustomDictionaryPath = ["dict.txt","dict2.txt"]
CustomDictionary.reload()

Please sign in to comment.