diff --git a/src/main/java/com/hankcs/hanlp/collection/trie/DoubleArrayTrie.java b/src/main/java/com/hankcs/hanlp/collection/trie/DoubleArrayTrie.java index a5bba4d6c..e0f9b7c45 100644 --- a/src/main/java/com/hankcs/hanlp/collection/trie/DoubleArrayTrie.java +++ b/src/main/java/com/hankcs/hanlp/collection/trie/DoubleArrayTrie.java @@ -1299,7 +1299,7 @@ public LongestSearcher(int offset, char[] charArray) */ public boolean next() { - value = null; + length = 0; begin = i; int b = base[0]; int n; @@ -1309,7 +1309,7 @@ public boolean next() { if (i >= arrayLength) // 指针到头了,将起点往前挪一个,重新开始,状态归零 { - return value != null; + return length > 0; } p = b + (int) (charArray[i]) + 1; // 状态转移 p = base[char[i-1]] + char[i] + 1 if (b == check[p]) // base[char[i-1]] == check[base[char[i-1]] + char[i] + 1] @@ -1317,7 +1317,7 @@ public boolean next() else { if (begin == arrayLength) break; - if (value != null) + if (length > 0) { i = begin + length; // 输出最长词后,从该词语的下一个位置恢复扫描 return true; diff --git a/src/test/java/com/hankcs/hanlp/collection/trie/DoubleArrayTrieTest.java b/src/test/java/com/hankcs/hanlp/collection/trie/DoubleArrayTrieTest.java index ed9131e56..c8219fe7a 100644 --- a/src/test/java/com/hankcs/hanlp/collection/trie/DoubleArrayTrieTest.java +++ b/src/test/java/com/hankcs/hanlp/collection/trie/DoubleArrayTrieTest.java @@ -54,6 +54,37 @@ public void testLongestSearcher() throws Exception } } + public void testLongestSearcherWithNullValue() { + TreeMap buildFrom = new TreeMap(); + TreeMap buildFromValueNull = new TreeMap(); + String[] keys = new String[]{"he", "her", "his"}; + for (String key : keys) { + buildFrom.put(key, key); + buildFromValueNull.put(key, null); + } + DoubleArrayTrie trie = new DoubleArrayTrie(buildFrom); + DoubleArrayTrie trieValueNull = new DoubleArrayTrie(buildFromValueNull); + + String text = "her3he6his-hers! "; + + DoubleArrayTrie.LongestSearcher searcher = trie.getLongestSearcher(text.toCharArray(), 0); + DoubleArrayTrie.LongestSearcher searcherValueNull = trieValueNull.getLongestSearcher(text.toCharArray(), 0); + + while (true) { + boolean next = searcher.next(); + boolean nextValueNull = searcherValueNull.next(); + + if (next && nextValueNull) { + assertTrue(searcher.begin == searcherValueNull.begin && searcher.length == searcherValueNull.length); + } else if (next || nextValueNull) { + assert false; + break; + } else { + break; + } + } + } + public void testTransmit() throws Exception { DoubleArrayTrie dat = CustomDictionary.DEFAULT.dat;