Class WordTree

All Implemented Interfaces:
Serializable, Cloneable, Map<Character,WordTree>

public class WordTree extends HashMap<Character,WordTree>
DFA(Deterministic Finite Automaton 确定有穷自动机) DFA单词树(以下简称单词树),常用于在某大段文字中快速查找某几个关键词是否存在。 单词树使用group区分不同的关键字集合,不同的分组可以共享树枝,避免重复建树。 单词树使用树状结构表示一组单词。 例如:红领巾,红河 构建树后为:
            红
            /\
          领  河
         /
       巾
 
其中每个节点都是一个WordTree对象,查找时从上向下查找
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Constructor Details

    • WordTree

      public WordTree()
      默认构造
    • WordTree

      public WordTree(int initialCapacity)
      指定初始化容量
      Parameters:
      initialCapacity - 初始容量,一般是关键词的数量
  • Method Details

    • setCharFilter

      public WordTree setCharFilter(Predicate<Character> charFilter)
      设置字符过滤规则,通过定义字符串过滤规则,过滤不需要的字符 当accept为false时,此字符不参与匹配
      Parameters:
      charFilter - 过滤函数
      Returns:
      this
    • addWords

      public WordTree addWords(Collection<String> words)
      增加一组单词
      Parameters:
      words - 单词集合
      Returns:
      this
    • addWords

      public WordTree addWords(String... words)
      增加一组单词
      Parameters:
      words - 单词数组
      Returns:
      this
    • addWord

      public WordTree addWord(String word)
      添加单词,使用默认类型
      Parameters:
      word - 单词
      Returns:
      this
    • isMatch

      public boolean isMatch(String text)
      指定文本是否包含树中的词
      Parameters:
      text - 被检查的文本
      Returns:
      是否包含
    • match

      public String match(String text)
      获得第一个匹配的关键字
      Parameters:
      text - 被检查的文本
      Returns:
      匹配到的关键字
    • matchWord

      public FoundWord matchWord(String text)
      获得第一个匹配的关键字
      Parameters:
      text - 被检查的文本
      Returns:
      匹配到的关键字
    • matchAll

      public List<String> matchAll(String text)
      找出所有匹配的关键字
      Parameters:
      text - 被检查的文本
      Returns:
      匹配的词列表
    • matchAllWords

      public List<FoundWord> matchAllWords(String text)
      找出所有匹配的关键字
      Parameters:
      text - 被检查的文本
      Returns:
      匹配的词列表
    • matchAll

      public List<String> matchAll(String text, int limit)
      找出所有匹配的关键字
      Parameters:
      text - 被检查的文本
      limit - 限制匹配个数,如果小于等于0,则返回全部匹配结果
      Returns:
      匹配的词列表
    • matchAllWords

      public List<FoundWord> matchAllWords(String text, int limit)
      找出所有匹配的关键字
      Parameters:
      text - 被检查的文本
      limit - 限制匹配个数,如果小于等于0,则返回全部匹配结果
      Returns:
      匹配的词列表
    • matchAll

      public List<String> matchAll(String text, int limit, boolean isDensityMatch, boolean isGreedMatch)
      找出所有匹配的关键字

      假如被检查文本是"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

      public List<String> flatten()
      扁平化WordTree 例如:红领巾,红河 构建树后为:
                  红
                  /\
                领  河
               /
             巾
       
      扁平化后得到
           红河
           红领巾
       
      Returns:
      扁平化后的结果,不保证顺序
    • clear

      public void clear()
      清除所有的词, 此方法调用后, wordTree 将被清空 endCharacterSet 也将清空
      Specified by:
      clear in interface Map<Character,WordTree>
      Overrides:
      clear in class HashMap<Character,WordTree>