Skip to content

Commit

Permalink
修复Nature.fromString和IOUtil.loadDictionary:#626
Browse files Browse the repository at this point in the history
  • Loading branch information
hankcs committed Sep 17, 2017
1 parent 2077a2b commit 5a5a056
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/hankcs/hanlp/corpus/io/IOUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import com.hankcs.hanlp.corpus.tag.Nature;
import com.hankcs.hanlp.dictionary.CoreDictionary;
import com.hankcs.hanlp.utility.LexiconUtility;
import com.hankcs.hanlp.utility.TextUtility;

import java.io.*;
Expand Down Expand Up @@ -620,7 +621,7 @@ public static void loadDictionary(BufferedReader br, TreeMap<String, CoreDiction
CoreDictionary.Attribute attribute = new CoreDictionary.Attribute(natureCount);
for (int i = 0; i < natureCount; ++i)
{
attribute.nature[i] = Enum.valueOf(Nature.class, param[1 + 2 * i]);
attribute.nature[i] = LexiconUtility.convertStringToNature(param[1 + 2 * i]);
attribute.frequency[i] = Integer.parseInt(param[2 + 2 * i]);
attribute.totalFrequency += attribute.frequency[i];
}
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/hankcs/hanlp/corpus/tag/Nature.java
Original file line number Diff line number Diff line change
Expand Up @@ -809,8 +809,17 @@ public static Nature fromString(String name)
}
catch (Exception e)
{
return null;
// 动态添加的词语有可能无法通过valueOf获取,所以遍历搜索
for (Nature nature : Nature.values())
{
if (nature.toString().equals(name))
{
return nature;
}
}
}

return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,18 @@ public static void restore()
enumBuster.restore();
extraValueMap.clear();
}

public static Nature getNature(String name)
{

try
{
return Nature.valueOf(name);
}
catch (Exception e)
{
// 动态添加的词语有可能无法通过valueOf获取
return extraValueMap.get(name);
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/hankcs/hanlp/utility/LexiconUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,14 @@ public static Nature convertStringToNature(String name, LinkedHashSet<Nature> cu
return nature;
}
}

/**
* 将字符串词性转为Enum词性
* @param name 词性名称
* @return 转换结果
*/
public static Nature convertStringToNature(String name)
{
return convertStringToNature(name, null);
}
}
16 changes: 16 additions & 0 deletions src/test/java/com/hankcs/hanlp/corpus/tag/NatureTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.hankcs.hanlp.corpus.tag;

import com.hankcs.hanlp.corpus.util.CustomNatureUtility;
import junit.framework.TestCase;

public class NatureTest extends TestCase
{
public void testFromString() throws Exception
{
Nature one = CustomNatureUtility.addNature("新词性1");
Nature two = CustomNatureUtility.addNature("新词性2");

assertEquals(one, Nature.fromString("新词性1"));
assertEquals(two, Nature.fromString("新词性2"));
}
}

0 comments on commit 5a5a056

Please sign in to comment.