Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DoubleArrayTrie里的LongestSearcher的next方法需要进行强化,当传入的treemap的value为null时… #1674

Merged
merged 1 commit into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -1309,15 +1309,15 @@ 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]
b = base[p]; // 转移成功
else
{
if (begin == arrayLength) break;
if (value != null)
if (length > 0)
{
i = begin + length; // 输出最长词后,从该词语的下一个位置恢复扫描
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,37 @@ public void testLongestSearcher() throws Exception
}
}

public void testLongestSearcherWithNullValue() {
TreeMap<String, String> buildFrom = new TreeMap<String, String>();
TreeMap<String, String> buildFromValueNull = new TreeMap<String, String>();
String[] keys = new String[]{"he", "her", "his"};
for (String key : keys) {
buildFrom.put(key, key);
buildFromValueNull.put(key, null);
}
DoubleArrayTrie<String> trie = new DoubleArrayTrie<String>(buildFrom);
DoubleArrayTrie<String> trieValueNull = new DoubleArrayTrie<String>(buildFromValueNull);

String text = "her3he6his-hers! ";

DoubleArrayTrie<String>.LongestSearcher searcher = trie.getLongestSearcher(text.toCharArray(), 0);
DoubleArrayTrie<String>.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<CoreDictionary.Attribute> dat = CustomDictionary.DEFAULT.dat;
Expand Down