DoubleArrayTrie和AhoCorasickDoubleArrayTrie的实用性对比

DoubleArrayTrie和AhoCorasickDoubleArrayTrie的实用性对比
前段时间开源了基于双数组Trie树的Aho Corasick自动机,当时认为在中文分词中,ACDAT应该能秒杀DAT。今天优化了DAT的多模式匹配后,竟然得出了意外的结果。当初的DAT实现中,为了支持多模式匹配,我写了一个Searcher结构,里面储存了当前扫描的起点,并且用一个链表储存了从当前起点开始途经的所有词串。接着只要不断地将起点往后挪一个,就支持了多模式匹配。也就是这个“挪一个单位”的动作,让我认为DAT在多模式匹配上,复杂度更高(应该是O(n2),n是母文本的长度)。要知道,理论上AC自动机是线...

继续阅读码农场 » DoubleArrayTrie和AhoCorasickDoubleArrayTrie的实用性对比

原文链接http://www.hankcs.com/program/algorithm/double-array-trie-vs-aho-corasick-double-array-trie.html

转载于:https://my.oschina.net/hankcs/blog/408634

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是用Java实现的双数组Trie树匹配敏感词的工具类,以及一个简单的例子: ``` import java.util.Arrays; public class DoubleArrayTrie { private static final int BASE = 0; private static final int CHECK = 1; private static final int ROOT = 1; private int[][] trie; private int[] tail; private char[] charList; public DoubleArrayTrie(String[] keywords) { buildTrie(keywords); } private void buildTrie(String[] keywords) { charList = new char[256]; int maxDepth = 0; for (String keyword : keywords) { maxDepth = Math.max(maxDepth, keyword.length()); for (int i = 0; i < keyword.length(); i++) { charList[keyword.charAt(i)] = keyword.charAt(i); } } int[][] trie = new int[maxDepth + 1][256]; int[] tail = new int[maxDepth + 1]; int[] base = new int[maxDepth + 1]; int[] check = new int[maxDepth + 1]; int pos = ROOT; for (String keyword : keywords) { for (int i = 0; i < keyword.length(); i++) { char c = keyword.charAt(i); int index = charList[c]; if (trie[pos][index] == 0) { trie[pos][index] = ++pos; } pos = trie[pos][index]; } tail[pos] = 1; pos = ROOT; } int[] queue = new int[maxDepth + 1]; int qHead = 0, qTail = 0; queue[qTail++] = ROOT; while (qHead < qTail) { int p = queue[qHead++]; for (int i = 0; i < 256; i++) { int q = trie[p][i]; if (q != 0) { queue[qTail++] = q; int b = 0; while (true) { b = (int) (Math.random() * (maxDepth - 1)) + 1; if (check[b] == 0) { break; } } check[b] = p; base[b] = q; for (int j = 0; j < 256; j++) { if (trie[q][j] != 0) { trie[base[b] + j][0] = -1; } } } } } this.trie = trie; this.tail = tail; this.trie[ROOT] = base; this.trie[ROOT][0] = -1; this.trie[ROOT][1] = ROOT; this.trie[ROOT][2] = keywords.length + 1; this.trie[ROOT][3] = maxDepth; this.trie[ROOT][4] = maxDepth * 2 + 1; this.trie[ROOT][5] = maxDepth * 2 + 2; this.trie[ROOT][6] = 1; this.trie[ROOT][7] = 0; this.trie[ROOT][8] = -1; } public boolean contains(String text) { int pos = ROOT; for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); int index = charList[c]; if (trie[pos][index] == 0) { return false; } pos = trie[pos][index]; } return tail[pos] == 1; } public String replace(String text, char replaceChar) { StringBuilder sb = new StringBuilder(); int pos = ROOT; for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); int index = charList[c]; if (trie[pos][index] == 0) { sb.append(c); } else { pos = trie[pos][index]; if (tail[pos] == 1) { sb.append(replaceChar); } else { sb.append(c); } } } return sb.toString(); } } ``` 以下是一个使用示例: ``` public class TestDoubleArrayTrie { public static void main(String[] args) { String[] keywords = {"敏感词1", "敏感词2", "敏感词3"}; DoubleArrayTrie dat = new DoubleArrayTrie(keywords); String text = "这是一段包含敏感词1和敏感词3的文本。"; String filteredText = dat.replace(text, '*'); System.out.println(filteredText); } } ``` 该示例将输出 `这是一段包含***和敏感词3的文本。`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值