سؤال

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

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

You can use JOrtho. I have used it earlier in one of the swing app.

A good offline solution is Jazzy. Try this example and download the dictionary.

Here's the Maven dependency for library:

<dependency>
    <groupId>net.sf.jazzy</groupId>
    <artifactId>jazzy</artifactId>
    <version>0.5.2-rtext-1.4.1-2</version>
</dependency>

Languagetool is Java bases spell checking and proofreading software that might fit. See

Try Hunspell. It is a standard for spell check. You can use Java port of Hunspell which is Hunspell-c+ JNA

If you want a simple and offline solution, based on Peter Norvig explanation of Google spell corrector, take a look here: http://raelcunha.com/spell-correct.php

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top