Spelling Suggestions with Google API and PHP

Disclaimer

Code and instructions for implementing Google Spelling Suggestion features for WebVoyage. Use at your own risk. TCC is not responsible for damage or problems caused by the use of this code. No warranties or guarantees of any kind are implied or assumed.
Version 0.1
Scripts by Jim Robinson
Tarrant County College South Campus
5301 Campus Dr.
Fort Worth, TX 76119
(817) 515-4310

Copyright © 2024, Tarrant County College ("TCC"). All rights reserved.

For use with the WebVoyage product by Endeavor Systems, Inc.
"WebVoyage" is a trademark of Endeavor Information Systems, Inc.

PHP Copyright© 2001-2005 The PHP Group All rights reserved.

NuSOAP - Web Services Toolkit for PHP Copyright (c) 2002 NuSphere Corporation
Available from http://sourceforge.net/projects/nusoap/

Purpose: Javascript and PHP scripts use the Google Spelling API to check a user's spelling when no hits are found.

No warranties or guarantees of any kind or implied. use at your own risk. It works for us, so we thought we'd share... ;-)

By using this software the USER indicates that he or she has read, understood and and will comply with the following:

TCC hereby grants USER permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee, provided that:
  1. the above copyright notice appears in all copies of the software and its documentation, or portions thereof, and
  2. a full copy of this notice is included with the software and its documentation, or portions thereof, and
  3. neither the software nor its documentation, nor portions thereof, is sold for profit. Any commercial sale or license of this software, copies of the software, its associated documentation and/or modifications of either is strictly prohibited without the prior consent of TCC.
  4. Title to copyright to this software and its associated documentation shall at all times remain with TCC. No right is granted to use in advertising, publicity or otherwise any trademark, service mark, or the name of TCC.
  5. This software and any associated documentation are provided "as is," and TCC MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESSED OR IMPLIED, INCLUDING THOSE OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, OR THAT USE OF THE SOFTWARE, MODIFICATIONS, OR ASSOCIATED DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER INTELLECTUAL PROPERTY RIGHTS OF A THIRD PARTY. TCC, Tarrant County College, its Regents, officers, and employees shall not be liable under any circumstances for any direct, indirect, special, incidental, or consequential damages with respect to any claim by USER or any third party on account of or arising from the use, or inability to use, this software or its associated documentation, even if TCC has been advised of the possibility of those damages.
This code is meant to be used with the WebVoyage product by Endeavor Systems, Inc.™ When a user search of the library catalog receives no hits, the user's search criteria is sent to Google. Google will return a spelling suggestion if the word is misspelled. Google limits the number of spelling checks to 1000 hits per day.

The javascripts, php, and html are enclosed in Google_spellcheck.zip.

You can see these scripts in action at http://library.tccd.edu. (PHP/aspell version) Enter a mispelled word, such as pikcle or "captian cook".

Requirements:
  • PHP must be installed on the WebVóyage server. If you do not have PHP installed and don't wish to install it, feel free to rewrite this for Perl (which is installed on your WebVóyage server) or ColdFusion or any other server-side processor you have available to you. (There's a Perl interface available at CPAN or you can try my Perl installation instructions.)
  • Due to JavaScript security issues, the server-side processing must be done on the same server unless you have a signed security certificate installed, which I haven't done and can't help with... ;-)
  • The spelling suggestion uses JavaScript. Browsers with JavaScript disabled will not be able to take advantage of this technology, but it will not prevent them from using WebVoyage normally.
  • A free Google account/license key. Note: Google no longer allows new sign-ups for the Google SOAP Search API.
    You may want to consider using aspell with PHP or Perl

Instructions



    Download
  1. Make sure your WebVoyage server has PHP installed and running.
  2. Visit Google APIs and download the developer's kit, create a Google account, and get your free license key.
  3. Download spell.zip, which contains
    • instructions.txt - A text version of these instructions.
    • spellcheck directory - Contains the PHP code and libraries.
    • spell.js - The JavaScript that calls the PHP code.
    • load.gif - a tiny image used for a JavaScript onLoad() call.


    Install
  4. Move the spellcheck directory to your WebVoyage html folder.
    By default, the html directory is located at /m1/voyager/xxxdb/webvoyage/html/
  5. If you do not already have one, create a directory called js in your WebVoyage html.
    By default, the html directory is located at /m1/voyager/xxxdb/webvoyage/html/
  6. Move spell.js to the js directory.
  7. In header.htm, located by default in /m1/voyager/xxxdb/etc/webvoyage/, add
    <script language="JavaScript" type="text/javascript" src="/js/spell.js"></script>
  8. Move load.gif to the images directory.
    By default, the image directory is located at /m1/voyager/xxxdb/webvoyage/html/images/

    Configure (You have backed all files up before making changes, right?)
  9. Open spellcheck/spell.php and replace $myKey = 'Google license key' with your Google license key.
  10. Locate nohitsmsg.htm.
    By default, this can be found at /m1/voyager/xxxdb/etc/webvoyage/local/nohitsmsg.htm
  11. At the very top of nohitsmsg.htm, add <img src="/images/load.gif" onload="spellCheck();">
  12. In nohitsmsg.htm, add <div name="spellMessage" id="spellMessage">.</div> where you would like the spelling suggestion to appear.
  13. Locate badformmsg.htm.
    By default, this can be found at /m1/voyager/xxxdb/etc/webvoyage/local/badformmsg.htm
  14. At the very top of badformmsg.htm, add <img src="/images/load.gif" onload="spellCheck();">
  15. In badformmsg.htm, add <div name="spellMessage" id="spellMessage">.</div> where you would like the spelling suggestion to appear.

    Miscellaneous
  16. Also included is a counter, a way to track how many spellchecks are performed each day. If you do not want to implement the counter, comment or delete this line in spell.php: <?php include_once("counter/counter.php"); ?>
    To view the counter,visit http://your.webvoyage.domain/spellcheck/counter/index.php

How It Works

When nohitsmsg.htm or badformmsg.htm are accessed (by the user not getting any search results), the tiny GIF image is loaded, calling the spellCheck javascript function found in /js/spell.js.

In spell.js, an XMLHttpRequest object is created and all user search criteria is gathered together. This information is sent to /spellcheck/spell.php.

In spell.php, a connection is made to Google and the relevant information is sent. Google returns spelling suggestions, then spell.php outputs this information to the JavaScript.

While Google is being consulted, the user sees the message "Searching for suggestions..." When results are returned, the JavaScript then replaces this message with the new information, where it is printed in nohitsmsg.htm, which happens to be in the middle of the user's web page. Voila!

Troubleshooting

Questions and comments can be sent to Jim Robinson at Tarrant County College.

Last modified: August 14 2007.