I did get this code for spell check program in java but here it says hashTable not resolved... can some one guide me.
import java.io.*;
import java.util.StringTokenizer;
public class SpellChecker {
HashTable dictionary;
public static void main(String argv[]) {
SpellChecker checker = new SpellChecker();
}
public SpellChecker() {
dictionary = new HashTable(53);
try {
String s, token;
// 2.a. Read dictionary from file
BufferedReader dictReader = new BufferedReader(new FileReader("dictionary.dat"));
while ((s = dictReader.readLine())!= null) {
StringTokenizer st = new StringTokenizer(s);
while (st.hasMoreTokens()) {
token = st.nextToken();
// key and value are identical
dictionary.put(token, token);
}
}
dictReader.close();
// 2.b. Read file with commonly misspelled words.
BufferedReader misspellings = new BufferedReader(new FileReader("dictionary2.dat"));
String wrongSpelling, rightSpelling;
while ((s = misspellings.readLine()) != null) {
StringTokenizer st = new StringTokenizer(s);
// should really check that both of these exist
wrongSpelling = st.nextToken();
rightSpelling = st.nextToken();
// key is the wrong spelling, value is the correct spelling
dictionary.put(wrongSpelling, rightSpelling);
}
misspellings.close();
// 2.c-f. Read in the text file to be checked & output corrected file.
BufferedReader inputFile = new BufferedReader(new FileReader("testfile.dat"));
BufferedWriter outputFile = new BufferedWriter(new FileWriter("checked.dat"));
// Preserve original line breaks by reading in one line at a time. Note, however, that
// we do not preserve other whitespace, nor do we handle punctuation.
while ((s = inputFile.readLine()) != null) {
StringTokenizer st = new StringTokenizer(s);
while (st.hasMoreTokens()) {
String inputWord = st.nextToken();
String outputWord = spellCheckWord(inputWord);
outputFile.write(outputWord+" ");
}
outputFile.newLine();
}
inputFile.close();
outputFile.close();
}
catch (IOException e) {
System.out.println("Error -- " + e.toString());
e.printStackTrace();
System.exit(-1);
}
}
public String spellCheckWord(String wordToCheck) {
String lookup, uninflectedWord;
String word = wordToCheck.toLowerCase();
// if spelt correctly, output as is
// if it is a common mispelling, output the corrected word
if ((lookup = (String)dictionary.get(word)) != null)
return lookup;
// Remove inflections at end of word and try again ("es", "s", "ing", "ed")
int length = word.length();
// first check for final 's'.
if (length > 1 && word.substring(length - 1).equals("s")) {
uninflectedWord = word.substring(0, length-1);
if ((lookup = (String)dictionary.get(uninflectedWord)) != null)
return lookup + "s";
// don't fail yet. fall through to 'es' check
}
if (length > 2 && word.substring(length-2).equals("es")) {
uninflectedWord = word.substring(0, length-2);
if ((lookup = (String)dictionary.get(uninflectedWord)) != null)
return lookup + "es";
else // not found
return word.toUpperCase();
}
if (length > 3 && word.substring(length - 3).equals("ing")) {
uninflectedWord = word.substring(0, length-3);
if ((lookup = (String)dictionary.get(uninflectedWord)) != null)
return lookup + "ing";
else // not found
return word.toUpperCase();
}
if (length > 2 && word.substring(length - 2).equals("ed")) {
uninflectedWord = word.substring(0, length-2);
if ((lookup = (String)dictionary.get(uninflectedWord)) != null)
return lookup + "ed";
else // not found
return word.toUpperCase();
}
// word was not found, even after "uninflecting". Assume it is misspelt and return
// it in ALL CAPS.
return word.toUpperCase();
}
}
spellcheck in javaspell check program using hash in java
Page 1 of 1
10 Replies - 5573 Views - Last Post: 12 January 2010 - 08:57 AM
Replies To: spellcheck in java
#2
Re: spellcheck in java
Posted 07 January 2010 - 04:29 AM
To use a Hashtable you have to import it. The Hashtable, as any other collection, is located in the java.util package. And you did a pretty common mistake, is Hashtable instead of HashTable.
And another thing, when posting a code use the code tags:
And another thing, when posting a code use the code tags:
This post has been edited by anonymouscodder: 07 January 2010 - 04:33 AM
#3
Re: spellcheck in java
Posted 08 January 2010 - 09:38 AM
#4
Re: spellcheck in java
Posted 08 January 2010 - 09:43 AM
Are you still having a problem? Or are you letting us know his solution worked?
#5
Re: spellcheck in java
Posted 08 January 2010 - 09:45 AM
anonymouscodder, on 7 Jan, 2010 - 03:29 AM, said:
To use a Hashtable you have to import it. The Hashtable, as any other collection, is located in the java.util package. And you did a pretty common mistake, is Hashtable instead of HashTable.
And another thing, when posting a code use the code tags:

And another thing, when posting a code use the code tags:
java.io.FileNotFoundException: dictionary.dat (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at SpellChecker.<init>(SpellChecker.java:22)
at SpellChecker.main(SpellChecker.java:12)
Error -- java.io.FileNotFoundException: dictionary.dat (The system cannot find the file specified)
after correcting as u said still these are the errors it shows.
#6
Re: spellcheck in java
Posted 08 January 2010 - 09:50 AM
Well,
That means that the system cannot find dictionary.dat
Is it in the same directory? Do you have your program in a jar file, and if so, do you have the .dat file being accessed from the jar?
java.io.FileNotFoundException: dictionary.dat (The system cannot find the file specified)
That means that the system cannot find dictionary.dat
Is it in the same directory? Do you have your program in a jar file, and if so, do you have the .dat file being accessed from the jar?
#7
Re: spellcheck in java
Posted 09 January 2010 - 01:18 PM
we were just given a dictionary from open office and i have placed them in the same folder. donno i am confused with these errors
java.io.FileNotFoundException: dictionary.dat (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at SpellChecker.<init>(SpellChecker.java:22)
at SpellChecker.main(SpellChecker.java:12)
Error -- java.io.FileNotFoundException: dictionary.dat (The system cannot find the file specified)
after correcting as u said still these are the errors it shows.
java.io.FileNotFoundException: dictionary.dat (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at SpellChecker.<init>(SpellChecker.java:22)
at SpellChecker.main(SpellChecker.java:12)
Error -- java.io.FileNotFoundException: dictionary.dat (The system cannot find the file specified)
after correcting as u said still these are the errors it shows.
#8
Re: spellcheck in java
Posted 09 January 2010 - 01:52 PM
The rules have been pointed out to you multiple times. Please follow them:
1) Use BBCode when posting. For example,
2) Include errors along with your code, and code to go with your errors (this means in the same post).
If you don't want to follow the rules, then we won't be able to give you the help you need.
1) Use BBCode when posting. For example,
2) Include errors along with your code, and code to go with your errors (this means in the same post).
If you don't want to follow the rules, then we won't be able to give you the help you need.
#9
Re: spellcheck in java
Posted 11 January 2010 - 03:32 PM
import java.io.*;
import java.util.StringTokenizer;
public class SpellChecker {
HashTable dictionary;
public static void main(String argv[]) {
SpellChecker checker = new SpellChecker();
}
public SpellChecker() {
dictionary = new HashTable(53);
try {
String s, token;
// 2.a. Read dictionary from file
BufferedReader dictReader = new BufferedReader(new FileReader("dictionary.dat"));
while ((s = dictReader.readLine())!= null) {
StringTokenizer st = new StringTokenizer(s);
while (st.hasMoreTokens()) {
token = st.nextToken();
// key and value are identical
dictionary.put(token, token);
}
}
dictReader.close();
// 2.b. Read file with commonly misspelled words.
BufferedReader misspellings = new BufferedReader(new FileReader("dictionary2.dat"));
String wrongSpelling, rightSpelling;
while ((s = misspellings.readLine()) != null) {
StringTokenizer st = new StringTokenizer(s);
// should really check that both of these exist
wrongSpelling = st.nextToken();
rightSpelling = st.nextToken();
// key is the wrong spelling, value is the correct spelling
dictionary.put(wrongSpelling, rightSpelling);
}
misspellings.close();
// 2.c-f. Read in the text file to be checked & output corrected file.
BufferedReader inputFile = new BufferedReader(new FileReader("testfile.dat"));
BufferedWriter outputFile = new BufferedWriter(new FileWriter("checked.dat"));
// Preserve original line breaks by reading in one line at a time. Note, however, that
// we do not preserve other whitespace, nor do we handle punctuation.
while ((s = inputFile.readLine()) != null) {
StringTokenizer st = new StringTokenizer(s);
while (st.hasMoreTokens()) {
String inputWord = st.nextToken();
String outputWord = spellCheckWord(inputWord);
outputFile.write(outputWord+" ");
}
outputFile.newLine();
}
inputFile.close();
outputFile.close();
}
catch (IOException e) {
System.out.println("Error -- " + e.toString());
e.printStackTrace();
System.exit(-1);
}
}
public String spellCheckWord(String wordToCheck) {
String lookup, uninflectedWord;
String word = wordToCheck.toLowerCase();
// if spelt correctly, output as is
// if it is a common mispelling, output the corrected word
if ((lookup = (String)dictionary.get(word)) != null)
return lookup;
// Remove inflections at end of word and try again ("es", "s", "ing", "ed")
int length = word.length();
// first check for final 's'.
if (length > 1 && word.substring(length - 1).equals("s")) {
uninflectedWord = word.substring(0, length-1);
if ((lookup = (String)dictionary.get(uninflectedWord)) != null)
return lookup + "s";
// don't fail yet. fall through to 'es' check
}
if (length > 2 && word.substring(length-2).equals("es")) {
uninflectedWord = word.substring(0, length-2);
if ((lookup = (String)dictionary.get(uninflectedWord)) != null)
return lookup + "es";
else // not found
return word.toUpperCase();
}
if (length > 3 && word.substring(length - 3).equals("ing")) {
uninflectedWord = word.substring(0, length-3);
if ((lookup = (String)dictionary.get(uninflectedWord)) != null)
return lookup + "ing";
else // not found
return word.toUpperCase();
}
if (length > 2 && word.substring(length - 2).equals("ed")) {
uninflectedWord = word.substring(0, length-2);
if ((lookup = (String)dictionary.get(uninflectedWord)) != null)
return lookup + "ed";
else // not found
return word.toUpperCase();
}
// word was not found, even after "uninflecting". Assume it is misspelt and return
// it in ALL CAPS.
return word.toUpperCase();
}
}
[ERROR]
we were just given a dictionary from open office and i have placed them in the same folder. donno i am confused with these errors
java.io.FileNotFoundException: dictionary.dat (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at SpellChecker.<init>(SpellChecker.java:22)
at SpellChecker.main(SpellChecker.java:12)
Error -- java.io.FileNotFoundException: dictionary.dat (The system cannot find the file specified)
after correcting as u said still these are the errors it shows.
[/ERROR]
#10
Re: spellcheck in java
Posted 11 January 2010 - 09:00 PM
java.io.FileNotFoundException: dictionary.dat (The system cannot find the file specified)
What other explanations do you need ? Seems clear to me. No need to post hundred lines of code.
Just try it with the only code you need to test
What other explanations do you need ? Seems clear to me. No need to post hundred lines of code.
Just try it with the only code you need to test
BufferedReader dictReader = new BufferedReader(new FileReader("dictionary.dat"));
while ((s = dictReader.readLine())!= null) {
System.out.println(s);
}
#11
Re: spellcheck in java
Posted 12 January 2010 - 08:57 AM
//////BufferedReader dictReader = new BufferedReader(new FileReader ("C:\\Users\fatima\desktop\zvijezda\dictionary.dat"));///////
while ((s = dictReader.readLine())!= null) {
StringTokenizer st = new StringTokenizer(s);
while (st.hasMoreTokens()) {
token = st.nextToken();
// key and value are identical
dictionary.put(token, token);
}
}
dictReader.close();
// 2.b. Read file with commonly misspelled words.
BufferedReader misspellings = new BufferedReader(new FileReader("c:\Users\fatima\desktop\zvijezda\dictionary.dat"));
String wrongSpelling, rightSpelling;
while ((s = misspellings.readLine()) != null) {
StringTokenizer st = new StringTokenizer(s);
// should really check that both of these exist
wrongSpelling = st.nextToken();
rightSpelling = st.nextToken();
// key is the wrong spelling, value is the correct spelling
dictionary.put(wrongSpelling, rightSpelling);
}
misspellings.close();
// 2.c-f. Read in the text file to be checked & output corrected file.
BufferedReader inputFile = new BufferedReader(new FileReader("c:\Users\fatima\desktop\zvijezda\testfile.dat"));
BufferedWriter outputFile = new BufferedWriter(new FileReader("c:\Users\fatima\desktop\zvijezda\checked.dat"));
// Preserve original line breaks by reading in one line at a time. Note, however, that
// we do not preserve other whitespace, nor do we handle punctuation.
while ((s = inputFile.readLine()) != null) {
StringTokenizer st = new StringTokenizer(s);
while (st.hasMoreTokens()) {
String inputWord = st.nextToken();
String outputWord = spellCheckWord(inputWord);
outputFile.write(outputWord+" ");
}
outputFile.newLine();
}
inputFile.close();
outputFile.close();
[/quote]
error log
Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ ) the line is within slashes is where this error is, i dont know why it doesnt recognise the file :S
at SpellChecker.<init>(SpellChecker.java:31)
at SpellChecker.main(SpellChecker.java:14)
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote







|