class VocabBlacklist

Constants

BLACKLIST_DIR
CONSIDER_REGEX
FULL_WORDS
FULL_WORDS_CSV
GREEDY_WORDS
PHRASES
WHITELIST

Public Class Methods

blacklisted?(str, age = "0") click to toggle source

Returns true or false, check to see if the string is on the blacklist

# File lib/l2e_vocab_blacklist.rb, line 7
def self.blacklisted?(str, age = "0")
        # Sanitize string
        str = str.downcase.strip

        whitelisted_phrases = self.whitelist_matches(str)

        # Blacklist if any of the words
        str.split(/[ -]/).each do |word|
                word = word.gsub(CONSIDER_REGEX, "")

                if check_full_words_csv(word, age)

                        is_whitelisted = whitelisted_phrases.any? do |phrase|
                                phrase.include?(word)
                        end
                        if !is_whitelisted
                                return true
                        end
                end
        end
        # For compound dirty words
        PHRASES.each do |bad_phrase|
                return true if str.include?(bad_phrase)
        end
        
        return GREEDY_WORDS.any? { |s| str.include?(s) }
end
censor(str, age = "0", replace_with = "*") click to toggle source
# File lib/l2e_vocab_blacklist.rb, line 43
def self.censor(str, age = "0", replace_with = "*")
        PHRASES.each do |bad_phrase|
                # match number of characters for any replace_with that is 1 character
                if replace_with.length == 1
                        str.gsub!(/#{ bad_phrase }/i, replace_with * bad_phrase.length)
                else
                        str.gsub!(/#{ bad_phrase }/i, replace_with)
                end
        end

        whitelisted_phrases = self.whitelist_matches(str)

        str.split(/ /).map do |working_word|
                working_word.split(/-/).map do |sub_working_word|
                        word = sub_working_word.downcase.gsub(CONSIDER_REGEX, "")

                        is_whitelisted = whitelisted_phrases.any? do |phrase|
                                phrase.include?(word)
                        end

                        if !is_whitelisted
                                if check_full_words_csv(word, age)
                                        # match number of characters for any replace_with that is 1 character
                                        if replace_with.length == 1
                                                sub_working_word.gsub!(/#{ word }/i, replace_with * word.length)
                                        else
                                                sub_working_word.gsub!(/#{ word }/i, replace_with)
                                        end
                                end

                                if GREEDY_WORDS.any? { |w| word.include?(w) }
                                        # match number of characters for any replace_with that is 1 character
                                        if replace_with.length == 1
                                                sub_working_word = replace_with * sub_working_word.length
                                        else
                                                sub_working_word = replace_with
                                        end
                                end
                        end

                        sub_working_word
                end.join("-")
        end.join(" ")
end
file_to_normalized_words(file) click to toggle source
# File lib/l2e_vocab_blacklist.rb, line 88
def self.file_to_normalized_words(file)
        CSV.parse(File.read(file)).map(&:first).reject { |s| s.to_s.strip.empty? }.map(&:downcase).map { |s| s.gsub(CONSIDER_REGEX, "") }
end
whitelist_matches(text) click to toggle source
# File lib/l2e_vocab_blacklist.rb, line 35
def self.whitelist_matches(text)
        text = text.downcase.strip.gsub(CONSIDER_REGEX, "")
        WHITELIST.select do |whitelist_phrase|
                text.include?(whitelist_phrase)
        end

end
words_with_expansions(words) click to toggle source
# File lib/l2e_vocab_blacklist.rb, line 92
def self.words_with_expansions(words)
        words.map { |s| [s, s.pluralize, s.singularize] }.uniq.flatten
end

Private Class Methods

check_full_words_csv(word, age) click to toggle source
# File lib/l2e_vocab_blacklist.rb, line 108
def self.check_full_words_csv(word, age)
        FULL_WORDS_CSV.each do |row|
                return true  if row[1].to_i >= age.to_i && row[0].downcase == word.downcase
        end
        return false
end