• home
  • forum
  • my
  • kt
  • download
  • Spell Checking with Google Web Services API

    Author: 2007-08-03 14:37:07 From:

     

    In our tutorial Using Google Web Services API, we discussed how to use the Java classes to access the Google Web API. In this tutorial, we will create a class that allows you to use the Google Web API for spell checking.

    To start using the Google Web API, you will need to download the Google Web APIs developer's kit and register for a Google license key on Google's developer page (see Related Links). Once you have downloaded and unzipped the Google developer's kit, include the googleapi.jar file in your Java class path.

    The first step of our GoogleSpecllCheck class is to create the constructor. In the constructor, we create an instance of GoogleSearch. Next, we set the key provided by Google. You will need to create an instance of the GoogleSearch class and set the key that was provided by Google.

    /**
     * The constructor for the GoogleSpellCheck class
     */
    public GoogleSpellCheck()
    {
        googleSearch = new GoogleSearch();

        //Set the key provided by Google
        googleSearch.setKey("your key");
    }

    Next, we create a method that checks the spelling of a word or phrase.  In this method, we make a call to the GoogleSearch to request a spelling suggestion. If the word is spelled correctly, the doSpellingSuggestion method will return null. This method will also catch any exceptions thrown by the doSpellingSuggestion method and return null.

    /**
     * Check the spelling of a word
     *
     * @param word - The word to check
     * @return 
    The suggested spelling of the word.
     *         If there are no suggestions, a null will be returned.
     */
    public String checkSpelling(String word)
    {
        String result = null;
        try
        {
            result = googleSearch.doSpellingSuggestion(word);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return result;
    }

    In the main method of the class, we provide an example of using the new GoogleSpellCheck class. For the example, we misspell the word Gogle. If it does not find a suggested spelling, the checkSpelling method will return null. In our example, it will return the value of Google.

    GoogleSpellCheck googleSpellCheck = new GoogleSpellCheck();

    //Check the spelling
    String result = googleSpellCheck.checkSpelling("Gogle");

    if (result == null)
    {
            //If the result is null, there are no suggested spellings for the word
            System.out.println("No suggestions");
    }
    else
    {
            //Print the suggested spelling
            System.out.println("Did you mean : " + result );
    }

    The complete GoogleSpellCheck class is below.

    package tutorial;

    import com.google.soap.search.*;

    Public class GoogleSpellCheck
    {
        private GoogleSearch googleSearch;

        /**
         * The constructor for the GoogleSpellCheck class
         */
        public GoogleSpellCheck()
        {
            googleSearch = new GoogleSearch();

            //Set the key provided by Google
            googleSearch.setKey("your key");
        }

        /**
         * Check the spelling of a word
         *
         * @param word - The word to check
         * @return The suggested spelling of the word.
                If there are no suggestions, a null will be returned.
         */
        Public String checkSpelling(String word)
        {
            String result = null;
            try
            {
                result = googleSearch.doSpellingSuggestion(word);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }

            return result;
        }

        public static void main(String[] args)
        {
            GoogleSpellCheck googleSpellCheck = new GoogleSpellCheck();

            //Check the spelling
            String result = googleSpellCheck.checkSpelling("Gogle");

            if (result == null)
            {
                //If the result is null, there are no suggested spellings for the word
                System.out.println("No suggestions");
            }
            else
            {
                //Print the suggested spelling
                System.out.println("Did you mean : " + result );
            }
         }
    }

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Applet Building (2)
      Application Building (3)
      Communication (1)
      Database Related (8)
      Development (12)
      EJB (14)
      Game Programming (2)
      General Java (38)
      Javabeans (4)
      JSP and Servlets (8)
      Miscellaneous (23)
      Networking (1)
      Security (2)
      Swing (13)
      WAP and WML (1)
      XML and Java (0)

    New

    Hot