Monday, 20 August 2012

Trick for using Google's Did You Mean feature in Java

You must have used the Google's Did you mean feature. It corrects any mistakes in your query automatically. It acts as a spell checker. Best part is it also handles proper nouns like Delhi, Sachin Tendulkar, or any place names or any movie title or series name which is not possible using any regular spell checker. So here is a trick to use Google's Did you mean feature in your own apps.
I wrote a small java code to demonstrate it. The function didYouMean takes as input the query and makes a request to Google search and parses the HTML response to get the suggestion (if any). Its written in java and uses the Jsoup library for parsing the HTML content. But understanding the logic it can be converted to any language.
 If you take a look at the HTML response from Google you will find that the div tag with id "topstuff" contains the Did you mean suggestion by google if any. We simply extract that element and the parse it to plain text.Then we remove any extra characters in result and extract only the actual suggestion.
You can find the code for it on github.

Let me know if you like it.

0 comments:

Post a Comment