Listing 1
package com.sys_con.ColdFusion;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.swabunga.spell.engine.SpellDictionary;
import com.swabunga.spell.engine.SpellDictionaryHashMap;
import com.swabunga.spell.event.SpellCheckEvent;
import com.swabunga.spell.event.SpellCheckListener;
import com.swabunga.spell.event.SpellChecker;
import com.swabunga.spell.event.StringWordTokenizer;
/**
* @author Darron Schall (darron@darronschall.com)
*
*/
public class CFSpellCheck {
private SpellDictionary dictionary;
private SpellChecker spellChecker;
private ArrayList errors;
private String textToCheck;
public CFSpellCheck() {
errors = new ArrayList();
}
public ArrayList getErrors() {
return errors;
}
public String getText() {
return textToCheck;
}
public void checkSpelling() {
spellChecker = new SpellChecker(dictionary);
spellChecker.addSpellCheckListener(new SpellCheckListener() {
public void spellingError(SpellCheckEvent event) {
SpellingError s = new
SpellingError(event.getInvalidWord(),
event.getWordContextPosition());
System.out.println("SpellingError - " +
event.getInvalidWord() + " at position " +
event.getWordContextPosition());
List suggestions = event.getSuggestions();
Iterator suggestedWord = suggestions.iterator();
while (suggestedWord.hasNext()) {
String suggestion =
suggestedWord.next().toString();
s.addSuggestion(suggestion);
System.out.println(suggestion);
}
errors.add(s);
}
});
spellChecker.checkSpelling(new StringWordTokenizer(textToCheck));
}
public void setDictionary(String dictFile) {
try {
dictionary = new SpellDictionaryHashMap(new
File(dictFile));
} catch (Exception e) {
e.printStackTrace();
}
}
public void setText(String txt) {
textToCheck = txt;
}
public static void main(String args[]) {
CFSpellCheck spellcheck = new CFSpellCheck();
spellcheck.setDictionary("dict/english.0");
spellcheck.setText("This is some text that needs
speel checkin");
System.out.println(spellcheck.getText());
spellcheck.checkSpelling();
System.out.println("... done!");
System.out.println(spellcheck.getErrors());
}
private class SpellingError {
private String word;
private int position;
private ArrayList suggestions;
public SpellingError(String word, int position) {
init(word, position);
}
public void init(String word, int position) {
this.word = word;
this.position = position;
suggestions = new ArrayList();
}
public void addSuggestion(String suggestedWord) {
suggestions.add(suggestedWord);
}
public String getWord() {
return word;
}
public int getPosition() {
return position;
}
public ArrayList getSuggestions() {
return suggestions;
}
public String toString() {
return "word:" + word + " | position:" + position +
" | suggestions: " + suggestions;
}
}
}
Listing 2
<html>
<head>
<title>CFSpellCheck example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<br />
<cfscript>
if (StructKeyExists(form, "checkText")) {
CFSpellCheck = CreateObject("java",
"com.sys_con.ColdFusion.CFSpellCheck");
CFSpellCheck.init();
CFSpellCheck.setDictionary("C:/JavaClasses/dict/english.0");
CFSpellCheck.setText(form.checkText);
CFSpellCheck.checkSpelling();
errors = CFSpellCheck.getErrors();
if (ArrayLen(errors)) {
WriteOutput("<b>Errors:</b> <br />");
for (i = 1; i lte ArrayLen(errors); i = i + 1) {
WriteOutput("word: " & errors[i].getWord() & "<br />");
WriteOutput("position: " & errors[i].getPosition() & "<br />");
WriteOutput("suggestions: ");
suggestions = errors[i].getSuggestions();
for (j = 1; j lte ArrayLen(suggestions); j = j + 1) {
WriteOutput(suggestions[j] & "<br />");
}
WriteOutput("<hr />");
}
} else {
WriteOutput("No errors found! <hr />");
}
} else {
form.checkText = "";
}
</cfscript>
<form action="index.cfm" method="post">
<textarea name="checkText" rows="4" cols="40">
<cfoutput>#form.checkText#</cfoutput></textarea>
<input type="submit" value="Check Spelling" />
</form>
</body>
</html>