10 Replies - 9181 Views - Last Post: 19 February 2010 - 08:25 PM Rate Topic: -----

#1 53greek53  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 27-November 08

Java Spell Checker

Posted 19 February 2010 - 07:14 PM

Hello all, I am attempting to write a spell checker in java. What I would like to happen is for the program to read a list of words and return the mispelled words. I can't figure out how to get java to read the text file of words though. Any help would be appreciated.





Here is my code:

import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.ArrayList;

public class SpellCheck {

public SpellCheck(String wordFileName)
      throws IOException
   {
      filename = wordFileName;

      BufferedReader in = new BufferedReader(
         new FileReader(filename));

      wordsList = new ArrayList();
      boolean more = true;

      while (more)
      {
         String w = in.readLine();
         if (w == null)
            more = false;
         else
            wordsList.add(w.toLowerCase());

      }
      in.close();
   }

   /**
      Checks if a word is in the word list
      @param word the word to check for
      @return true if word was found, false otherwise
   */
   public boolean check(String word)
   {
      boolean isFound = false;

      for (int i = 0; i < wordsList.size();i++)
      {
         if (word.equals(wordsList.get(i)))
            isFound = true;
      }

      return isFound;
   }

   private String filename;
   private ArrayList wordsList;
}




And the tester.

import java.io.FileReader;
import java.io.  Reader;
import java.util.StringTokenizer;

public class SpellCheckTester {

/**
   This class tests the SpellChecker class
*/

   public static void main(String[] args)
   {
      if (args.length < 2)
      {
         System.out.println("Usage: ExP15_9 file wordfile");
         return;
      }

      try
      {
         SpellCheck sc = new SpellCheck(args[1]);

         BufferedReader in = new BufferedReader(new FileReader(args[0]));
         boolean more = true;
         while (more)
         {
            String line = in.readLine();
            if (line == null)
               more = false;
            else
            {
               StringTokenizer tokenizer = new StringTokenizer(line.toLowerCase());
               while (tokenizer.hasMoreTokens())
               {
                  String word = tokenizer.nextToken();
                  if (!sc.check(word))
                     System.out.println(word);
               }
            }
         }
         in.close();
      }
      catch (IOException exception)
      {
         System.out.println(exception);
      }
   }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Java Spell Checker

#2 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Java Spell Checker

Posted 19 February 2010 - 07:21 PM

This is how I've been doing it. It puts the entire file in the tempFile string:
fstream = new FileInputStream(fileToRead);
br = new BufferedInputStream(fstream);
in = new DataInputStream(br);

try {
	tempFile = "";
	while (in.available() != 0) {
		tempFile += in.readLine();
	}
} catch (Exception e) {
	JOptionPane.showMessageDialog(mainGUI.pane,"There was an error reading the file.");
	return;
}


Was This Post Helpful? 0
  • +
  • -

#3 53greek53  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 27-November 08

Re: Java Spell Checker

Posted 19 February 2010 - 07:37 PM

Does it need the entire file path? Also would this be inserted in the tester?
Was This Post Helpful? 0
  • +
  • -

#4 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Java Spell Checker

Posted 19 February 2010 - 07:39 PM

This would be inserted wherever you need to read the file.
Was This Post Helpful? 0
  • +
  • -

#5 53greek53  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 27-November 08

Re: Java Spell Checker

Posted 19 February 2010 - 07:43 PM

View Postxor-logic, on 19 February 2010 - 06:39 PM, said:

This would be inserted wherever you need to read the file.



Im sorry I don't understand how this works or where to put it could you please explain?
Was This Post Helpful? 0
  • +
  • -

#6 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Java Spell Checker

Posted 19 February 2010 - 07:47 PM

//The argument passed here (fileToRead) is the file you want to read.
fstream = new FileInputStream(fileToRead);

//Create a BufferedInputStream with the FileInputStream as the argument
br = new BufferedInputStream(fstream);

//Create a new DataInputStream with the BufferedInputStream as an argument
in = new DataInputStream(br);

try {
        //makes sure the String tempFile is clear
        tempFile = "";

        /*The following code states that while there are lines
         remaining in the DataInputStream, add them to tempFile*/
        while (in.available() != 0) {
                tempFile += in.readLine();
        }

} catch (Exception e) {
       //This gets executed if the file read failed for some reason
        JOptionPane.showMessageDialog(mainGUI.pane,"There was an error reading the file.");
        return;
}



Edit:

You can replace all that first bit with :
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(fileToRead)));


This post has been edited by xor-logic: 19 February 2010 - 07:49 PM

Was This Post Helpful? 0
  • +
  • -

#7 53greek53  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 27-November 08

Re: Java Spell Checker

Posted 19 February 2010 - 08:09 PM

Doesn't seem to be working when I insert it just creates many errors.
Was This Post Helpful? 0
  • +
  • -

#8 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Java Spell Checker

Posted 19 February 2010 - 08:12 PM

View Post53greek53, on 19 February 2010 - 07:09 PM, said:

Doesn't seem to be working when I insert it just creates many errors.

Well let's see how you're trying to insert it (God that sounded dirty). :P
Was This Post Helpful? 0
  • +
  • -

#9 53greek53  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 27-November 08

Re: Java Spell Checker

Posted 19 February 2010 - 08:21 PM

View Postxor-logic, on 19 February 2010 - 07:12 PM, said:

View Post53greek53, on 19 February 2010 - 07:09 PM, said:

Doesn't seem to be working when I insert it just creates many errors.

Well let's see how you're trying to insert it (God that sounded dirty). :P


hahahhaha thats what she said

Class:


import java.io.IOException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;

/**
   A class to check the spelling of all words in a file.
*/
public class SpellCheck
{
   /**
      Construct a SpellingCheck object.
      @param aFileName the name of the file
   */
   public SpellCheck(String wordFileName)
         throws IOException
   {
      filename = wordFileName;
      Scanner in = new Scanner(new FileReader(filename));

      wordsList = new ArrayList<String>();
      boolean more = true;

      while (more)
      {
         if (!in.hasNextLine())
            more = false;
         else
         {
            String w = in.nextLine();
            wordsList.add(w.toLowerCase());
         }
      }
      in.close();
   }

   /**
      Checks if a word is in the word list
      @param word the word to check for
      @return true if word was found, false otherwise
   */
   public boolean check(String word)
   {
      boolean isFound = false;

      for (int i = 0; i < wordsList.size();i++)
      {
         if (word.equals(wordsList.get(i)))
            isFound = true;
      }

      return isFound;
   }

   private String filename;
   private ArrayList<String> wordsList;
}







Tester:


import java.io.IOException;
import java.io.FileReader;
import java.util.Scanner;

/**
   This class tests the SpellChecker class.
*/
public class SpellCheckTester
{
   public static void main(String[] args)
   {
      if (args.length < 2)
      {
         System.out.println("Usage: ExP16_9 file wordfile");
         return;
      }
     fstream = new FileInputStream(fileToRead); 
br = new BufferedInputStream(fstream); 
in = new DataInputStream(br); 
 
try { 
        tempFile = "words.txt"; 
        while (in.available() != 0) { 
                tempFile += in.readLine(); 
        } 
} catch (Exception e) { 
        JOptionPane.showMessageDialog(mainGUI.pane,"There was an error reading the file."); 
        return; 

   }
}

Was This Post Helpful? 1
  • +
  • -

#10 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Java Spell Checker

Posted 19 February 2010 - 08:25 PM

View Postxor-logic, on 19 February 2010 - 08:47 PM, said:

try {
        //makes sure the String tempFile is clear
        tempFile = "";
        /*The following code states that while there are lines
         remaining in the DataInputStream, add them to tempFile*/
        while (in.available() != 0) {
                tempFile += in.readLine();
        }



Repeatedly concatenating the Strings is a bad habit, since Strings are immutable, a new String will have to be created each time it is appended.

You should use a StringBuilder with the append(String) method, which would have the same effect, but would run quicker (I think I saw that it ran in linear time compared to regular Strings which were quadratic (or somewhere around there))

The Java compiler will sometimes optimize the code to use this class anyway, but it's good practice
Was This Post Helpful? 1
  • +
  • -

#11 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Java Spell Checker

Posted 19 February 2010 - 08:25 PM

View Post53greek53, on 19 February 2010 - 07:21 PM, said:

fstream = new FileInputStream(fileToRead); 
br = new BufferedInputStream(fstream); 
in = new DataInputStream(br); 
 
try { 
        tempFile = "words.txt"; 
        while (in.available() != 0) { 
                tempFile += in.readLine(); 
        } 
} catch (Exception e) { 
        JOptionPane.showMessageDialog(mainGUI.pane,"There was an error reading the file."); 
        return; 

   }
}

You need to create these variables in your code. The first three are of the object type shown on the right, for example in is a DataInputStream, so:
DataInputStream in = null;


And tempFile is a string. You might want to rename in though since you use a scanner of the same name in your own code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1