?
Solved

SEARCHING WORDS IN A DICTIONARY

Posted on 2003-03-12
23
Medium Priority
?
509 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
23 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
Technology Partners: 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

800 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