-
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
-
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
-
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.
Code:
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();
}
}
Similar Threads
-
Replies: 2
Last Post: 06-14-2006, 03:16 PM
-
Replies: 1
Last Post: 05-13-2005, 06:46 AM
-
By Rob Abbe in forum Talk to the Editors
Replies: 44
Last Post: 01-13-2003, 02:57 PM
-
By Mike Tsakiris in forum .NET
Replies: 11
Last Post: 10-04-2002, 05:32 PM
-
By JJ in forum Enterprise
Replies: 1
Last Post: 07-06-2000, 04:50 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks