Skip to content

Commit

Permalink
自定义词典支持热更新:#563
Browse files Browse the repository at this point in the history
  • Loading branch information
hankcs committed Jun 20, 2017
1 parent 10bf486 commit dca825b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/main/java/com/hankcs/hanlp/dictionary/CustomDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,11 @@ public class CustomDictionary
*/
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]))
{
Expand All @@ -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;
Expand Down Expand Up @@ -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]);
}
}
Original file line number Diff line number Diff line change
@@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@hankcs
Copy link
Owner Author

@hankcs hankcs commented on dca825b Aug 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.