Gem 敏感词过滤

wear · December 14, 2012 · Last by vik replied at August 10, 2016 · 12297 hits

和谐宝典 和谐宝典用于检查输入是否包含中文或英文敏感词,并可替换为特殊字符。生活在天朝,和谐宝典必须人手必备。

特点

速度快,比常规的正则匹配要快 10 倍以上,具体性能可运行 benchmark 查看 可以输出检测到的敏感词,请看初始 简单,可根据需要方便的调整敏感词字库

https://github.com/wear/harmonious_dictionary

额。。。

及时雨啊,一直在找分词方面的资料

github.com 现在现在上不去了

这个给力!

不错 不错!

俺用 trie 实现了个,两万敏感词,一秒钟能过滤二三十万文本

哎,这个好

Github Download 就要被砍了,楼主找个别的地方放?官方公告: https://github.com/blog/1302-goodbye-uploads

另外感谢楼主

力挺鱼哥。

#6 楼 @watgon 有 demo 可以参考一下吗?

感谢楼主,刚好有这个需求

gem 'fast_trie', :require => "trie"

# encoding: utf-8
class SegTrie

  attr_accessor :trie

  def initialize
    self.trie = ::Trie.new
  end

  def all_words(str, offset = 0, words= [])
    words_a ||= []
    str ||= ''
    str.length.times do |i|
      self.word(str, i, words_a)
    end
    words_a
  end

  def word(str, offset = 0, words_a = [])
    len = 0
    while  offset + len < str.length
      status = self.trie.get(str[offset, (len + 1)])
      if status == 2
        len = len + 1
        words_a.push(str[offset, len])
      elsif status == 1
        len = len + 1
      elsif status == 3
        words_a.push(str[offset, len + 1])
        break
      else
        break
      end
    end
    words_a
  end

  def add_words(words)
    words.each_index do |i|
      self.add_word(words[i])
    end
  end

  def add_word(word)
    return false if word.blank?
    word.length.times do |i|
      next if i == 0
      self.trie.add(word[0, i], 1) if self.trie.get(word[0, i]).to_i < 1
    end
    status =self.trie.get(word).to_i
    if status == 1
      self.trie.add(word, 2)
    elsif !([2, 3].include?(status))
      self.trie.add(word, 3)
    end
  end

  def clear_words
    self.trie = ::Trie.new
  end

end

HarmoniousDictionary.clean(your_input),这个我觉得放在输出的地方比较好,在用户数据入库的时候尽量不要去做处理,希望能保留用户输入的原始数据,只要在显示的地方做一下就可以了

已用上,感谢楼主,感谢和谐社会!! 发现了一个不和谐因子:https://github.com/wear/harmonious_dictionary/issues/1

#12 楼 @watgon discuz 的敏感词也是用 AC 自动机实现的 我也是用这个来做的 实时过滤 效率还不错

无限欢喜~~~~

发个多正则引擎 ( http://nfabo.cn ):单趟扫描就可以识别出匹配到的正则 ID 集合,在 ( http://bbs.csdn.net/topics/390949259 ) 中,匹配性能达到 118M Byte 每秒

测试下 擦

You need to Sign in before reply, if you don't have an account, please Sign up first.