Acts As Suggest plugin

When searching for the word “honnolullu”, Google will promptly suggest “Did you mean: honolulu”. This feature is quite useful because it drastically improves usability. The lack of this exact function makes Wikipedia a pain in the neck as far as searching goes. So much so that I, and I suspect most people, just google for “wikipedia searched_term”.

Hence, I decided to create a small plugin. The acts_as_suggest Rails plugin allows developers to easily add this functionality to any ActiveRecord class, basing the suggestions on the existing values in the table. For example, if you have a table Articles with a column Title, you will be able to retrieve a list of possible corrections for a misspelled article title. The suggestions are not coming from a dictionary, making it very flexible.

Installation

Make sure you have the required Text rubygem, if not please install it: gem install Text

Download acts_as_suggest.zip, extract it and copy the acts_as_suggest folder into the vendor/plugins directory of your application. You’ll also need to restart your web server.

Usage

Assuming you have a model for a table that contains a list of articles, you would start by including acts_as_suggest in your model:

class Article < ActiveRecord::Base
  acts_as_suggest
end

This will provide the model with the suggest method, which is the core of the plugin. At this point you can retrieve suggestions as follows:

Article.suggest(:title, 'honnolullu')
Article.suggest([:title,:author], 'David Copperffelds')

These will return an array of records if the searched string is not misspelled and matches an existing value in the database. If no matches are found, the method will attempt to find similar existing values and return an array of strings (your suggestions). If none of the strings in the given column(s) are close enough, an empty suggestion array is returned.

The return values reflect a fairly common scenario. With a single method call I can retrieve all the records that match a user search, or I can retrieve suggestions to correct the spelling of his/her search if no records are found.

Please check the documentation for further details.

Documentation

You can read the short documentation online here or access the local copy in the /doc folder.

Bugs and Feedback

Here comes a little disclaimer. I wrote this plugin in half an hour, including the basic documentation and a few tests. Therefore, I’ve not yet setup an SVN repository for it; there are just a few test cases (I’m not even publishing them at this time) and it is very likely that there will be bugs, issues or possible improvements required. I’m releasing this .zip file as early as possible, while the code is in its infancy. In this way I can survey user interest and eventually further develop it. So please, send in any feedback and bug reports by email.

UPDATE (02/14/07): I’ve fixed a vulnerability regarding the unsafe handling of tainted data in a finder method (spotted by Alex Wayne). Please download version 0.1.1.

Given the enthusiasm and interest shown by many people, I’m now considering the possibility of starting an actual small project on Rubyforge.

Get more stuff like this

Subscribe to my mailing list to receive similar updates about programming.

Thank you for subscribing. Please check your email to confirm your subscription.

Something went wrong.

12 Comments

  1. ittays February 9, 2007
  2. Antonio Cangiano February 9, 2007
  3. Hsiu-Fan Wang February 11, 2007
  4. Zach Dennis February 13, 2007
  5. Benjamin Curtis February 14, 2007
  6. Antonio Cangiano February 14, 2007
  7. Alex Wayne February 14, 2007
  8. Antonio Cangiano February 14, 2007
  9. Arash February 18, 2007
  10. Antonio Cangiano February 18, 2007
  11. Bill Siggelkow March 2, 2007
  12. Antonio Cangiano March 3, 2007