Java spell checking with Jazzy (test #2)

Java spell checking FAQ: Can you share a Java spell checking example?

Earlier I shared the source code for a simple Jazzy Java spell checking example. Next up, here's the Java source code from my second working Java spell checking program using the Jazzy spell checking tool. In this Java program I'm starting to get where I want, which is the ability to pass a String into Jazzy, and determine whether there are any misspellings in that String. Hopefully this is a little bit cleaner looking than my first Java spell checking example.

My Java spell checking program using Jazzy

import com.swabunga.spell.event.*;
import com.swabunga.spell.engine.*;

import java.io.*;
import java.util.*;

/**
 * In this test I think I'm starting to get a little smarter as far as how Jazzy works.
 * Here are some of the things I've done:
 * (1) I've switched from the FileWordTokenizer to the StringWordTokenizer.
 * (2) I learned that "spellChecker.addSpellCheckListener(this)" can only be run once.
 * (3) I've also started to abstract/extract some of the functionality out of the constructor and into other methods,
 * such as the new "populateListOfMisspelledWords" and "printWordsInMisspelledList" methods.
 */
public class JazzyTest2 implements SpellCheckListener
{
  private final String DICTIONARY_FILE = "resources/english.0";
  private final String string1 = "This is a sample test string with no misspellings.";   // first test string
  private final String string2 = "Viagra will make your male m'ember larger";            // second test string

  private SpellChecker spellChecker;
  private ArrayList listOfMisspelledWords;

  public JazzyTest2()
  {
    createDictionary();
    spellChecker.addSpellCheckListener(this);

    // run the test on string1
    StringWordTokenizer texTok = 
       new StringWordTokenizer(string1, new TeXWordFinder());
    populateListOfMisspelledWords(texTok);
    printWordsInMisspelledList();

    // run a second test, this one on string2
    texTok = new StringWordTokenizer(string2, new TeXWordFinder());
    populateListOfMisspelledWords(texTok);
    printWordsInMisspelledList();
  }

  private void createDictionary()
  {
    File dict = new File(DICTIONARY_FILE);
    try
    {
      spellChecker = new SpellChecker(new SpellDictionaryHashMap(dict));
    }
    catch (FileNotFoundException e)
    {
      System.err.println("Dictionary File '" + dict + "' not found! Quitting. " + e);
      System.exit(1);
    }
    catch (IOException ex)
    {
      System.err.println("IOException occurred while trying to read the dictionary file: " + ex);
      System.exit(2);
    }
  }

  private void populateListOfMisspelledWords(StringWordTokenizer texTok)
  {
    listOfMisspelledWords = new ArrayList();
    spellChecker.checkSpelling(texTok);
  }

  private void printWordsInMisspelledList()
  {
    Iterator it = listOfMisspelledWords.iterator();
    while (it.hasNext())
    {
      System.out.println("listOfMisspelledWords: " + it.next());
    }
  }

  public void spellingError(SpellCheckEvent event)
  {
    event.ignoreWord(true);
    listOfMisspelledWords.add(event.getInvalidWord());
  }

  public static void main(String[] args)
  {
    new JazzyTest2();
  }
}

Java spell checking with Jazzy - Summary

I hope this Java spell checking example with Jazzy has been helpful. As usual, if you have any questions or comments, just leave a note below.