Class: Profanalyzer
- Inherits:
- 
      Object
      
        - Object
- Profanalyzer
 
- Defined in:
- lib/profanalyzer.rb
Overview
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Constant Summary collapse
- VERSION =
- "0.1.1"
- @@full_list =
- YAML::load_file(File.dirname(__FILE__)+"/../config/list.yml") 
- @@racist_list =
- @@full_list.select {|w| w[:racist]} 
- @@sexual_list =
- @@full_list.select {|w| w[:sexual]} 
- @@settings =
- {:racism => :forbidden, :sexual => :forbidden, :profane => :forbidden, :tolerance => 4, :custom_subs => {}} 
Class Method Summary collapse
- 
  
    
      .check_all=(check)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Sets Profanalyzer to scan (or not scan) for all profane words, based on the set tolerance. 
- 
  
    
      .check_racist=(check)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Sets Profanalyzer to scan (or not scan) for racist words, based on the set tolerance. 
- 
  
    
      .check_sexual=(check)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Sets Profanalyzer to scan (or not scan) for sexual words, based on the set tolerance. 
- 
  
    
      .filter(str)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Filters the provided string using the currently set rules, with #!@$%-like characters substituted in. 
- 
  
    
      .forbidden_words_from_settings  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    :nodoc:. 
- 
  
    
      .profane?(str)  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    Decides whether the given string is profane, given Profanalyzer’s current settings. 
- 
  
    
      .substitute(*args)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Sets a custom substitution for the filter. 
- 
  
    
      .subtitutions=(hash)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Sets the list of substitutions to the hash passed in. 
- 
  
    
      .tolerance=(new_tol)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Sets Profanalyzer’s tolerance. 
Class 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.
| 192 193 194 | # File 'lib/profanalyzer.rb', line 192 def self.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.
| 180 181 182 | # File 'lib/profanalyzer.rb', line 180 def self.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.
| 186 187 188 | # File 'lib/profanalyzer.rb', line 186 def self.check_sexual=(check) @@settings[:sexual] = (check) ? :forbidden : :ignore end | 
.filter(str) ⇒ 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") #==> "#!$%"
| 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | # File 'lib/profanalyzer.rb', line 156 def self.filter(str) retstr = str @@settings[:custom_subs].each do |k,v| retstr.gsub!(/#{k.to_s}/,v.to_s) end banned_words = Profanalyzer.forbidden_words_from_settings banned_words.each do |word| retstr.gsub!(/#{word}/, "#!$%@&!$%@%@&!$#!$%@&!$%@%@&!#!$%@&!$%@%@&!"[0..(word.length-1)]) end retstr end | 
.forbidden_words_from_settings ⇒ Object
:nodoc:
| 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | # File 'lib/profanalyzer.rb', line 109 def self.forbidden_words_from_settings # :nodoc: banned_words = [] @@full_list.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_list.each do |word| banned_words << word[:word] if @@settings[:tolerance] <= word[:badness] end if @@settings[:racism] == :forbidden @@sexual_list.each do |word| banned_words << word[:word] if @@settings[:tolerance] <= word[:badness] end if @@settings[:sexual] == :forbidden banned_words end | 
.profane?(str) ⇒ 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
| 137 138 139 140 141 142 143 | # File 'lib/profanalyzer.rb', line 137 def self.profane?(str) banned_words = Profanalyzer.forbidden_words_from_settings banned_words.each do |word| return true if str =~ /#{word}/ end false end | 
.substitute(*args) ⇒ Object
Sets a custom substitution for the filter. Can be passed as substitute(“foo”,“bar”) or “foo” => “bar”
| 204 205 206 207 208 209 210 211 | # File 'lib/profanalyzer.rb', line 204 def self.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 | 
.subtitutions=(hash) ⇒ Object
Sets the list of substitutions to the hash passed in. Substitutions are performed such that Profanalyzer.filter(key) = value.
| 198 199 200 | # File 'lib/profanalyzer.rb', line 198 def self.subtitutions=(hash) @@settings[:custom_subs] = hash end | 
.tolerance=(new_tol) ⇒ Object
Sets Profanalyzer’s tolerance. Value should be an integer such that 0 <= T <= 5.
| 173 174 175 | # File 'lib/profanalyzer.rb', line 173 def self.tolerance=(new_tol) @@settings[:tolerance] = new_tol end |