fix(dfa): 修复WordTree.addWord在关键词以停顿词结尾时词尾标记错误的问题 #4092
+36
−9
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #4091
问题描述
当使用
WordTree.addWord("hh(")
添加一个以停顿词(如'('
)结尾的关键词时,该关键词的合法前缀(如"hh"
)无法被matchAll
正确匹配,返回空列表。根本原因
addWord
方法在循环结束后,使用原始字符串的最后一个字符(currentChar
)调用parent.setEnd(currentChar)
。如果该字符被charFilter
过滤,则setEnd
会设置一个无效的结束标记(因为该字符并未真正插入到WordTree
中)。修复方案
引入
lastAcceptedChar
变量,仅在字符被charFilter
接受时更新它,并在最后使用该变量调用setEnd
,确保结束标记始终基于合法字符。验证
新增两个单元测试:
addWordWithTrailingFilteredCharTest
:验证结尾带过滤字符的情况。addWordWithMiddleFilteredCharTest
:验证中间带过滤字符的情况。已通过相关测试。