Skip to main content

Mobomo webinars-now on demand! | learn more.

I'm pleased to announce Profanity Filter for Rails. This gem filters undesirable words from text and displays a friendly version. Filtering is non-destructive by default but has the option of replacing the original text. It's based on a dictionary that is customizable so you can add additional words as needed.

Profanity Filter is the evolution of a plugin I created a few years ago called Fu-Fu (the Profanity Filter for Rails). This new version has been renamed, tested with Rails 3 and has been moved over to the Intridea github account to ensure proper support (bug fixing, development time, etc) going forward.

Installation

gem install profanity_filter

Rails 3 with Bundler

Open your Gemfile and add:

gem "profanity_filter"

Rails 2.x without Bundler

Open config/environment.rb and add:

config.gem "profanity_filter"

Usage: Basic

You can call the profanity_filter method within your ActiveRecord models.

class Post < ActiveRecord::Base    profanity_filter :title, :body  end

Usage: Non-Destructive

Filters content when called, original text remains in the database.

profanity_filter :foo, :bar    #=> banned words will be replaced with @#=>$%    profanity_filter :foo, :bar, :method => 'dictionary'    #=> banned words will be replaced by value in config/dictionary.yml    profanity_filter :foo, :bar, :method => 'vowels'    #=> banned words will have their vowels replaced    profanity_filter :foo, :bar, :method => 'hollow'    #=> all letters except the first and last will be replaced    Non-destructive versions of the filtered attribute:    some_model.foo => 'filtered version'    some_model.foo_original => 'non-filtered version'  

Usage: Destructive

Filters content in the database, original text is lost. Note the "!" when calling the profanity_filter method.

profanity_filter! :foo, :bar    #=> banned words will be replaced with @#=>$%    profanity_filter! :foo, :bar, :method => 'dictionary'    #=> banned words will be replaced by value in config/dictionary.yml    profanity_filter! :foo, :bar, :method => 'vowels'    #=> banned words will have their vowels replaced    profanity_filter! :foo, :bar, :method => 'hollow'    #=> all letters except the first and last will be replaced  

You can find additional information in the README.

Contributing

I've used this on a number of projects and it's served me well but it's by no means perfect. If you find something that doesn't work for you or have a suggestion please let me know and I'll see what I can do (directly or through Github Issues). If you have an improvement/refactoring I welcome pull requests on Github. The project has a full test suite with benchmarking and the code base is tiny so feel free to jump in!

You can find Profanity Filter on Intridea's Github account.

Categories
Author

There will be no killer words in this application: Behold the mighty Fu-fu! And there was much rejoicing… But first, a little history on Fu-fu: The Profanity Filter for Rails.

A Little History

Recently, I needed a simple (profanity/cuss/swear/bad word) filter for a Rails app, so I hit up Google for the answer as this seemed like a problem that should have been solved by an expert. Sadly, this was not the case.

Over the next day or so I was able to get a simple prototype up and running in our application and that’s the way it stayed for the next couple weeks. Then I realized that this was reason I wasn’t able to find a profanity filter plugin on Google.

Upon closer inspection it seems that people are building their own filters and leaving it at that. Almost being guilty of this, I decided that it was time to give back to the community and get a profanity filter plugin out in the wild.

I was able to publish the first version of the Profanity Filter during the Community Code Drive at RailsConf 2008. Hacking in the same room as DHH, David Chelimsky, Chad Fowler, Rich Kilmer, Marcel Molina Jr., and the entire Intridea team is a great motivator.

During RailsConf we decided that the plugin needed a real name; “Profanity Filter” wasn’t cutting it. Someone suggested fu-fu pronounced ‘eff-you-foo’. That was promptly shortened to ‘foo-foo’. How can you not love something named Fu-fu that deals with profanity and abuses plugin idioms at the same time?

Continue Rejoicing (Examples!)

The interface for Fu-fu is clean and straight forward. For example. lets say that ‘frak’ is a common curse word.

class Post < ActiveRecord::Base   profanity_filter :title, :body end  post = Post.create(:title => 'Fraking title', :body => 'What a bunch of frak')  post.title          #=> '@#$% title' post.title_original #=> 'Fraking title'  post.body           #=> 'What a bunch of @#$%' post.body_original  #=> 'What a bunch of frak'

 

By default the filter will replace common curses with the standard curse notation of ‘@#$%’. Fu-fu is also has the ability to do dictionary replacements:

class Post < ActiveRecord::Base   profanity_filter :title, :body, :method => 'dictionary' end  post = Post.create(:title => 'Fraking title', :body => 'What a bunch of frak')  post.title          #=> 'fr*k*ng title' post.body           #=> 'What a bunch of fr*k'

 

Fu-fu comes with a default dictionary file that replaces all vowels with asterisks (*).

You can also add an exclamation point to the end of the filter call (profanity_filter!) and have the method save the filtered text to the database (although this is not recommended for most applications).

You can also call Fu-fu directly:

ProfanityFilter::Base.clean('frak')               #=> '@#$%' ProfanityFilter::Base.clean('frak', 'dictionary') #=> 'fr*k

 

Todos and Fixes

But alas, there is still danger in Caerbannog. As with all things, there is room for improvement.

* better filter regex, doesn't currently catch things like 'f-r-a-k' * needs support for multiple dictionaries and configuration * needs support for different levels of filtering (prude, normal, weak, etc)

 

Installation

To install the Fu-fu: The Profanity Filter on Edge Rails or Rails 2.1 and greater:

script/plugin install git://github.com/adambair/fu-fu.git

 

On earlier versions of Rails:

git clone git://github.com/adambair/fu-fu.git vendor/plugins/fu-fu

 

Resources

Bug tracking is available through the Fu-fu Lighthouse project. Also, feel free to contribute. I’ll be happy to accept patches and push requests for reasonable fixes and additions as long as they come with test coverage.

The source code is available on GitHub.

For general discussion about the plugin, please use the forums and wall of Fu-Fu’s Acts As Community Profile

Thanks to the Intridea team for their time, contributions, and suggestions. I’ve had a great time building Fu-fu and I hope someone may find it useful.

Categories
Author
1
Subscribe to Profanity