Class: SensitiveWords
- Inherits:
-
Object
- Object
- SensitiveWords
- Defined in:
- lib/sensitive_words.rb
Constant Summary collapse
- @@dict =
{}
Class Method Summary collapse
- .get_dict_file_hash(path) ⇒ Object
- .load_dict(dict_path) ⇒ Object
- .sensitive_words(input, max = nil) ⇒ Object
Instance Method Summary collapse
-
#all_sensitive_words ⇒ Object
所有的敏感词.
-
#initialize(input) ⇒ SensitiveWords
constructor
A new instance of SensitiveWords.
-
#sensitive_words(max) ⇒ Object
只要有限个敏感词.
Constructor Details
#initialize(input) ⇒ SensitiveWords
Returns a new instance of SensitiveWords.
54 55 56 57 |
# File 'lib/sensitive_words.rb', line 54 def initialize(input) @input = input @words = [] end |
Class Method Details
.get_dict_file_hash(path) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/sensitive_words.rb', line 17 def get_dict_file_hash(path) tree = {} file = File.open(path, 'r') if file file.each_line do |line| line = line.chomp next if line.empty? node = nil line.chars.each do |c| if node node[c] ||= {} node = node[c] else tree[c] ||= {} node = tree[c] end end node[:end] = :id end end tree ensure file.close if file end |
.load_dict(dict_path) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/sensitive_words.rb', line 9 def load_dict(dict_path) new_dict = get_dict_file_hash(dict_path) dict = @@dict.merge new_dict @@dict = dict rescue Errno::ENOENT => boom puts "#{boom.class} - #{boom.}" end |
.sensitive_words(input, max = nil) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/sensitive_words.rb', line 42 def sensitive_words(input,max=nil) ins = SensitiveWords.new(input) max = max.to_i if max > 0 ins.sensitive_words(max) else ins.all_sensitive_words end end |
Instance Method Details
#all_sensitive_words ⇒ Object
所有的敏感词
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/sensitive_words.rb', line 79 def all_sensitive_words @node, @words = @@dict, [] @word, @queue = '', [] @input.chars.each do |char| loop do break if @queue.empty? chr = @queue.shift process_check(chr, true) end process_check(char) end process_check('') @words end |
#sensitive_words(max) ⇒ Object
只要有限个敏感词
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/sensitive_words.rb', line 60 def sensitive_words(max) @node, @words = @@dict, [] @word, @queue = '', [] @input.chars.each do |char| break if @words.size >= max loop do break if @queue.empty? chr = @queue.shift process_check(chr, true) end process_check(char) end process_check('') @words.first(max) end |