Class: Profanalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/profanalyzer.rb

Overview

SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Constant Summary collapse

VERSION =
"1.2.0"
DEFAULT_TOLERANCE =
4
FULL =
YAML::load_file(File.dirname(__FILE__)+"/../config/list.yml")
RACIST =
FULL.select {|w| w[:racist]}
SEXUAL =
FULL.select {|w| w[:sexual]}
DEFAULT_SETTINGS =
{:racism => :forbidden,
:sexual => :forbidden,
:profane => :forbidden,
:tolerance => DEFAULT_TOLERANCE,
:custom_subs => {}}
DEFAULT_INSTANCE =
new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings = DEFAULT_SETTINGS) ⇒ Profanalyzer

Returns a new instance of Profanalyzer.



129
130
131
# File 'lib/profanalyzer.rb', line 129

def initialize(settings=DEFAULT_SETTINGS)
  @settings = DEFAULT_SETTINGS
end

Class Method Details

.forward_to_default(*methods) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/profanalyzer.rb', line 119

def self.forward_to_default(*methods)
  methods.each do |method|
    singleton_class.class_eval do
      define_method method do |*args|
        DEFAULT_INSTANCE.send(method, *args)
      end
    end
  end
end

.singleton_classObject



115
116
117
# File 'lib/profanalyzer.rb', line 115

def self.singleton_class
  class << self; self; end
end

Instance Method Details

#check_all=(check) ⇒ Object

Sets Profanalyzer to scan (or not scan) for all profane words, based on the set tolerance. This is set to true by default.



340
341
342
# File 'lib/profanalyzer.rb', line 340

def check_all=(check)
  @settings[:profane] = (check) ? :forbidden : :ignore
end

#check_racist=(check) ⇒ Object

Sets Profanalyzer to scan (or not scan) for racist words, based on the set tolerance. This is set to true by default.



328
329
330
# File 'lib/profanalyzer.rb', line 328

def check_racist=(check)
  @settings[:racism] = (check) ? :forbidden : :ignore
end

#check_sexual=(check) ⇒ Object

Sets Profanalyzer to scan (or not scan) for sexual words, based on the set tolerance. This is set to true by default.



334
335
336
# File 'lib/profanalyzer.rb', line 334

def check_sexual=(check)
  @settings[:sexual] = (check) ? :forbidden : :ignore
end

#filter(*args) ⇒ Object

Filters the provided string using the currently set rules, with #!@$%-like characters substituted in.

Example:

Profanalyzer.filter("shit") #==> "#!$%"

With Custom Substitutions:

Profanalyzer.substitute("shit","shiat")
Profanalyzer.filter("shit") #==> "shiat"
Profanalyzer.filter("damn") #==> "#!$%"

You can pass options to the method itself:

Profanalyzer.filter("you're a mick", :racist => false) #==> "you're a mick"

Available options:

:all

Set to true or false to specify checking all words in the blacklist

:sexual

Set to true or false to specify sexual checking

:racist

Set to true or false to specify racial slur checking

:tolerance

Sets the tolerance. 0-5.



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/profanalyzer.rb', line 268

def filter(*args)
  str = args[0]
  if (args.size > 1 && args[1].is_a?(Hash))
    oldsettings = @settings
    self.update_settings_from_hash args[1]
  end
  
  retstr = str
  
  @settings[:custom_subs].each do |k,v|
    retstr.gsub!(/\b#{k.to_s}\b/i,v.to_s)
  end
  
  banned_words = Profanalyzer.forbidden_words_from_settings
  banned_words.each do |word|
    retstr.gsub!(/\b#{word}\b/i,
        "#!$%@&!$%@%@&!$#!$%@&!$%@%@&!#!$%@&!$%@%@&!"[0..(word.length-1)])
  end
  @settings = oldsettings if oldsettings
  retstr
end

#flagged_words(*args) ⇒ Object

Returns an array of words that match the currently set rules against the provided string. The array will be empty if no words are matched.

Example:

Profanalyzer.flagged_words("shit damn foo") #==> ["shit", "damn"] 
Profanalyzer.flagged_words("profanalyzer is rad!") #==> []

With custom settings

Profanalyzer.check_all = false
Profanalyzer.check_racist = false
Profanalyzer.flagged_words("you're a mick") #==> []

You can pass options to the method itself:

Profanalyzer.flagged_words("you're a mick", :racist => false) #==> []

Available options:

:all

Set to true or false to specify checking all words in the blacklist

:sexual

Set to true or false to specify sexual checking

:racist

Set to true or false to specify racial slur checking

:tolerance

Sets the tolerance. 0-5.



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/profanalyzer.rb', line 227

def flagged_words(*args)
  flagged_words = []
  str = args[0]

  if (args.size > 1 && args[1].is_a?(Hash))
    oldsettings = @settings
    update_settings_from_hash args[1]
  end

  banned_words = forbidden_words_from_settings
  banned_words.each do |word|
    if str =~ /\b#{word}\b/i
      flagged_words << word
    end
  end
  @settings = oldsettings if oldsettings
  return flagged_words
end

#forbidden_words_from_settingsObject

:nodoc:



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/profanalyzer.rb', line 135

def forbidden_words_from_settings # :nodoc:
  banned_words = []
  
  FULL.each do |word|
    banned_words << word[:word] if @settings[:tolerance] <= word[:badness]
  end if @settings[:profane] == :forbidden
  
  return banned_words if @settings[:profane] == :forbidden #save some processing
  
  RACIST.each do |word|
    banned_words << word[:word] if @settings[:tolerance] <= word[:badness]
  end if @settings[:racism] == :forbidden
  
  SEXUAL.each do |word|
    banned_words << word[:word] if @settings[:tolerance] <= word[:badness]
  end if @settings[:sexual] == :forbidden
  banned_words
end

#profane?(*args) ⇒ Boolean

Decides whether the given string is profane, given Profanalyzer’s current settings. Examples:

Profanalyzer.profane?("you're an asshole") #==> true

With custom settings

Profanalyzer.check_all = false
Profanalyzer.check_racist = false
Profanalyzer.profane?("you're a mick") #==> false

You can pass options to the method itself:

Profanalyzer.profane?("you're a mick", :racist => false) #==> false

Available options:

:all

Set to true or false to specify checking all words in the blacklist

:sexual

Set to true or false to specify sexual checking

:racist

Set to true or false to specify racial slur checking

:tolerance

Sets the tolerance. 0-5.

Returns:

  • (Boolean)


188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/profanalyzer.rb', line 188

def profane?(*args)
  str = args[0]
  if (args.size > 1 && args[1].is_a?(Hash))
    oldsettings = @settings
    update_settings_from_hash args[1]
  end
  banned_words = forbidden_words_from_settings
  banned_words.each do |word|
    if str =~ /\b#{word}\b/i
      @settings = oldsettings if oldsettings
      return true
    end
  end
  @settings = oldsettings if oldsettings
  false
end

#strip(*args) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/profanalyzer.rb', line 291

def strip(*args)
  str = args[0]
  if (args.size > 1 && args[1].is_a?(Hash))
    oldsettings = @settings
    self.update_settings_from_hash args[1]
  end
  
  retstr = str
  
  @settings[:custom_subs].each do |k,v|
    retstr.gsub!(/\b#{k.to_s}\b/i,v.to_s)
  end
  
  banned_words = Profanalyzer.forbidden_words_from_settings
  banned_words.each do |word|
    retstr.gsub!(/\b#{word}\b/i,"")
  end
  @settings = oldsettings if oldsettings
  retstr
end

#substitute(*args) ⇒ Object

Sets a custom substitution for the filter. Can be passed as substitute(“foo”,“bar”) or “foo” => “bar”



352
353
354
355
356
357
358
359
# File 'lib/profanalyzer.rb', line 352

def substitute(*args)
  case args[0]
  when String
    @settings[:custom_subs].merge!(args[0] => args[1])
  when Hash
    @settings[:custom_subs].merge!(args[0])
  end
end

#substitutions=(hash) ⇒ Object

Sets the list of substitutions to the hash passed in. Substitutions are performed such that Profanalyzer.filter(key) = value.



346
347
348
# File 'lib/profanalyzer.rb', line 346

def substitutions=(hash)
  @settings[:custom_subs] = hash
end

#toleranceObject

Returns Profanalyzer’s tolerance. Value will be an integer 0 <= T <= 5.



321
322
323
# File 'lib/profanalyzer.rb', line 321

def tolerance
  @settings[:tolerance]
end

#tolerance=(new_tol) ⇒ Object

Sets Profanalyzer’s tolerance. Value should be an integer such that 0 <= T <= 5.



315
316
317
# File 'lib/profanalyzer.rb', line 315

def tolerance=(new_tol)
  @settings[:tolerance] = new_tol
end

#update_settings_from_hash(hash) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/profanalyzer.rb', line 155

def update_settings_from_hash(hash)
  self.tolerance = hash[:tolerance] if hash.has_key? :tolerance
  self.check_racist = hash[:racist] if hash.has_key? :racist
  self.check_sexual = hash[:sexual] if hash.has_key? :sexual
  if hash.has_key? :all
    self.check_all = hash[:all]
  elsif hash.has_key?(:sexual) || hash.has_key?(:racist)
    self.check_all = false
  else
    self.check_all = true
  end
end