【全程干货】程序员必备算法!AC自动机算法敏感词匹配算法!动画演示讲解,看完轻松掌握,面试官都被你唬住!!_哔哩哔哩_bilibili
著名的多模匹配算法
引入依赖:
<dependency> <groupId>org.ahocorasick</groupId> <artifactId>ahocorasick</artifactId> <version>0.6.3</version> </dependency>
public List<String> findKeywordsInText(String text, List<String> keywords) {
Trie trie = Trie.builder()
.addKeywords(keywords)
.build();
long start = System.currentTimeMillis();
Collection<Emit> emits = trie.parseText(text);
logger.info("trie: " + (System.currentTimeMillis() - start));
List<String> matchedKeywords = new ArrayList<>();
for (Emit emit : emits) {
matchedKeywords.add(emit.getKeyword());
}
return matchedKeywords;
}