@@ -56,48 +56,64 @@ def replacement=(value)
56
56
def match? ( text )
57
57
return false unless text . to_s . size >= 3
58
58
@matchlist . each do |list_item |
59
- text . scan ( /\b #{ list_item } \b /i ) { |match | return true unless protected_by_exceptionlist? ( match ) or match == [ nil ] }
59
+ start_at = 0
60
+ text . scan ( /\b #{ list_item } \b /i ) do |match |
61
+ match_start = text [ start_at , text . size ] . index ( /\b #{ list_item } \b /i ) unless @exceptionlist . empty?
62
+ match_end = match_start + match . size unless @exceptionlist . empty?
63
+ unless match == [ nil ] then
64
+ return true if @exceptionlist . empty? or not protected_by_exceptionlist? ( match_start , match_end , text , start_at )
65
+ end
66
+ start_at = match_end + 1
67
+ end
60
68
end
61
69
false
62
70
end
63
71
72
+ def matched ( text )
73
+ words = [ ]
74
+ return words unless text . to_s . size >= 3
75
+ @matchlist . each do |list_item |
76
+ start_at = 0
77
+ text . scan ( /\b #{ list_item } \b /i ) do |match |
78
+ match_start = text [ start_at , text . size ] . index ( /\b #{ list_item } \b /i ) unless @exceptionlist . empty?
79
+ match_end = match_start + match . size unless @exceptionlist . empty?
80
+ unless match == [ nil ] then
81
+ words << match if @exceptionlist . empty? or not protected_by_exceptionlist? ( match_start , match_end , text , start_at )
82
+ end
83
+ start_at = match_end + 1
84
+ end
85
+ end
86
+ words . uniq
87
+ end
88
+
64
89
def sanitize ( text )
65
90
return text unless text . to_s . size >= 3
66
91
@matchlist . each do |list_item |
67
- text . gsub! /\b #{ list_item } \b /i do |match |
68
- if protected_by_exceptionlist? ( match ) then
92
+ start_at = 0
93
+ text . gsub! /\b #{ list_item } \b /i do |match |
94
+ match_start = text [ start_at , text . size ] . index ( /\b #{ list_item } \b /i ) unless @exceptionlist . empty?
95
+ match_end = match_start + match . size unless @exceptionlist . empty?
96
+ unless @exceptionlist . empty? or not protected_by_exceptionlist? ( match_start , match_end , text , start_at ) then
97
+ start_at = match_end + 1
69
98
match
70
99
else
100
+ start_at = match_end + 1
71
101
replace ( match )
72
102
end
73
103
end
74
104
end
75
105
text
76
106
end
77
107
78
- def matched ( text )
79
- words = [ ]
80
- return words unless text . to_s . size >= 3
81
- @matchlist . each do |list_item |
82
- text . scan ( /\b #{ list_item } \b /i ) { |match | words << match unless protected_by_exceptionlist? ( match ) or match == [ nil ] }
83
- end
84
- words . uniq
85
- end
86
-
87
- def protected_by_exceptionlist? ( text )
88
- @exceptionlist . each { |list_item | return true unless text . scan ( /\b #{ list_item } \b /i ) . empty? }
89
- return false
90
- end
91
-
92
108
private
93
109
94
110
# VALIDATIONS
95
111
96
112
def validate_list_content ( content )
97
113
case content
98
- when Array then not ( content . empty? ) || raise ( LanguageFilter ::EmptyContentList . new ( "List content array is empty." ) )
99
- when String then File . exists? ( content ) || raise ( LanguageFilter ::UnkownContentFile . new ( "List content file \" #{ content } \" can't be found." ) )
100
- when Pathname then content . exist? || raise ( LanguageFilter ::UnkownContentFile . new ( "List content file \" #{ content } \" can't be found." ) )
114
+ when Array then content . all? { | c | c . class == String } || raise ( LanguageFilter ::EmptyContentList . new ( "List content array is empty." ) )
115
+ when String then File . exists? ( content ) || raise ( LanguageFilter ::UnkownContentFile . new ( "List content file \" #{ content } \" can't be found." ) )
116
+ when Pathname then content . exist? || raise ( LanguageFilter ::UnkownContentFile . new ( "List content file \" #{ content } \" can't be found." ) )
101
117
when Symbol then
102
118
case content
103
119
when :default , :hate , :profanity , :sex , :violence then true
@@ -132,6 +148,16 @@ def load_list(filepath)
132
148
IO . readlines ( filepath ) . each { |line | line . gsub! ( /\n / , '' ) }
133
149
end
134
150
151
+ def protected_by_exceptionlist? ( match_start , match_end , text , start_at )
152
+ @exceptionlist . each do |list_item |
153
+ exception_start = text [ start_at , text . size ] . index ( /\b #{ list_item } \b /i )
154
+ if exception_start and exception_start <= match_start then
155
+ return true if exception_start + text [ start_at , text . size ] [ /\b #{ list_item } \b /i ] . size >= match_end
156
+ end
157
+ end
158
+ return false
159
+ end
160
+
135
161
# This was moved to private because users should just use sanitize for any content
136
162
def replace ( word )
137
163
case @replacement
0 commit comments