Free Tutorial 4 All


RESOURCES

Spell Checking using Java

Posted by admin | Photoshop | Friday 21 August 2009 12:41 pm

If you are in need of a simple spell checker, you should check out Jazzy.

http://sourceforge.net/projects/jazzy

Here is an example of a class that will spell check a String and return a list of mispelled words. The Constants.SPELLPATH is the path where text files are located that contains the dictionaries.

Spell Checker using Jazzy

import com.swabunga.spell.engine.GenericSpellDictionary;
import java.io.File;

import java.util.ArrayList;
import java.util.StringTokenizer;
import com.delegata.common.util.string.Constants;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Set;
import java.util.HashSet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* Written by Greg Dias
*
* Purpose: This class is used for simple spell checker
*/
public class SpellChecker
{
private static final Log log = LogFactory.getLog(SpellChecker.class);
public SpellChecker()
{
}

/**
* Purpose: Pass in an ArrayList that will be populated with
* suggestions. Will return a concatinated string
* of misspelled words.
* @param paragraph
* @param suggestions
* @return String
*/
public String findInDictionary(String paragraph, ArrayList suggestions)
{
String strippedParagraph = strip(paragraph);
String wrongWord = "";

try
{
File file = new File(Constants.SPELLPATH +"//2of4brif.txt");
File file1 = new File(Constants.SPELLPATH +"//2of12.txt");
File file2 = new File(Constants.SPELLPATH +"//3esl.txt");
File file3 = new File(Constants.SPELLPATH +"//5desk.txt");
File file4 = new File(Constants.SPELLPATH +"//6of12.txt");
StringTokenizer token = new StringTokenizer(strippedParagraph);
GenericSpellDictionary dictionary1 = new GenericSpellDictionary(file);
GenericSpellDictionary dictionary2 = new GenericSpellDictionary(file1);
GenericSpellDictionary dictionary3 = new GenericSpellDictionary(file2);
GenericSpellDictionary dictionary4 = new GenericSpellDictionary(file3);
GenericSpellDictionary dictionary5 = new GenericSpellDictionary(file4);
String temp = "";
do
{
if(!token.hasMoreTokens())
{break;}
temp = token.nextToken();
if(!dictionary1.isCorrect(temp) && !dictionary2.isCorrect(temp) && !dictionary3.isCorrect(temp) && !dictionary4.isCorrect(temp) && !dictionary5.isCorrect(temp))
{
wrongWord = (new StringBuilder()).append(wrongWord).append("[").append(temp).append("]").toString();
suggestions.add(dictionary1.getSuggestions(temp, 3));
}
} while(true);
}
catch(Exception e)
{
log.error(e);
// System.out.println((new StringBuilder()).append("Error in findInDictionary").append(e).toString());
}
return wrongWord;
}

private String strip(String paragraph)
{
int length = paragraph.length();
String newString = "";
for(int x = 0; x < length; x++ )
{ if(paragraph.substring(x, x + 1).compareTo("\"") != 0 && paragraph.substring(x, x + 1).compareTo("!") != 0 && paragraph.substring(x, x + 1).compareTo(".") != 0 && paragraph.substring(x, x + 1).compareTo(",") != 0 && paragraph.substring(x, x + 1).compareTo("?") != 0 && paragraph.substring(x, x + 1).compareTo(")") != 0 && paragraph.substring(x, x + 1).compareTo("(") != 0 && paragraph.substring(x, x + 1).compareTo("]") != 0 && paragraph.substring(x, x + 1).compareTo("[") != 0 && paragraph.substring(x, x + 1).compareTo(":") != 0 && paragraph.substring(x, x + 1).compareTo(";") != 0 && paragraph.substring(x, x + 1).compareTo("1") != 0 && paragraph.substring(x, x + 1).compareTo("2") != 0 && paragraph.substring(x, x + 1).compareTo("3") != 0 && paragraph.substring(x, x + 1).compareTo("4") != 0 && paragraph.substring(x, x + 1).compareTo("5") != 0 && paragraph.substring(x, x + 1).compareTo("6") != 0 && paragraph.substring(x, x + 1).compareTo("7") != 0 && paragraph.substring(x, x + 1).compareTo("8") != 0 && paragraph.substring(x, x + 1).compareTo("9") != 0 && paragraph.substring(x, x + 1).compareTo("0") != 0 && paragraph.substring(x, x + 1).compareTo("-") != 0 && paragraph.substring(x, x + 1).compareTo("_") != 0 && paragraph.substring(x, x + 1).compareTo(" ") != 0 && paragraph.substring(x, x + 1).compareTo("=") != 0 && paragraph.substring(x, x + 1).compareTo("|") != 0 && paragraph.substring(x, x + 1).compareTo("@") != 0 && paragraph.substring(x, x + 1).compareTo("#") != 0&& paragraph.substring(x, x + 1).compareTo("/") != 0&& paragraph.substring(x, x + 1).compareTo("\\") != 0&& paragraph.substring(x, x + 1).compareTo("^") != 0&& paragraph.substring(x, x + 1).compareTo("%") != 0 && paragraph.substring(x, x + 1).compareTo("$") != 0 && paragraph.substring(x, x + 1).compareTo("&") != 0&& paragraph.substring(x, x + 1).compareTo("{") != 0&& paragraph.substring(x, x +1).compareTo("}") != 0&& paragraph.substring(x, x + 1).compareTo("*") != 0&& paragraph.substring(x, x + 1).compareTo("<") != 0&& paragraph.substring(x, x + 1).compareTo(">") != 0)
newString = (new StringBuilder()).append(newString).append(paragraph.substring(x, x + 1)).toString();
}
return newString;
}

/**
* Adds Word to dictionary
* @param addword
* @return boolean if word was inserted or not
*/
public boolean addWord(String addword)
{
String path=Constants.SPELLPATH +"//6of12.txt";
String path1=Constants.SPELLPATH +"//2of4brif.txt";
String path2=Constants.SPELLPATH +"//2of12.txt";
String path3=Constants.SPELLPATH +"//5desk.txt";
String path4=Constants.SPELLPATH +"//3esl.txt";

try {

//Adds all words from file into hashset
BufferedReader in = new BufferedReader(new FileReader(path));
Set data = new HashSet();
String line = in.readLine();
while (line != null) {
data.add(line);
line = in.readLine();
}
in = new BufferedReader(new FileReader(path1));
line = in.readLine();
while (line != null) {
data.add(line);
line = in.readLine();
}
in = new BufferedReader(new FileReader(path2));
line = in.readLine();
while (line != null) {
data.add(line);
line = in.readLine();
}
in = new BufferedReader(new FileReader(path3));
line = in.readLine();
while (line != null) {
data.add(line);
line = in.readLine();
}
in = new BufferedReader(new FileReader(path4));
line = in.readLine();
while (line != null) {
data.add(line);
line = in.readLine();
}

//Returns false if word is already in file
if(data.contains(addword))
return false;

//else adds word to dictionary
BufferedWriter out = new BufferedWriter(new FileWriter(path, true));
out.write(addword.trim() "\n");
out.close();
} catch (Exception e) {
log.error(e);
//System.out.println("Error writing to text file" e);
return false;
}

return true;
}

}

[Post to Twitter] Tweet This Post  [Post to Plurk] Plurk This Post  [Post to Yahoo Buzz] Buzz This Post  [Post to Delicious] Delicious This Post  [Post to Digg] Digg This Post  [Post to Ping.fm] Ping This Post  [Post to Reddit] Reddit This Post  [Post to StumbleUpon] Stumble This Post 

No Comments »

No comments yet.

RSS feed for comments on this post.

Leave a comment

atriumax wordpress theme