How to implement spell checker in java?

To make the words correct in java is much easy. For implementation on eclipse, it requires an api file and .txt file.

Here i have make a project over it, you can read the whole post and download the project.
First of download the .api and .txt file.
Create a project and add the api file to the lib folder and build path and add the .txt file to the dictionary folder.
import com.swabunga.spell.engine.SpellDictionaryHashMap;
public class First {
private static First dictionary;
private File file;
private SpellDictionaryHashMap dictionaryHashMap;
private First(File file) {
this.file = file;
public static First getInstance(String filePath) {
if (filePath != null && !filePath.isEmpty()) {
if (dictionary == null) {
File file = new File(filePath);
dictionary = new First(file);
} else {
return dictionary;
return dictionary;
public SpellDictionaryHashMap getDictionaryMap() {
if (file != null) {
try {
dictionaryHashMap = new SpellDictionaryHashMap(file);
} catch (FileNotFoundException e) {
} catch (IOException e) {
return dictionaryHashMap;
import java.util.List;
import java.util.ArrayList;
import com.swabunga.spell.engine.Word;
import com.swabunga.spell.event.SpellChecker;
import com.swabunga.spell.event.StringWordTokenizer;
import com.swabunga.spell.event.TeXWordFinder;
public class Second {
private SpellChecker spellChecker;
private List<String> misspelledWords = new ArrayList<String>();;
private First dictionaryMap;
public Second(String filePath) {
if(filePath != null && !filePath.isEmpty()) {
dictionaryMap = First.getInstance(filePath);
spellChecker = new SpellChecker(dictionaryMap.getDictionaryMap());
Third listener = new Third(misspelledWords);
// Method to Detect Misspelled Words From Given Line
public List<String> detectMisspelledWords(String text) {
StringWordTokenizer strTokenizer = new StringWordTokenizer(text, new TeXWordFinder());
return misspelledWords;
// Method to do Correction on Misspelled Words for the given Line
public String doCorrection(String line) {
List<String> misSpelledWords = detectMisspelledWords(line);
for (String misSpelledWord : misSpelledWords) {
List<String> suggestions = getSuggestions(misSpelledWord);
if (suggestions.size() == 0)
String bestSuggestion = suggestions.get(0);
line = line.replace(misSpelledWord, bestSuggestion);
return line;
// Getting Suggestions for Misspelled Words
public List<String> getSuggestions(String misspelledWord) {
List<Word> words = spellChecker.getSuggestions(misspelledWord, 0);
List<String> suggestions = new ArrayList<String>();
for (Word suggestion : words) {
return suggestions;
import java.util.List;
import com.swabunga.spell.event.SpellCheckEvent;
import com.swabunga.spell.event.SpellCheckListener;
public class Third implements SpellCheckListener {
private List<String> misspelledWords;
public Third(List<String> misspelledWords) {
this.misspelledWords = misspelledWords;
public void spellingError(SpellCheckEvent event) {
import java.util.Scanner;
public class Fourth {
public static void main(String[] args) {
Scanner scanner = new Scanner(;
// You can change your file path accordingly.
Second spellChecker = new Second(“dictionary/words.txt”);
System.out.println(“Enter the Sentence with Spelling Mistake..”);
// Reading Input from User
String line = scanner.nextLine();
System.out.println(“Before Correction : “+line);
// Method Invocation for Spelling Correction
line = spellChecker.doCorrection(line);
System.out.println(“After Correction : “+line);

See also:

2 thoughts on “How to implement spell checker in java?”

Leave a Comment