How to Check Words Spelling in Java using Jazzy API

Check Words Spelling in Java using Jazzy API

In this tutorial, I am writing Simple Java Program about, How to Check Words Spelling in Java. The example program was done using Jazzy API, Which is used to Check Words Spelling in Java. The Program has been tested with environment and output is shared in the same post.

Generally, The Jazzy API is used to perform Spelling Correction for Given String in Java. It can be downloaded in the below link.

Download Jazzy Core 0.5.2 Jar and Add downloaded jar file to your eclipse Project.

Once its done, Create a folder with a my_dictionary.txt text file (Note: You can even Change File Name based on your convenience). The text file should contains a list of Proper English words. You can download the sample word file in the following link.

http://introcs.cs.princeton.edu/java/data/words.utf-8.txt

Project Structure

Make sure that, You have added and organized all required files such as Jazzy API and my_dictionary.txt properly in your eclipse project. even I have attached the sample Screen Shot of Project Structure, Which you can find below.

Check Words Spelling in Java

MySpellCheckListener.java

package com.dineshkrish;

import java.util.List;

import com.swabunga.spell.event.SpellCheckEvent;
import com.swabunga.spell.event.SpellCheckListener;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class MySpellCheckListener implements SpellCheckListener {

	private List<String> misspelledWords;
	
	public MySpellCheckListener(List<String> misspelledWords) {

		this.misspelledWords = misspelledWords;
	}

	@Override
	public void spellingError(SpellCheckEvent event) {

		event.ignoreWord(true);
		misspelledWords.add(event.getInvalidWord());
	}

}

MyDictionaryMap.java

package com.dineshkrish;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import com.swabunga.spell.engine.SpellDictionaryHashMap;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class MyDictionaryMap {

	private static MyDictionaryMap dictionary;
	private File file;
	private SpellDictionaryHashMap dictionaryHashMap;

	private MyDictionaryMap(File file) {

		this.file = file;
	}

	public static MyDictionaryMap getInstance(String filePath) {

		if (filePath != null && !filePath.isEmpty()) {

			if (dictionary == null) {
				File file = new File(filePath);
				dictionary = new MyDictionaryMap(file);
			} else {
				return dictionary;
			}
		}
		return dictionary;
	}

	public SpellDictionaryHashMap getDictionaryMap() {

		if (file != null) {

			try {
				
				dictionaryHashMap = new SpellDictionaryHashMap(file);
				
			} catch (FileNotFoundException e) {

				System.out.println(e.getMessage());
				e.printStackTrace();
			} catch (IOException e) {
				
				System.out.println(e.getMessage());
				e.printStackTrace();
			}
		}
		return dictionaryHashMap;
	}
}

MySpellChecker.java

package com.dineshkrish;

import java.util.ArrayList;
import java.util.List;

import com.swabunga.spell.engine.Word;
import com.swabunga.spell.event.SpellChecker;
import com.swabunga.spell.event.StringWordTokenizer;
import com.swabunga.spell.event.TeXWordFinder;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class MySpellChecker {

	private SpellChecker spellChecker;
	private List<String> misspelledWords = new ArrayList<String>();;
	
	private MyDictionaryMap dictionaryMap;

	public MySpellChecker(String filePath) {

		if(filePath != null && !filePath.isEmpty()) {
			
			dictionaryMap = MyDictionaryMap.getInstance(filePath);
			spellChecker = new SpellChecker(dictionaryMap.getDictionaryMap());
			
			MySpellCheckListener listener = new MySpellCheckListener(misspelledWords);
			spellChecker.addSpellCheckListener(listener);
		}		
	}

	// Method to Detect Misspelled Words From Given Line
	public List<String> detectMisspelledWords(String text) {
		
		StringWordTokenizer strTokenizer = new StringWordTokenizer(text, new TeXWordFinder());
		spellChecker.checkSpelling(strTokenizer);
		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)
				continue;
			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) {	
			suggestions.add(suggestion.getWord());
		}
		return suggestions;
	}

}

Check Words Spelling in Java

The SpellCheckerApplication is a class, which contain the main method. Eventually Its our entry point of Java Spelling Checker application.

SpellCheckerApplication.java

package com.dineshkrish;

import java.util.Scanner;

public class SpellCheckerApplication {

	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
	
		// You can change your file path accordingly.
		MySpellChecker spellChecker = new MySpellChecker("dictionary/my_dictionary.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);
		
		scanner.close();
		
	}
	
}

Output

—————
Enter the Sentence with Spelling Mistake..
hello everyoen, I am java softwaree professinoal.
Before Correction : hello everyoen, I am java softwaree professinoal.
After Correction : hello everyone, I am java software professional.