public class WordTree extends HashMap<Character,WordTree>
红 /\ 领 河 / 巾其中每个节点都是一个WordTree对象,查找时从上向下查找。
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
Constructor and Description |
---|
WordTree()
默认构造
|
WordTree(int initialCapacity)
指定初始化容量
|
Modifier and Type | Method and Description |
---|---|
WordTree |
addWord(String word)
添加单词,使用默认类型
|
WordTree |
addWords(Collection<String> words)
增加一组单词
|
WordTree |
addWords(String... words)
增加一组单词
|
void |
clear()
清除所有的词,
此方法调用后, wordTree 将被清空
endCharacterSet 也将清空
|
List<String> |
flatten()
扁平化WordTree
例如:红领巾,红河 构建树后为:
红
/\
领 河
/
巾
扁平化后得到
红河
红领巾
|
boolean |
isMatch(String text)
指定文本是否包含树中的词
|
String |
match(String text)
获得第一个匹配的关键字
|
List<String> |
matchAll(String text)
找出所有匹配的关键字
|
List<String> |
matchAll(String text,
int limit)
找出所有匹配的关键字
|
List<String> |
matchAll(String text,
int limit,
boolean isDensityMatch,
boolean isGreedMatch)
找出所有匹配的关键字
假如被检查文本是"abab" 密集匹配原则:假如关键词有 ab,b,将匹配 [ab,b,ab] 贪婪匹配(最长匹配)原则:假如关键字a,ab,最长匹配将匹配[a, ab] |
List<FoundWord> |
matchAllWords(String text)
找出所有匹配的关键字
|
List<FoundWord> |
matchAllWords(String text,
int limit)
找出所有匹配的关键字
|
List<FoundWord> |
matchAllWords(String text,
int limit,
boolean isDensityMatch,
boolean isGreedMatch)
找出所有匹配的关键字
假如被检查文本是"abab" 密集匹配原则:假如关键词有 ab,b,将匹配 [ab,b,ab,b] 贪婪匹配(最长匹配)原则:假如关键字a,ab,最长匹配将匹配[ab] |
FoundWord |
matchWord(String text)
获得第一个匹配的关键字
|
static WordTree |
of(String... words)
通过预定义的关键词构造单词树
|
WordTree |
setCharFilter(Predicate<Character> charFilter)
设置字符过滤规则,通过定义字符串过滤规则,过滤不需要的字符
当accept为false时,此字符不参与匹配 |
clone, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, forEach, get, getOrDefault, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
equals, hashCode, toString
public WordTree()
public WordTree(int initialCapacity)
initialCapacity
- 初始容量,一般是关键词的数量public static WordTree of(String... words)
words
- 初始关键词public WordTree setCharFilter(Predicate<Character> charFilter)
charFilter
- 过滤函数public WordTree addWords(Collection<String> words)
words
- 单词集合public boolean isMatch(String text)
text
- 被检查的文本public FoundWord matchWord(String text)
text
- 被检查的文本public List<String> matchAll(String text)
text
- 被检查的文本public List<FoundWord> matchAllWords(String text)
text
- 被检查的文本public List<String> matchAll(String text, int limit)
text
- 被检查的文本limit
- 限制匹配个数,如果小于等于0,则返回全部匹配结果public List<FoundWord> matchAllWords(String text, int limit)
text
- 被检查的文本limit
- 限制匹配个数,如果小于等于0,则返回全部匹配结果public List<String> matchAll(String text, int limit, boolean isDensityMatch, boolean isGreedMatch)
假如被检查文本是"abab"
密集匹配原则:假如关键词有 ab,b,将匹配 [ab,b,ab]
贪婪匹配(最长匹配)原则:假如关键字a,ab,最长匹配将匹配[a, ab]
text
- 被检查的文本limit
- 限制匹配个数,如果小于等于0,则返回全部匹配结果isDensityMatch
- 是否使用密集匹配原则isGreedMatch
- 是否使用贪婪匹配(最长匹配)原则public List<FoundWord> matchAllWords(String text, int limit, boolean isDensityMatch, boolean isGreedMatch)
假如被检查文本是"abab"
密集匹配原则:假如关键词有 ab,b,将匹配 [ab,b,ab,b]
贪婪匹配(最长匹配)原则:假如关键字a,ab,最长匹配将匹配[ab]
text
- 被检查的文本limit
- 限制匹配个数,如果小于等于0,则返回全部匹配结果isDensityMatch
- 是否使用密集匹配原则isGreedMatch
- 是否使用贪婪匹配(最长匹配)原则public List<String> flatten()
红 /\ 领 河 / 巾扁平化后得到
红河 红领巾
Copyright © 2025. All rights reserved.