?
Solved

SEARCHING WORDS IN A DICTIONARY

Posted on 2003-03-12
23
Medium Priority
?
510 Views
Last Modified: 2007-11-27
I have a 'dictionary.dat' file and I want to search for words from it. This is a scrabble game, by the way. How do I go about this?
Do I have to load all the words from the dictionary into a java data structure and then search through it? And if I have to, how do I go about doing this?
Thank You.
0
Comment
Question by:adura
22 Comments
 
LVL 2

Expert Comment

by:antonsigur
ID: 8122183
This puts the "found" to true if the "stringToFind" is found in the file...

       boolean found = false; //Tell me if record is found
       String stringToFind = "someWord"; //The word you are serching for
        File f = new File("dictionary.dat");  //Open the file namde dictionary.dat
       FileInputStream fis = null;
       try {
           fis = new FileInputStream(f);
       } catch (FileNotFoundException e) {
           //Error handling if file not found
       }
       BufferedInputStream bis = new BufferedInputStream(fis);  
        DataInputStream dis = new DataInputStream(bis);  
        String record = null;  
        try {

           
       while ( (record=dis.readLine()) != null ) {  //Read all the file
            if (stringToFind.equalsIgnoreCase(record.trim()))
               found = true; // The record was found    
       }  
          dis.close(); //close the stream....
   
    } catch (IOException e) {//error handling}  
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8122202
You should add a "break;" in the if-statement, so you won't need to go trhough all the file...

If you are reading the file often, you should read it all in a String array and search it (it's faster) but that should also depend on the dictionary size....
0
 

Author Comment

by:adura
ID: 8122209
so do i have to have a new class for this? or this can be done in the class?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:adura
ID: 8122227
5,000 words?
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8122241
You should put it @-least in a special function.... but a class would be more flexible, for later implemention changes...

public class Search { .... }


----- The other class

Search search = new Search("dictionary.dat");

.....

if ( search.forWord("someWord") ) {
  //we found the word!!
}
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8122270
5000 words is not that much, you can read it into memory if you want. You could sort the String array, and that would provide you much faster search, if you would like to implement that... ( but that way you need to code much more than I was showing you... )
0
 

Author Comment

by:adura
ID: 8122288
thank you, that was just an estimate of the words, i probably have tens of thousands.
Thanks a lot.
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8122320
Is this working for you?
0
 

Author Comment

by:adura
ID: 8122324
thank you, that was just an estimate of the words, i probably have tens of thousands.
Thanks a lot.
0
 

Author Comment

by:adura
ID: 8122328
i am trying it out at the moment.
0
 

Author Comment

by:adura
ID: 8122558
what's the filenotfound exception?
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8122623
If it don't find the file (you give a wrong filename)
0
 

Author Comment

by:adura
ID: 8122691
there's something about deprecation i.e. using a BufferedReader so I am about to try that out. But apart from that it looks okay, although, it accepted a word that was not in the dictionary "yu" LOL
0
 

Author Comment

by:adura
ID: 8122716
looking at the BufferedInputStream in your code, that should be okay though, but I'll try
0
 

Author Comment

by:adura
ID: 8122795
Thank you so so so so much, I used a BufferedReader and it is working perfectly, thank you so much, that's the last bit of my project due on Friday. You've saved me.
0
 
LVL 2

Accepted Solution

by:
antonsigur earned 400 total points
ID: 8122830
Sorry for that, this is non depricated:

       boolean found = false; //Tell me if record is found
       String stringToFind = "someWord"; //The word you are serching for
        File f = new File("dictionary.dat");  //Open the file namde dictionary.dat
       FileInputStream fis = null;
       try {
           fis = new FileInputStream(f);
       } catch (FileNotFoundException e) {
           //Error handling if file not found
       }
       BufferedInputStream bis = new BufferedInputStream(fis);
        InputStreamReader dis = new InputStreamReader(bis);
        BufferedReader bfr = new BufferedReader(dis);
        String record = null;
        try {


       while ( (record=bfr.readLine()) != null ) {  //Read all the file
            if (stringToFind.equalsIgnoreCase(record.trim()))
               found = true; // The record was found
       }
          dis.close(); //close the stream....

    } catch (IOException e) {//error handling
        }


(buffered reader insted of datainput stream. I copied to old code I guess... but this is good!!
0
 

Author Comment

by:adura
ID: 8122996
i did exactly this so thanks a lot
0
 

Expert Comment

by:ianhowlett
ID: 8123512
You're probably going to need to do this lookup more than once, so it makes sense to read all the data into memory just once. (Unless the dataset is huge or you're running on a very weedy computer you'll be OK.)

So use the file reading code given elsewhere here, and then just store the data into a HashMap. This internally uses a Hash table (dig out the computer science books for more), which essentially does a calculation on a value to enable it to be retrieved very efficiently.

You may wish to convert the words to upper case first (using the stringVariable.toUpperCase() method. This will avoid case mismatch problems.

You can then use hashmapObject.containsKey("TEST") to find the word.
0
 
LVL 3

Expert Comment

by:allahabad
ID: 8124680
Try this one:

This uses binary search to look for the word. Assumed that file will have each word on one line.

import java.io.*;
import java.util.*;

public class WordSearch {


    public static void main(String args[]) throws IOException {

         ArrayList words = loadFile("dictionary.dat"); // Load the word in arrayList

         Collections.sort(words); //sort ArrayList required for binarySearch

         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
         while (true) {
            System.out.print("Enter Word (q to quit) ");
            System.out.print("\n");
            String option = stdin.readLine();

            if (option.equalsIgnoreCase("q")) System.exit(-1);
            else {

               if (Arrays.binarySearch(words.toArray(),option.toLowerCase())== -1)
                   System.out.println("Word is not found in dictionary.");
               else
                   System.out.println("Word Found");
            }

         }

    }

   private static ArrayList loadFile(String fileName) throws IOException{
        String word;

        File file  = new File(fileName);
        BufferedReader bfreader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        ArrayList wordList = new ArrayList();
        while ((word=bfreader.readLine()) != null){
         wordList.add(word.toLowerCase());

        }

        return wordList;

   }
}
0
 

Expert Comment

by:CleanupPing
ID: 9058734
adura:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 35

Expert Comment

by:girionis
ID: 9714448

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points to antonsigur

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0
 

Expert Comment

by:phil8258
ID: 20362150
I have picked up the code submitted by allahabad.
Although I'm probably trying to do something completely different, this topic seems very compatible with the solution that i'm seeking.

I'm taking a character stream say ABB and creating an array of all words begining with it, then getting another character, "R", say, and seeing which words in my array contain ABBR and basically trying to get the largest word that fits.
(in this case i'd eventually end up with a choice of abbreviate, abbreviated, abbreviation, abbreviature)

Problem I seem to have with your code (using manually with user keyboard input) is that it always returns found! - even for words that don't exist...

"TEST" will return found.
"TESTX" will return found (it shouldn't)
"TESXT" will return foun (it shouldn't)

Can anyone help me fix this or explain if this is just because the above code was developed for a different application?

Cheers,

Phil.

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month8 days, 19 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question