Spell check and/or spell correction in Java

Ronaldinho Learn Coding picture Ronaldinho Learn Coding · May 24, 2012 · Viewed 17.4k times · Source

How can I do spell checking and/or spell correction in a Java application?

Answer

Nishant picture Nishant · May 24, 2012

Google's Spell Checker http://code.google.com/p/google-api-spelling-java/

 SpellChecker checker = new SpellChecker();

 SpellResponse spellResponse = checker.check( "helloo worlrd" );

 for( SpellCorrection sc : spellResponse.getCorrections() )
    System.out.println( sc.getValue() );

It's much like when you use Gmail or Google services (like translate.google.com or search) that gives you alternate suggestion if you have a typo.

What happens in the background?

The SpellChecker class transforms the request into XML and sends it to the Google's spell checker service. The response is also in XML, which is then deserialized into simple POJOs.

The request to the first example above looks like:

  <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
  <spellrequest textalreadyclipped="0" ignoredigits="1" 
                          ignoreallcaps="1" ignoredups="0">
    <text>helloo worlrd</text>  
  </spellrequest>

And the response XML looks like:

  <?xml version="1.0" encoding="UTF-8"?>  
  <spellresult error="0" clipped="0" charschecked="13">
     <c o="0" l="6" s="1">hello  Helli   hell    hallo   hullo</c>
     <c o="7" l="6" s="1">world  whorled wold    warlord would</c>  
  </spellresult>

Haven't tried though.


UPDATE:
Google might have started charging for this. I do not have time to code to check this. Someone can confirm. As far as Google is concerned, it seems that they have deprecated the old API for new and paid one.

Refer: Google Translate API FAQ

What happened to earlier free versions of Translate API?
Google Translate API v1 is no longer available as of December 1, 2011 and has been replaced by Google Translate API v2. Google Translate API v1 was officially deprecated on May 26, 2011. The decision to deprecate the API and replace it with the paid service was made due to the substantial economic burden caused by extensive abuse.