diff --git a/src/main/java/com/hankcs/hanlp/collection/trie/datrie/IntArrayList.java b/src/main/java/com/hankcs/hanlp/collection/trie/datrie/IntArrayList.java index 8d1cf5ec2..599952638 100644 --- a/src/main/java/com/hankcs/hanlp/collection/trie/datrie/IntArrayList.java +++ b/src/main/java/com/hankcs/hanlp/collection/trie/datrie/IntArrayList.java @@ -4,6 +4,7 @@ import com.hankcs.hanlp.corpus.io.ICacheAble; import java.io.*; +import java.util.ArrayList; /** * 动态数组 @@ -205,4 +206,15 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE exponentialExpanding = in.readBoolean(); exponentialExpandFactor = in.readDouble(); } + + @Override + public String toString() + { + ArrayList head = new ArrayList(20); + for (int i = 0; i < Math.min(size, 20); ++i) + { + head.add(data[i]); + } + return head.toString(); + } } diff --git a/src/main/java/com/hankcs/hanlp/collection/trie/datrie/MutableDoubleArrayTrieInteger.java b/src/main/java/com/hankcs/hanlp/collection/trie/datrie/MutableDoubleArrayTrieInteger.java index 89390bc46..8c1e42f76 100644 --- a/src/main/java/com/hankcs/hanlp/collection/trie/datrie/MutableDoubleArrayTrieInteger.java +++ b/src/main/java/com/hankcs/hanlp/collection/trie/datrie/MutableDoubleArrayTrieInteger.java @@ -1245,6 +1245,7 @@ public KeyValuePair() from = b + i; path.append(from); b = base.get(from); + i = 0; if (getCheck(b + UNUSED_CHAR_VALUE) == from) { value = getLeafValue(getBase(b + UNUSED_CHAR_VALUE)); diff --git a/src/test/java/com/hankcs/hanlp/model/perceptron/feature/ImmutableFeatureMDatMapTest.java b/src/test/java/com/hankcs/hanlp/model/perceptron/feature/ImmutableFeatureMDatMapTest.java index a7e0c88e4..0f1a3d31e 100644 --- a/src/test/java/com/hankcs/hanlp/model/perceptron/feature/ImmutableFeatureMDatMapTest.java +++ b/src/test/java/com/hankcs/hanlp/model/perceptron/feature/ImmutableFeatureMDatMapTest.java @@ -1,9 +1,13 @@ package com.hankcs.hanlp.model.perceptron.feature; import com.hankcs.hanlp.HanLP; +import com.hankcs.hanlp.collection.trie.datrie.MutableDoubleArrayTrieInteger; import com.hankcs.hanlp.model.perceptron.model.LinearModel; import junit.framework.TestCase; +import java.util.Map; +import java.util.TreeMap; + public class ImmutableFeatureMDatMapTest extends TestCase { public void testCompress() throws Exception @@ -11,4 +15,22 @@ public void testCompress() throws Exception LinearModel model = new LinearModel(HanLP.Config.PerceptronCWSModelPath); model.compress(0.1); } + + public void testFeatureMap() throws Exception + { + LinearModel model = new LinearModel(HanLP.Config.PerceptronCWSModelPath); + ImmutableFeatureMDatMap featureMap = (ImmutableFeatureMDatMap) model.featureMap; + MutableDoubleArrayTrieInteger dat = featureMap.dat; + System.out.println(featureMap.size()); + System.out.println(featureMap.entrySet().size()); + System.out.println(featureMap.idOf("\u0001/\u00014")); + TreeMap map = new TreeMap(); + for (Map.Entry entry : dat.entrySet()) + { + map.put(entry.getKey(), entry.getValue()); + assertEquals(entry.getValue().intValue(), dat.get(entry.getKey())); + } + System.out.println(map.size()); + assertEquals(dat.size(), map.size()); + } } \ No newline at end of file