Class: ProfanityFilter
- Inherits:
-
Object
- Object
- ProfanityFilter
- Defined in:
- lib/profanity-filter.rb,
lib/profanity-filter/version.rb
Constant Summary collapse
- WP_DEFAULT_LANGS =
[:en].freeze
- WP_LANG_CONVERSIONS =
{ es: :sp, ko: :kr, ja: :jp }.freeze
- WP_AVAILABLE_LANGS =
[ :en, :ar, :fr, :de, :hi, :jp, :it, :pt, :ru, :sp, :th, :tr, :zh, :kr, :pa ].freeze
- LEET_STRATEGY =
:leet
- ALLOW_SYMBOL_STRATEGY =
:allow_symbol
- PARTIAL_MATCH_STRATEGY =
:partial_match
- DUPLICATE_CHARACTERS_STRATEGY =
:duplicate_characters
- VERSION =
'1.0'
Instance Attribute Summary collapse
-
#available_strategies ⇒ Object
readonly
Returns the value of attribute available_strategies.
Instance Method Summary collapse
- #all_strategy_names ⇒ Object
- #basic_strategy_names ⇒ Object
-
#initialize(web_purifier_api_key: nil, whitelist: []) ⇒ ProfanityFilter
constructor
A new instance of ProfanityFilter.
- #profane?(phrase, lang: nil, strategies: :basic) ⇒ Boolean
- #profanity_count(phrase, lang: nil, strategies: :basic) ⇒ Object
Constructor Details
#initialize(web_purifier_api_key: nil, whitelist: []) ⇒ ProfanityFilter
Returns a new instance of ProfanityFilter.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# 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 |
Instance Attribute Details
#available_strategies ⇒ Object (readonly)
Returns the value of attribute available_strategies.
23 24 25 |
# File 'lib/profanity-filter.rb', line 23 def available_strategies @available_strategies end |
Instance Method Details
#all_strategy_names ⇒ Object
54 55 56 |
# File 'lib/profanity-filter.rb', line 54 def all_strategy_names available_strategies.keys end |
#basic_strategy_names ⇒ Object
58 59 60 |
# File 'lib/profanity-filter.rb', line 58 def basic_strategy_names [ALLOW_SYMBOL_STRATEGY, PARTIAL_MATCH_STRATEGY] end |
#profane?(phrase, lang: nil, strategies: :basic) ⇒ Boolean
62 63 64 65 66 67 68 69 70 71 |
# 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) ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# 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 |