Package org.miaixz.bus.core.text.dfa
Class WordTree
- All Implemented Interfaces:
Serializable,Cloneable,Map<Character,WordTree>
DFA(Deterministic Finite Automaton 确定有穷自动机) DFA单词树(以下简称单词树),常用于在某大段文字中快速查找某几个关键词是否存在。
单词树使用group区分不同的关键字集合,不同的分组可以共享树枝,避免重复建树。 单词树使用树状结构表示一组单词。 例如:红领巾,红河 构建树后为:
红
/\
领 河
/
巾
其中每个节点都是一个WordTree对象,查找时从上向下查找- Since:
- Java 17+
- Author:
- Kimi Liu
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription添加单词,使用默认类型增加一组单词addWords(Collection<String> words) 增加一组单词voidclear()清除所有的词, 此方法调用后, wordTree 将被清空 endCharacterSet 也将清空flatten()扁平化WordTree 例如:红领巾,红河 构建树后为:boolean指定文本是否包含树中的词获得第一个匹配的关键字找出所有匹配的关键字找出所有匹配的关键字找出所有匹配的关键字matchAllWords(String text) 找出所有匹配的关键字matchAllWords(String text, int limit) 找出所有匹配的关键字matchAllWords(String text, int limit, boolean isDensityMatch, boolean isGreedMatch) 找出所有匹配的关键字获得第一个匹配的关键字static WordTree通过预定义的关键词构造单词树setCharFilter(Predicate<Character> charFilter) 设置字符过滤规则,通过定义字符串过滤规则,过滤不需要的字符 当accept为false时,此字符不参与匹配Methods inherited from class java.util.HashMap
clone, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, forEach, get, getOrDefault, isEmpty, keySet, merge, newHashMap, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, valuesMethods inherited from class java.util.AbstractMap
equals, hashCode, toString
-
Constructor Details
-
WordTree
public WordTree()默认构造 -
WordTree
public WordTree(int initialCapacity) 指定初始化容量- Parameters:
initialCapacity- 初始容量,一般是关键词的数量
-
-
Method Details
-
of
通过预定义的关键词构造单词树- Parameters:
words- 初始关键词- Returns:
- this
-
setCharFilter
设置字符过滤规则,通过定义字符串过滤规则,过滤不需要的字符 当accept为false时,此字符不参与匹配- Parameters:
charFilter- 过滤函数- Returns:
- this
-
addWords
增加一组单词- Parameters:
words- 单词集合- Returns:
- this
-
addWords
增加一组单词- Parameters:
words- 单词数组- Returns:
- this
-
addWord
添加单词,使用默认类型- Parameters:
word- 单词- Returns:
- this
-
isMatch
指定文本是否包含树中的词- Parameters:
text- 被检查的文本- Returns:
- 是否包含
-
match
获得第一个匹配的关键字- Parameters:
text- 被检查的文本- Returns:
- 匹配到的关键字
-
matchWord
获得第一个匹配的关键字- Parameters:
text- 被检查的文本- Returns:
- 匹配到的关键字
-
matchAll
找出所有匹配的关键字- Parameters:
text- 被检查的文本- Returns:
- 匹配的词列表
-
matchAllWords
找出所有匹配的关键字- Parameters:
text- 被检查的文本- Returns:
- 匹配的词列表
-
matchAll
找出所有匹配的关键字- Parameters:
text- 被检查的文本limit- 限制匹配个数,如果小于等于0,则返回全部匹配结果- Returns:
- 匹配的词列表
-
matchAllWords
找出所有匹配的关键字- Parameters:
text- 被检查的文本limit- 限制匹配个数,如果小于等于0,则返回全部匹配结果- Returns:
- 匹配的词列表
-
matchAll
找出所有匹配的关键字假如被检查文本是"abab" 密集匹配原则:假如关键词有 ab,b,将匹配 [ab,b,ab] 贪婪匹配(最长匹配)原则:假如关键字a,ab,最长匹配将匹配[a, ab]
- Parameters:
text- 被检查的文本limit- 限制匹配个数,如果小于等于0,则返回全部匹配结果isDensityMatch- 是否使用密集匹配原则isGreedMatch- 是否使用贪婪匹配(最长匹配)原则- Returns:
- 匹配的词列表
-
matchAllWords
public List<FoundWord> matchAllWords(String text, int limit, boolean isDensityMatch, boolean isGreedMatch) 找出所有匹配的关键字假如被检查文本是"abab" 密集匹配原则:假如关键词有 ab,b,将匹配 [ab,b,ab,b] 贪婪匹配(最长匹配)原则:假如关键字a,ab,最长匹配将匹配[ab]
- Parameters:
text- 被检查的文本limit- 限制匹配个数,如果小于等于0,则返回全部匹配结果isDensityMatch- 是否使用密集匹配原则isGreedMatch- 是否使用贪婪匹配(最长匹配)原则- Returns:
- 匹配的词列表
-
flatten
扁平化WordTree 例如:红领巾,红河 构建树后为:红 /\ 领 河 / 巾扁平化后得到红河 红领巾- Returns:
- 扁平化后的结果,不保证顺序
-
clear
public void clear()清除所有的词, 此方法调用后, wordTree 将被清空 endCharacterSet 也将清空
-