class ProfanityFilterEngine::RegexpStrategy

Constants

DEFAULT_DELIMITER

Attributes

dictionary[R]
profanity_regexp[RW]

Public Class Methods

new(dictionary:, profanity_regexp: nil) click to toggle source
# File lib/profanity-filter/engines/regexp_strategy.rb, line 14
def initialize(dictionary:, profanity_regexp: nil)
  @dictionary = dictionary
  @profanity_regexp = profanity_regexp || build_profanity_regexp
end

Public Instance Methods

profane?(text) click to toggle source
# File lib/profanity-filter/engines/regexp_strategy.rb, line 23
def profane?(text)
  profanity_regexp.match?(text)
end
profane_words(text) click to toggle source
# File lib/profanity-filter/engines/regexp_strategy.rb, line 19
def profane_words(text)
  text.scan(profanity_regexp).uniq
end

Private Instance Methods

build_profanity_regexp() click to toggle source
# File lib/profanity-filter/engines/regexp_strategy.rb, line 29
def build_profanity_regexp
  regexp_list = dictionary.map do |word|
    Regexp.new("#{DEFAULT_DELIMITER}#{Regexp.escape(word)}#{DEFAULT_DELIMITER}")
  end

  Regexp.union(*regexp_list)
end