class ProfanityFilter
Constants
- ALLOW_SYMBOL_STRATEGY
- DUPLICATE_CHARACTERS_STRATEGY
- LEET_STRATEGY
- PARTIAL_MATCH_STRATEGY
- VERSION
- WP_AVAILABLE_LANGS
- WP_DEFAULT_LANGS
- WP_LANG_CONVERSIONS
Attributes
available_strategies[R]
Public Class Methods
new(web_purifier_api_key: nil, whitelist: [])
click to toggle source
# File lib/profanity-filter.rb, line 25 def initialize(web_purifier_api_key: nil, whitelist: []) # If we are using Web Purifier @wp_client = web_purifier_api_key ? WebPurify::Client.new(web_purifier_api_key) : nil @whitelist = whitelist raise 'Whitelist should be an array' unless @whitelist.is_a?(Array) exact_match_dictionary = load_exact_match_dictionary partial_match_dictionary = load_partial_match_dictionary @available_strategies = { ALLOW_SYMBOL_STRATEGY => ::ProfanityFilterEngine::AllowSymbolsInWordsStrategy.new( dictionary: exact_match_dictionary, ignore_case: true ), DUPLICATE_CHARACTERS_STRATEGY => ::ProfanityFilterEngine::AllowDuplicateCharactersStrategy.new( dictionary: exact_match_dictionary, ignore_case: true ), LEET_STRATEGY => ::ProfanityFilterEngine::LeetExactMatchStrategy.new( dictionary: exact_match_dictionary, ignore_case: true ), PARTIAL_MATCH_STRATEGY => ::ProfanityFilterEngine::PartialMatchStrategy.new( dictionary: partial_match_dictionary + exact_match_dictionary, ignore_case: true ), } end
Public Instance Methods
all_strategy_names()
click to toggle source
# File lib/profanity-filter.rb, line 54 def all_strategy_names available_strategies.keys end
basic_strategy_names()
click to toggle source
# File lib/profanity-filter.rb, line 58 def basic_strategy_names [ALLOW_SYMBOL_STRATEGY, PARTIAL_MATCH_STRATEGY] end
profane?(phrase, lang: nil, strategies: :basic)
click to toggle source
# File lib/profanity-filter.rb, line 62 def profane?(phrase, lang: nil, strategies: :basic) return false if phrase == '' return false if @whitelist.include?(phrase) if use_webpurify? !!(pf_profane?(phrase, strategies: strategies) || wp_profane?(phrase, lang: lang)) else !!pf_profane?(phrase, strategies: strategies) end end
profanity_count(phrase, lang: nil, strategies: :basic)
click to toggle source
# File lib/profanity-filter.rb, line 73 def profanity_count(phrase, lang: nil, strategies: :basic) return 0 if phrase == '' || phrase.nil? pf_count = pf_profanity_count(phrase, strategies: strategies) if use_webpurify? pf_count.zero? ? wp_profanity_count(phrase, lang: lang).to_i : pf_count else pf_count end end
Private Instance Methods
filter(strategies:)
click to toggle source
# File lib/profanity-filter.rb, line 90 def filter(strategies:) ::ProfanityFilterEngine::Composite.new.tap do |engine| case strategies when :all all_strategy_names.each { |s| engine.add_strategy(available_strategies[s]) } when :basic basic_strategy_names.each { |s| engine.add_strategy(available_strategies[s]) } else strategies.each do |s| raise "Strategy name \"#{s}\" not supported." unless all_strategy_names.include?(s) engine.add_strategy(available_strategies[s]) end end end end
load_dictionary(file_path)
click to toggle source
# File lib/profanity-filter.rb, line 147 def load_dictionary(file_path) dir = File.dirname(__FILE__) YAML.load(File.read("#{dir}/profanity-dictionaries/#{file_path}.yaml")) end
load_exact_match_dictionary()
click to toggle source
# File lib/profanity-filter.rb, line 152 def load_exact_match_dictionary en_dictionary = load_dictionary('en') es_dictionary = load_dictionary('es') pt_dictionary = load_dictionary('pt') en_dictionary + es_dictionary + pt_dictionary end
load_partial_match_dictionary()
click to toggle source
# File lib/profanity-filter.rb, line 159 def load_partial_match_dictionary load_dictionary('partial_match') end
pf_profane?(phrase, strategies:)
click to toggle source
# File lib/profanity-filter.rb, line 107 def pf_profane?(phrase, strategies:) filter(strategies: strategies).profane?(phrase) end
pf_profanity_count(phrase, strategies:)
click to toggle source
# File lib/profanity-filter.rb, line 111 def pf_profanity_count(phrase, strategies:) filter(strategies: strategies).profanity_count(phrase) end
shorten_language(lang)
click to toggle source
# File lib/profanity-filter.rb, line 163 def shorten_language(lang) lang && lang.to_s.downcase[0, 2] end
use_webpurify?()
click to toggle source
# File lib/profanity-filter.rb, line 86 def use_webpurify? !!@wp_client end
wp_langs_list_with(lang)
click to toggle source
# File lib/profanity-filter.rb, line 133 def wp_langs_list_with(lang) langs = Set.new(WP_DEFAULT_LANGS) if lang lang = shorten_language(lang).to_sym lang = WP_LANG_CONVERSIONS[lang] || lang if WP_AVAILABLE_LANGS.include?(lang) langs << lang end end langs.to_a.join(',') end
wp_profane?(phrase, lang: nil, timeout_duration: 5)
click to toggle source
# File lib/profanity-filter.rb, line 115 def wp_profane?(phrase, lang: nil, timeout_duration: 5) profanity_count = wp_profanity_count(phrase, lang: lang, timeout_duration: timeout_duration) if profanity_count.nil? || profanity_count == 0 false else true end end
wp_profanity_count(phrase, lang: nil, timeout_duration: 5)
click to toggle source
# File lib/profanity-filter.rb, line 125 def wp_profanity_count(phrase, lang: nil, timeout_duration: 5) Timeout::timeout(timeout_duration) do @wp_client.check_count phrase, lang: wp_langs_list_with(lang) end rescue StandardError nil end