Using the Google API/web service to check spelling

Have you ever wanted to try check the spelling of text a user has entered in a web form? Or at any other time. Google has a very good spelling recognition system based on the number of database records it has for a specific spelling. In this article I’ll explain how you can use the web service google provides to make a simple spell checker.

So as I mentioned in the intro, Google uses the number of results it has in its database to decide what should be a correct spelling, or should I say, a more likely spelling. It then uses this to give you the “Did you mean: Blah“.
It is very useful to have a spell checker when creating web-based applications, especially if you’re performing searches on them. There are numerous spell checkers around, but only the Google spelling API gives you a single suggestion which is most commonly used. Here is a good example of a spell checker using the aspell library for Linux. There’s also an option to see the code. It’s a very useful checker if you want to generate a list of suggestions for the correct spelling.
Now, about the Google spelling checker: It uses the Google APIs, which are still in the Beta phase and, as such, have no guarantees. There is a bug with the spell checker which will be discussed later.
To use this web service you will need to create a Google account and then get a licence key to use with the APIs. I think you can also use this account for othe Google services including Google Groups.
Then you need to download the NuSoap library for PHP. It’s a very useful library that makes it extremely easy to use web services.
Right, now for the code.
1     <?php
require_once "nusoap.php";
5          function
spellcheck ($string)
6          {
$soapclient = new soapclient ("");
$params = array('key' => 'your_google_key','phrase' => $string);
$spellresult = $soapclient->call("doSpellingSuggestion", $params, "urn:GoogleSearch", "urn:GoogleSearch");
13             return
14         }
16         if (isset(
17         {
21         }

25     <form name="google_spell_test" method="GET">
26     Test Google's spelling <input type="text" name="search" value="<?php echo $_REQUEST['search'];?>">
27     <br>
28     <input type="submit" name="submit_button" value="Check Spelling">
29     </form>
31     <?php if (!empty($spell_suggest) && !is_array($spell_suggest)):?>

33     Did you mean <i><?php echo $spell_suggest;?></i></a>
35     <?php endif;?>

Let’s go through this line-by-line.
Line 3 is just the line to include that useful NuSoap library.
Line 7 is the creation of the soap web service, specifically for the google search API. This tells the soap client where to find the web service.
Line 9 is setting up the parameters to pass to the remote function. You’ll need your google key and ‘phrase’ is the string of text to check.
Then on line 11 the function is called – gotta love web services – how easy is this? All you do is secify the function name, which can be found in info for the web service, send the parameters as defined on line 9, and then specify the namespace for the function.
Then all the rest is simple PHP to implement this spell checker. We’ve got a form with an input field that get’s posted and then the field gets checked and a spelling suggestion given if there is one.
I’ve left out error handling in this, as it’s a bit more detailed, but not really necessary for this.
You’ll notice I check whether there is a suggestion before I display it, and also that the suggestion isn’t an array. If it’s an array, it will contain an error.
This brings me to an interesting bug:
If you type in “profesor” i.e. professor with one “s” to check the spelling, it generates a remote error! This is very strange and seems to be a bug in the Google API. If you however type in “profesorr” it correctly suggests the correct spelling. If anyone knows a reason for this let me know.
So, all-in-all a very useful web service. I hope to write some more on web services soon.


One thought on “Using the Google API/web service to check spelling

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s