Alex Rivera | Logout

How to integrate "Did you mean" functionality in rails?

Asked 2012-10-12T10:15:05.657
11

How can you implement the "Did you mean: " like Google does in some search queries?

PS: I am using sphinx in my product. Can you suggest how can I implement this. Any guides or suggestions for some other search engines who has this functionality are most welcomed. I am using rails2.3.8, if that helps

One Solution can be:

Make a dictionary of known "keywords" or "phrases", and in search action if nothing is found then run a secondary query in that dictionary. Update that dictionary whenever a searchable entry is created say, a blog post or username.

  • query = "supreman"

  • dictionary = ["superman", "batman", "hanuman" ...] (in DB table)

  • search(query)

  • if no results, then

search in dictionary (where "keyword" LIKE query or "phrase" LIKE query) => "superman"

Check in sphinx or solr documentation. They might have a better implementation of this "Like" query which returns a % match.

  • display -> Did you mean "superman"?

But the point is how to make it efficient?

Edit
Report

1 Answer

5

Have a look at the Damerau-Levenshtein distance algorithm. It calculates the "distance" between two strings and determines how many steps it takes to transform one string into another. The less steps the closer the two strings are.

This article shows the algorithm implemented as a MySQL stored function.

The algorithm is so much better than LIKE or SOUNDEX.

I believe Google uses crowd sourced data rather than an algorithm. ie if a user types in abcd, clicks on the back button and then immediately searches for abd then it establishes a relationship between the two search terms as the user wasn't happy with the results. Once you have a very large community searching then the pattern appears.

answered 2012-10-18T05:14:12.263

Your Answer