Module: FilterKeyWordOn
- Defined in:
- lib/filter_key_word_on/key_word.rb
Defined Under Namespace
Classes: KeyWordFilter
Class Method Summary collapse
- .filter_word_tree(object = nil) ⇒ Object
- .handle_word(do_words, replace = true, word_tree = nil, word_hash = nil) ⇒ Object
- .included(base) ⇒ Object
- .replacement_hash ⇒ Object
Class Method Details
.filter_word_tree(object = nil) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/filter_key_word_on/key_word.rb', line 18 def self.filter_word_tree(object = nil) word_tree = Array.new(256) << 0 object = replacement_hash if object.nil? object.each do |word,replace| temp = word_tree bytes = word.bytes.to_a len = bytes.size bytes.each_with_index do |asicc_code,arr_index| if arr_index < len - 1 if temp[asicc_code].nil? node = [Array.new(256),0] temp[asicc_code] = node elsif temp[asicc_code] == 1 node = [Array.new(256),1] temp[asicc_code] = node else end temp = temp[asicc_code][0] else temp[asicc_code] = 1 end end end [word_tree,0] end |
.handle_word(do_words, replace = true, word_tree = nil, word_hash = nil) ⇒ Object
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 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/filter_key_word_on/key_word.rb', line 45 def self.handle_word(do_words,replace = true,word_tree = nil,word_hash = nil) word_tree = filter_word_tree if word_tree.nil? word_hash = replacement_hash if word_hash.nil? temp = word_tree nodeTree = word_tree words = [] word = [] to_replace= [] a = 0 byte_words = do_words.bytes.to_a while a < byte_words.size index = byte_words[a] temp = temp[0][index] if temp.nil? temp = nodeTree a = a - word.size word = [] to_replace = [] elsif temp == 1 or temp[1] == 1 word << index to_replace << a words << word if replace replace_word = word_hash[word.pack("C*").force_encoding("UTF-8")].bytes.to_a byte_words[(a-to_replace.size + 1),to_replace.size] = replace_word a = (a - to_replace.size + 1) + (replace_word.size - 1) else a = a - word.size + 1 end word = [] to_replace = [] temp = nodeTree else word << index to_replace << a end a += 1 end return byte_words.pack("C*").force_encoding("UTF-8") if replace words.collect{|e| e.collect{|ch|ch.chr}.join } end |
.included(base) ⇒ Object
3 4 5 6 |
# File 'lib/filter_key_word_on/key_word.rb', line 3 def self.included(base) base.send :include, InstanceMethods base.send :extend, ClassMethods end |
.replacement_hash ⇒ Object
14 15 16 |
# File 'lib/filter_key_word_on/key_word.rb', line 14 def self.replacement_hash Hash[FilterKeyWordOn::KeyWordFilter.all.collect{|re| [re.content,re.replace] }] end |