Skip to content

Commit 0a90e0b

Browse files
committed
"I think it finally all works", he said warily.
1 parent c3ebc97 commit 0a90e0b

File tree

2 files changed

+50
-24
lines changed

2 files changed

+50
-24
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ For example:
148148

149149
``` ruby
150150
my_filter = LanguageFilter::Filter.new(
151-
matchlist: ['dog(s)?'],
151+
matchlist: ['dogs?'],
152152
exceptionlist: ['dogs drool'],
153153
replacement: :garbled
154154
)
@@ -158,7 +158,7 @@ my_filter.sanitize('Dogs rule, cats drool!')
158158
my_filter.sanitize('Cats rule, dogs drool!')
159159
=> "Cats rule, dogs drool!"
160160

161-
my_filter.matchlist = ['dog(s)?','cats drool']
161+
my_filter.matchlist = ['dogs?','cats drool']
162162
my_filter.exceptionlist = ['dogs drool','dogs are cruel']
163163
my_filter.replacement = :stars
164164

@@ -173,8 +173,8 @@ In the above case though, we just wanted to add items to the existing lists, so
173173
For example:
174174

175175
``` ruby
176-
my_filter.matchlist.delete "dogs drool"
177-
my_filter.matchlist << "dogs are mean"
176+
my_filter.matchlist.pop
177+
my_filter.matchlist << "cats are liars" << "don't listen to( the)? cats" << "why does no one heed my warnings about the cats?! aren't you getting my messages?"
178178
my_filter.matchlist.uniq!
179179
# etc...
180180
```

lib/language_filter.rb

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,48 +56,64 @@ def replacement=(value)
5656
def match?(text)
5757
return false unless text.to_s.size >= 3
5858
@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
6068
end
6169
false
6270
end
6371

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+
6489
def sanitize(text)
6590
return text unless text.to_s.size >= 3
6691
@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
6998
match
7099
else
100+
start_at = match_end + 1
71101
replace(match)
72102
end
73103
end
74104
end
75105
text
76106
end
77107

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-
92108
private
93109

94110
# VALIDATIONS
95111

96112
def validate_list_content(content)
97113
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."))
101117
when Symbol then
102118
case content
103119
when :default, :hate, :profanity, :sex, :violence then true
@@ -132,6 +148,16 @@ def load_list(filepath)
132148
IO.readlines(filepath).each {|line| line.gsub!(/\n/,'')}
133149
end
134150

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+
135161
# This was moved to private because users should just use sanitize for any content
136162
def replace(word)
137163
case @replacement

0 commit comments

Comments
 (0)