贴一个我的吧
# frozen_string_literal: true
# DFA 算法实现敏感词扫描
module Yfxs
class SensitiveWords
@@sensitiveMap = Hash.new
class << SensitiveWords
def addWord(word)
nowMap = @@sensitiveMap
for i in 0..word.strip.length - 1
c = word[i].chr
if nowMap.has_key? c
nowMap = nowMap[c]
else
child = Hash.new
child["end"] = 0
nowMap[c] = child
nowMap = nowMap[c]
end
end
nowMap["end"] = 1
end
def initWordMap
return true if @@sensitiveMap.size > 0
stop_words = Setting.stop_words.uniq
stop_words.each_with_index do |word, index|
addWord word
end
true
end
def p
initWordMap
@@sensitiveMap.size
end
def scan(word)
initWordMap
word = word.to_s
words = Array.new
for i in 0..word.length - 1
c = word[i]
if @@sensitiveMap.has_key? c
theword = "".freeze
nowMap = @@sensitiveMap
for j in i..word.length - 1
c = word[j]
if nowMap.has_key? c
theword += c
nowMap = nowMap[c]
if nowMap['end'] == 1
words.push theword
end
else
break
end
end
end
end
return words
end
end
end
end
在 rails 内写个 自定义的 validates 丢到 concern 用起来更顺手
# frozen_string_literal: true
module WithSensitiveWords
extend ActiveSupport::Concern
module ClassMethods
def sensitive_words_safe(*args)
args.each do |key|
define_method("#{key.to_s}_without_stopwords") do
Yfxs::Stopword.safe self[key]
end
end
end
def sensitive_words_validates(*args)
validates_each args do |record, attr, value|
# record.errors.add(attr, '包含不允许出现的词语') if Yfxs::SensitiveWords.scan(value).length > 0
if Yfxs::SensitiveWords.scan(value.to_s).length > 0
record.errors[:base] << "请不要发布会给我们带来伤害的内容"
record.errors.add(attr, "包含不允许出现的词语")
end
end
end
end
end
# frozen_string_literal: true
module Yfxs
class Stopword
def self.safe(text)
words = SensitiveWords.scan text
if words.present?
words.each_with_index do |word, index|
text = text.gsub(/#{word}/, "***")
end
end
text
end
end
end
# frozen_string_literal: true
class Comment < ApplicationRecord
include WithSensitiveWords
belongs_to :commentable, polymorphic: true
belongs_to :user, optional: true
sensitive_words_safe :title
sensitive_words_validates :body
end
Android studio 香!
这是个经典问题了。正向遍历的过程中改变了集合的长度
一边从桶里舀水出来,一边把桶高锯掉一些。
这种情况类 for 循环需要倒着遍历,或者用 while … do 这样的循环
过过过儿过过的生活?
这个叫 四舍六入五成双
哦哦,谢谢~
另外,看着是直接用了 es6 啊,现在流行直接上 es6 了啊,感觉跟不上潮流了
线上的 js 是 asset pipeline 还是 webpacker 在跑?怎么看着 js 没压缩啊
啊对,打错了

不错不错
rails 6.0.2.1 跑在 ruby 2.7.0 上,warning 太多了
哎,差点都想找楼主,买点他/她爷爷的手工茶叶呢 😄
马良你好!
不是我的,是偶然在 github 发现的
让 session 失效这个想法可以换成让 session 内保存的内容失效
我觉得这个叫 sexy 😄
平安云?
投一份简历试试
辛苦了👍
dart 大法好!😄
说的不错
还有 flutter 全家桶呢😄
好棒的工具!
这里还有一个 Elixir 的 https://github.com/samphilipd/rihanna
有苗不愁长,有坑不怕填!