Sha256: e1db3a3d9fbcb5c46d157bcfa00b434e68a845033f706313ee40168d2f4bd875
Contents?: true
Size: 1.55 KB
Versions: 5
Compression:
Stored size: 1.55 KB
Contents
require 'yaml' require 'fuzzy_hash' class Swearjar def self.default from_language end def self.from_language(language = 'en') new(File.join(File.dirname(__FILE__), 'config', "#{language}.yml")) end attr_reader :tester, :hash def initialize(file = nil) @tester = FuzzyHash.new @hash = {} load_file(file) if file end def load_file(file) data = YAML.load_file(file) data['regex'].each do |pattern, type| @tester[Regexp.new(pattern)] = type end if data['regex'] data['simple'].each do |test, type| @hash[test] = type end if data['simple'] end def scan(string, &block) string = string.to_s string.scan(/\b[a-zA-Z0-9-]+\b/) do |word| block.call(word, hash[word.downcase]) end if match = tester.match_with_result(string) block.call(match.last, match.first) end end def profane?(string) string = string.to_s scan(string) {|word, test| return true unless test.nil?} return false end def list_profanities(string) profanities = [] string = string.to_s scan(string) {|word, test| profanities << word if test } profanities end def scorecard(string) string = string.to_s 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.to_s.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
5 entries across 5 versions & 1 rubygems
Version | Path |
---|---|
cf-swearjar-1.1.1 | lib/swearjar.rb |
cf-swearjar-1.1.0 | lib/swearjar.rb |
cf-swearjar-1.0.9 | lib/swearjar.rb |
cf-swearjar-1.0.8 | lib/swearjar.rb |
cf-swearjar-1.0.7 | lib/swearjar.rb |