spellcheck in java

spell check program using hash in java

Page 1 of 1

10 Replies - 13479 Views - Last Post: 12 January 2010 - 08:57 AM Rate Topic: -----

#1 fatum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-January 10

spellcheck in java

Post icon  Posted 07 January 2010 - 04:21 AM

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();
}

}

Is This A Good Question/Topic? 0
  • +

Replies To: spellcheck in java

#2 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

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:
:code:

This post has been edited by anonymouscodder: 07 January 2010 - 04:33 AM

Was This Post Helpful? 0
  • +
  • -

#3 fatum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-January 10

Re: spellcheck in java

Posted 08 January 2010 - 09:38 AM

View Postanonymouscodder, 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:
:code:

Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

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?
Was This Post Helpful? 0
  • +
  • -

#5 fatum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-January 10

Re: spellcheck in java

Posted 08 January 2010 - 09:45 AM

View Postanonymouscodder, 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:
:code:




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.
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: spellcheck in java

Posted 08 January 2010 - 09:50 AM

Well,
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?
Was This Post Helpful? 0
  • +
  • -

#7 fatum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-January 10

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.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10647
  • View blog
  • Posts: 39,539
  • Joined: 27-December 08

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, :code:
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.
Was This Post Helpful? 1
  • +
  • -

#9 fatum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-January 10

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]
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

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

BufferedReader dictReader = new BufferedReader(new FileReader("dictionary.dat"));

while ((s = dictReader.readLine())!= null) {
  System.out.println(s);
}


Was This Post Helpful? 0
  • +
  • -

#11 fatum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-January 10

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)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1