Sha256: d2a04e649d6154f7b430410b774f599437bf7e50a9a9490905a18a07d815c8ed
Contents?: true
Size: 1.7 KB
Versions: 1
Compression:
Stored size: 1.7 KB
Contents
require 'yaml' require 'fuzzy_hash' class SwearJar @settings = {} def self.default self["default"] end def self.[](key) setup! words = @settings[key] if words new(words) else raise "No SwearJar is configured for #{key}" end end def self.setup! return unless @settings.empty? file = File.join(File.dirname(__FILE__), 'config', "#{language ||= "en"}.yml") YAML.load_file(file).each do |key, inner_config| scoped = {:words => {}, :phrases => FuzzyHash.new} # cycle through each config inner_config.each do |word, matches| if word.include?(" ") scoped[:phrases][Regexp.new(word, :case_insensitive => true)] = matches else scoped[:words][word] = matches end end @settings[key] = scoped end end def initialize(scoped) @scoped = scoped || {} end def scan(string, &block) # match words string.scan(/\b[a-zA-Z-]+\b/) do |word| if match = @scoped[:words][word.downcase] block.call(word, match) end end if match = @scoped[:phrases].match_with_result(string) block.call(match.last, match.first) end end def profane?(string) scan(string) {|word, test| return true unless test.nil?} return false end alias_method :match?, :profane? def scorecard(string) scorecard = {} scan(string) {|word, test| test.each { |type| scorecard.key?(type) ? scorecard[type] += 1 : scorecard[type] = 1} if test} scorecard end def censor(string) censored_string = string.dup scan(string) {|word, test| censored_string.gsub!(word, block_given? ? yield(word) : word.gsub(/\S/, '*')) if test} censored_string end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
zaarly-swearjar-0.2.2 | lib/swearjar.rb |