Custom Search

   
       
  Integrate Your Java App With Google Spell Checker  
  March 29, 2008 filed under Tutorials, Spell Checker  
     
  Introduction  
 

If you have installed the google tool bar on your browser you will notice a button that does spell checks. This tutorial will provide you with a Java class that mimics the calls of the toolbar button. This is not an official API.

DISCLAIMER: You are following this tutorial at your own risk. If anything blows up do some meditation before assigning blame.

 
     
     
  The Code
 

1) You can download the full source code from here: Source code file and unzip it to C:\googlespellchecker

 
     
 

2) The zip file consists of one src file, compile batch file, run batch file. Assuming you have Java installed the only thing that you need to do is change the path for javac in the compile.bat if its different.

 
     
 

3) From a command prompt go to C:\googlespellchecker and execute the compile.bat. Make sure you get a .class file. You can change the string that you want to be spell checked in the main method. Next execute the run.bat file.

 
     
 
Toggle Line Numbers   View Plain   ?
01 import java.io.InputStream;
02 import java.io.BufferedReader;
03 import java.io.InputStreamReader;
04 import java.io.OutputStreamWriter;
05 import java.net.URL;
06 import java.net.URLConnection;
07 
08 /*
09 *
10 * This class demonstrates how to interact with Googles
11 * Spell Checker. Google uses this spell checker
12 * through their browser toolbar. Therefore this class
13 * just mimics the toolbar. This is not an official API.
14 * We are not liable for anything if Google comes after
15 * you.
16 *
17 * More information at http://www.gmacker.com
18 *
19 */
20 
21 public class GSpellCheck {
22 
23 
24 /*
25 * This method makes a call to the Google Spell Checker
26 * and returns possible corrections.
27 *
28 * @param pText - any text that needs to be spell checked
29 *
30 */
31 public static void doSpellCheck(String pText) {
32 try {
33  //Format the XML that needs to be send to Google Spell
34  //Checker.
35  StringBuffer requestXML = new StringBuffer();
36  requestXML.append("<spellrequest textalreadyclipped=\"0\""+
37  " ignoredups=\"1\""+
38  " ignoredigits=\"1\" ignoreallcaps=\"0\"><text>");
39  requestXML.append(pText);
40  requestXML.append("</text></spellrequest>");
41  //The Google Spell Checker URL
42  URL url = new URL("https://www.google.com/tbproxy/spell?lang=en&hl=en");
43  URLConnection conn = url.openConnection();
44  conn.setDoOutput(true);
45 
46  OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
47  out.write(requestXML.toString());
48  out.close();
49 
50 //Get the result from Google Spell Checker
51  InputStream in = conn.getInputStream();
52  BufferedReader br = new BufferedReader(new InputStreamReader(in));
53  String inputLine;
54 
55  while ((inputLine = br.readLine()) != null) {
56   //Print out the response from Google
57   System.out.println(inputLine);
58  }
59  in.close();
60  }
61 catch (Exception e) {
62  System.out.println("Exception "+ e);
63  }
64 }
65 
66 /*
67 * Main method that tries to get hello world back from
68 * Google Spell checker
69 *
70 */
71 public static void main(String[] args)
72  {
73   GSpellCheck.doSpellCheck("heelo wirld");
74  }
75 
 
     
 

The above will give an output as follows:

 
 
Toggle Line Numbers   View Plain   ?
1 <?xml version="1.0"?>
2 <spellresult 
3 error="0" clipped="0" charschecked="11">
4 <c o="0" l="5" s="1">hello heel Helli halo heal</c>
5 <c o="6" l="5" s="1">world wield wild wiled whirled</c>
6 </spellresult>
 
     
 

The 'o' indicates the offset from the start of your string to the misspelled word. 'l' is the length of the misspelled word. 's' seems to be a confidence indicator

 
     
 


Inspired By: Hacking Google Spell Checker for Fun and Profit

 
   

 

   
   
 
   
  Table Of Contents
  >>Introduction
  >>The Code
   
  Links
  >>Contact Us