module RussianObscenity

Constants

DEFAULT_DICTIONARY
VERSION

Public Instance Methods

dictionary() click to toggle source
# File lib/russian_obscenity/base.rb, line 22
def dictionary
  @dictionary_list
end
dictionary=(list) click to toggle source
# File lib/russian_obscenity/base.rb, line 26
def dictionary=(list)
  list = [:default] if list.nil? or list.empty?
  @dictionary_list = list.uniq.compact
  reload_regexp
end
find(text) click to toggle source
# File lib/russian_obscenity/base.rb, line 18
def find(text)
  text.gsub(regexp).map {|word| word}.uniq
end
obscene?(text) click to toggle source
# File lib/russian_obscenity/base.rb, line 10
def obscene?(text)
  (text =~ regexp) ? true : false
end
sanitize(text, replace = '*') click to toggle source
# File lib/russian_obscenity/base.rb, line 14
def sanitize(text, replace = '*')
  text.gsub(regexp, replace)
end

Private Instance Methods

make_regexp(dict_list) click to toggle source
# File lib/russian_obscenity/base.rb, line 43
def make_regexp(dict_list)
  words = []
  dict_list.each do |dict|
    dict = DEFAULT_DICTIONARY if dict == :default
    words << YAML.load_file(dict)
  end
  Regexp.new prepare_regexp_str(words), Regexp::IGNORECASE
end
prepare_regexp_str(words) click to toggle source
# File lib/russian_obscenity/base.rb, line 52
def prepare_regexp_str(words)
  reg = words.join('|')
  reg.gsub!(/\\W/, '[^a-zA-Z0-9а-яА-ЯёЁ_]') # because \W in ruby support only ASCII symbols
  reg.gsub!(/\\w/, '[a-zA-Z0-9а-яА-ЯёЁ]')   # because \w in ruby support only ASCII symbols
  '(\b|(?<=_))(' + reg + ')(\b|(?<=_))'
end
regexp() click to toggle source
# File lib/russian_obscenity/base.rb, line 34
def regexp
  reload_regexp if @regexp.nil?
  @regexp
end
reload_regexp() click to toggle source
# File lib/russian_obscenity/base.rb, line 39
def reload_regexp
  @regexp = make_regexp(@dictionary_list)
end