class Language::Censor

Censor

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.

Attributes

rules[R]

Abritraty rules.

word_rules[R]

Word-oriented rules.

Public Class Methods

default_words() click to toggle source

Default censor list.

# File lib/language/censor.rb, line 14
def self.default_words
  []
end
new() click to toggle source

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

Public Instance Methods

apply(string) click to toggle source
Alias for: filter
censored?(string) click to toggle source

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
filter(string) click to toggle source

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
Also aliased as: apply
matches() click to toggle source
# File lib/language/censor.rb, line 91
def matches
  rules.collect{ |match, modify| match }
end
rule(match, &edit) click to toggle source

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
word_rule(match, &edit) click to toggle source

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