keyword_filter 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d1076af3ca630be1fe25c43cdefb78ef2dce12212a26cb4548c44428b39eab0f
4
- data.tar.gz: ca1a3bd544b399428980b34e9d9144fc054dfceea8b0ce24214916c9483b6463
3
+ metadata.gz: c855283a86e7f370ad3775fd996feed7e4a3679d868a1efcd389f95f6c7b07a2
4
+ data.tar.gz: 99f447a049f524f0afb55f1bb993d8ea527a5358a95b5b5d196a6b52de3ab0dc
5
5
  SHA512:
6
- metadata.gz: 07b5c96453ee6746f0128097955e5c9edd255798baa186d620b3c8ded285e210d3613c3fab41f5089de9be8a7c3f31af38016b5923bd4f5efcea60456990a666
7
- data.tar.gz: c38a17c64c7ca2291a105fbe79ac192a91467aec0e946d912c2181ab58bf44060ad3feea97527963f0b4c631b3ef4e0ef5b3bb52777844cd5f4612caf4e83c43
6
+ metadata.gz: 24100789e067b1d89ce5b5cd785399a77dc66cbf8e741e00e281f6df0e173536dfac63012423495b16ac1575bd077cfadbdd6e58093195ee2db89d39a03bacc2
7
+ data.tar.gz: cd2fa2dc9626e04433cb24e39a605cb950c2656fc40c3ce4e2392e50e96220db85e3d2e894593d7cea0d53cb239acb0a6341759fa09edc8761f43e6444886b52
@@ -2,22 +2,25 @@
2
2
 
3
3
  require "keyword_filter/version"
4
4
 
5
+ class String
6
+ SPECIFIC_SYMBOL = %w(
7
+ ~ ! @ # $ % ^ & * ( ) _ - + = [ ] { } | < > / ? ; : ' " , . \ |
8
+ 。 , 《 》 ? 、 「 」 【 】 ( ) ; ‘ ’ “ ” : ·
9
+ ).freeze
10
+
11
+ def is_ignore_word?
12
+ self.empty? || self == ' ' || SPECIFIC_SYMBOL.include?(self)
13
+ end
14
+ end
15
+
5
16
  module KeywordFilter
6
17
  extend self
7
-
8
18
  #
9
19
  # 广告词, 敏感词过滤, 采用 DFA 算法
10
20
  #
11
21
  attr_reader :dict
12
22
 
13
- SPECIFIC_SYMBOL = %w(
14
- ~ ! @ # $ % ^ & * ( ) _ - + = [ ] { } | < > / ? ; : ' " , . \ |
15
- 。 , 《 》 ? 、 「 」 【 】 ( ) ; ‘ ’ “ ” : ·
16
- ).freeze
17
-
18
- def clear
19
- @dict = {}
20
- end
23
+ alias_method :tree, :dict
21
24
 
22
25
  def add(*words)
23
26
  @dict ||= { is_end: false }
@@ -37,6 +40,7 @@ module KeywordFilter
37
40
  end
38
41
  end
39
42
  end
43
+ true
40
44
  end
41
45
 
42
46
  def filtered_words
@@ -45,21 +49,18 @@ module KeywordFilter
45
49
 
46
50
  def filter(text)
47
51
  filtered_words.clear
48
- tmp_dict = self.dict
49
52
 
50
- text.each_char do |ch|
51
- next if ch.blank? || ch == ' ' || SPECIFIC_SYMBOL.include?(ch)
52
- if next_dict = tmp_dict[ch]
53
- filtered_words << ch
54
- return true if next_dict[:is_end]
55
- tmp_dict = next_dict
56
- else
57
- filtered_words.clear
58
- tmp_dict = self.dict
53
+ (0..text.length - 1).each do |i|
54
+ tmp_dict = self.dict
55
+ text[i..-1].each_char do |ch|
56
+ next if ch.is_ignore_word?
59
57
  if next_dict = tmp_dict[ch]
60
58
  filtered_words << ch
61
59
  return true if next_dict[:is_end]
62
60
  tmp_dict = next_dict
61
+ else
62
+ filtered_words.clear
63
+ break
63
64
  end
64
65
  end
65
66
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module KeywordFilter
4
- VERSION = "0.1.0".freeze
4
+ VERSION = "0.1.1".freeze
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: keyword_filter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Spirit
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-19 00:00:00.000000000 Z
11
+ date: 2018-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry