keyword_filter 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/keyword_filter.rb +20 -19
- data/lib/keyword_filter/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c855283a86e7f370ad3775fd996feed7e4a3679d868a1efcd389f95f6c7b07a2
|
4
|
+
data.tar.gz: 99f447a049f524f0afb55f1bb993d8ea527a5358a95b5b5d196a6b52de3ab0dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24100789e067b1d89ce5b5cd785399a77dc66cbf8e741e00e281f6df0e173536dfac63012423495b16ac1575bd077cfadbdd6e58093195ee2db89d39a03bacc2
|
7
|
+
data.tar.gz: cd2fa2dc9626e04433cb24e39a605cb950c2656fc40c3ce4e2392e50e96220db85e3d2e894593d7cea0d53cb239acb0a6341759fa09edc8761f43e6444886b52
|
data/lib/keyword_filter.rb
CHANGED
@@ -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
|
-
|
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.
|
51
|
-
|
52
|
-
|
53
|
-
|
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
|
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.
|
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-
|
11
|
+
date: 2018-04-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|