Class: BadWordDetector
- Inherits:
-
Object
- Object
- BadWordDetector
- Defined in:
- lib/bad_word_detector.rb,
lib/bad_word_detector/version.rb
Constant Summary collapse
- VERSION =
"0.0.5"
Instance Method Summary collapse
-
#find(text, return_white = false) ⇒ BadWord?
Searches string for some word in library.
-
#initialize(rules = nil, library = nil, whitelist = nil) ⇒ BadWordDetector
constructor
Create new badword checker.
- #inspect ⇒ Object
Constructor Details
#initialize(rules = nil, library = nil, whitelist = nil) ⇒ BadWordDetector
Create new badword checker
21 22 23 24 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/bad_word_detector.rb', line 21 def initialize(rules = nil, library = nil, whitelist = nil) confdir = File.(File.dirname(__FILE__) + "/conf") rules_file = if rules.is_a? String rules else "#{confdir}/rules.yaml" end library_file = if library.is_a? String library else "#{confdir}/library.yaml" end whitelist_file = if whitelist.is_a? String whitelist else "#{confdir}/whitelist.yaml" end unless rules.is_a? Hash rules = YAML.load_file(rules_file) end unless library.is_a? Array library = YAML.load_file(library_file) end unless whitelist.is_a? Array whitelist = YAML.load_file(whitelist_file) end @rule_sets = rules.select do |key, _| key.to_s.length == 1 end.hmap do |key, rule| key = key.to_s rule = rule.map do |item| Rule.new(key, item['symbol'], item['weight']) end rule << Rule.new(key, key, 3) [key, rule] end @string_sets = rules.select do |key, _| key.to_s.length > 1 end.hmap do |key, rule| key = key.to_s rule = rule.map do |item| Rule.new(key, item['symbol'], item['weight']) end [key, rule] end @library = PrefixTree.new library @whitelist = Whitelist.new whitelist @library.freeze @whitelist.freeze @rule_sets.freeze @string_sets.freeze self.freeze true end |
Instance Method Details
#find(text, return_white = false) ⇒ BadWord?
Searches string for some word in library
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/bad_word_detector.rb', line 87 def find(text, return_white = false) downcased = text.downcase length = text.length index = 0 while index < length found = find_part(downcased, index) if found word = BadWord.new( found[:word], text, index, found[:length], @whitelist) if not word.white? or return_white return word end end index += 1 end end |
#inspect ⇒ Object
107 108 109 |
# File 'lib/bad_word_detector.rb', line 107 def inspect "#<#{self.class.name}:#{self.object_id}>" end |