Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

java dictionary

hi guys i created a java program (element) dat looks into a file for any words a user inputs and displays any output.

i would like to knwo how i could adavcne this tool so that the spellchcekr goes thoug the same file, and everytime it finds a word dat nt in da dictionary it can ask me to accept the wprd or enter a replace ment one.

this is da code i done so far:

import java.io.*;
public class spell2
{
public static void main(String[] args) throws Exception
{
BufferedReader inone =new BufferedReader(new FileReader("words.txt"));
BufferedReader in =new BufferedReader(new InputStreamReader(System.in));
System.out.print(">");
String s=in.readLine();
while (s.length()>0)
{ String t=inone.readLine();
while (t!=null)
{
if (t.startsWith(s))
System.out.println(t);
t=inone.readLine();
}
System.out.print(">");
s=in.readLine();
inone =new BufferedReader(new FileReader("words.txt"));
}
}
}

the file i used is a txt file full of words.

thank u for any help
[1210 byte] By [tanbir_1987] at [2007-11-11 8:01:47]
# 1 Re: java dictionary
I think your post needs a spell checker.

Run each word in the file through the dictionary list of words. Keeping the dictionary in alphabetical order, you can use a binary search to drastically increase the speed of the word search. If the word isn't found, find the closest match (compareTo) and present the option to the user
Phaelax at 2007-11-11 22:36:36 >
# 2 Re: java dictionary
it appears that "words.txt" is essentially your dictionary and that you're checking works entered via the standard output. true, you could load the words in the file into a list, sort the list, and then search it later quickly using binary search. however, you could run into memory problems if "words.txt" is massive. and besides, you're interested in re-scanning the dictionary file. so, the following code should help; it is an adaptation of your code.

Tip: When posting, you can use the # button to insert code that is laid out well with indenting so that it is easier to read.

import java.io.*;

public class spell2
{
public static void main(String[] args)
throws Exception
{
RandomAccessFile inone = new RandomAccessFile("words.txt", "r");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print(">");
boolean foundMatch;
String s = in.readLine();
while (s.length() > 0) {
inone.seek(0L); // return to the beginning of the dictionary
foundMatch = false; // unless proven otherwise
String t = inone.readLine();
while (t != null) {
if (t.equalsIgnoreCase(s)) {
foundMatch = true;
break;
}
t = inone.readLine();
}
if (foundMatch == false) {
System.out.println("Unknown word: " + s);
// add code to keep/replace here
}
System.out.print(">");
s = in.readLine();
}
inone.close();
}
}
xtsunami at 2007-11-11 22:37:41 >