This class allows one to define a resuable text filter. This is useful for removing or replacing curse words or senstive information from user input.
Abritraty rules.
Word-oriented rules.
Default censor list.
# File lib/language/censor.rb, line 14 def self.default_words [] end
New Censor object.
# File lib/language/censor.rb, line 26 def initialize() @rules = [] @word_rules = [] self.class.default_words.each do |word| word_rule(word) end end
Is the string clear of any matching rules?
Note that running a filter does not necessarily clear a a string of all matches, since the filter could apply edits that would also match the filter expressions.
# File lib/language/censor.rb, line 80 def censored?(string) case string when *matches false else true end end
Apply the set of rules (regular expression matches) to a string.
# File lib/language/censor.rb, line 64 def filter(string) rewritten_string = string.dup rules.each do |match,edit| rewritten_string.gsub!(match,edit) end return (rewritten_string or string) end
# File lib/language/censor.rb, line 91 def matches rules.collect{ |match, modify| match } end
Create new rule. A rule consists of a string or regexp to match against.
NOTE: The rules must be applied in order! So we cannot use a hash because the ordering is not guaranteed. So an array is used instead.
# File lib/language/censor.rb, line 42 def rule(match, &edit) edit = lambda{''} unless edit @rules << [match, edit] end
Rules that apply only to words. This takes the regular expression and add word boundry matches to either side.
filter.word_rule(/damn/){ |w| 'darn' }
Is equivalent to teh regular rule:
filter.rule(/\bdamn\b/){ |w| 'darn' }
# File lib/language/censor.rb, line 56 def word_rule(match, &edit) edit = lambda{''} unless edit @word_rules << [/\b#{match}\b/, edit] end