Class WordTree

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

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

    • WordTree

      public WordTree()
      默认构造
  • Method Details

    • setCharFilter

      public WordTree setCharFilter(Predicate<Character> predicate)
      设置字符过滤规则,通过定义字符串过滤规则,过滤不需要的字符 当accept为false时,此字符不参与匹配
      Parameters:
      predicate - 过滤函数
      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:
      匹配到的关键字
    • matchAll

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

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

      public List<String> matchAll(String text, int limit, boolean isDensityMatch, boolean isGreedMatch)
      找出所有匹配的关键字 密集匹配原则:假如关键词有 ab,b,文本是abab,将匹配 [ab,b,ab] 贪婪匹配(最长匹配)原则:假如关键字a,ab,最长匹配将匹配[a, ab]
      Parameters:
      text - 被检查的文本
      limit - 限制匹配个数
      isDensityMatch - 是否使用密集匹配原则
      isGreedMatch - 是否使用贪婪匹配(最长匹配)原则
      Returns:
      匹配的词列表
    • clear

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