Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this week\'s lab, you will use two of the classes in the Java Collection Fra

ID: 3722705 • Letter: F

Question

For this week's lab, you will use two of the classes in the Java Collection Framework: HashSet and TreeSet. You will use these classes to implement a spell checker.

Set Methods

For this lab, you will need to use some of the methods that are defined in the Set interface. Recall that if set is a Set, then the following methods are defined:

set.size() -- Returns the number of items in the set.

set.add(item) -- Adds the item to the set, if it is not already there.

set.contains(item) -- Check whether the set contains the item.

set.isEmpty() -- Check whether the set is empty.

You will also need to be able to traverse a set, using either an iterator or a for-each loop.

Reading a Dictionary

The file /classes/s09/cs225/words.txt http://math.hws.edu/eck/cs225/s09/lab9/words.txt contains a list of English words, with one word on each line. You will look up words in this list to check whether they are correctly spelled. To make the list easy to use, you can store the words in a set. Since there is no need to have the words stored in order, you can use a HashSet for maximum efficiency.

Use a Scanner to read the file. Recall that you can create a scanner, filein, for reading from a file with a statement such as:

and that a file can be processed, token by token, in a loop such as:

(For the wordlist file, a token is simply a word.)

Start your main program by reading the words from /classes/s09/cs225/words.txt and storing them in aHashSet<String>. For the purposes of this program, convert all words to lower case before putting them in the set. To make sure that you've read all the words, check the size of the set. (It should be 73845.) You could also use the contains method to check for the presence of some common word in the set.

Checking the Words in a File

Once you have the list of words in a set, it's easy to read the words from a file and check whether each word is in the set. Start by letting the user select a file. You can either let the user type the name of the file or you can use the following method:

Use a Scanner to read the words from the selected file. In order to skip over any non-letter characters in the file, you can use the following command just after creating the scanner (where in is the variable name for the scanner):

(In this statement, "[^a-zA-Z]+" is a regular expression that matches any sequence of one or more non-letter characters. This essentially makes the scanner treat any non-letter the way it would ordinarily treat a space.)

You can then go through the file, read each word (converting it to lower case) and check whether the set contains the word. At this point, just print out any word that you find that is not in the dictionary.

Providing a List of Possible Correct Spellings

A spell checker shouldn't just tell you what words are misspelled -- it should also give you a list of possible correct spellings for that word. Write a method

that creates and returns a TreeSet<String> containing variations on badWord that are contained in the dictionary. In your main program, when you find a word that is not in the set of legal words, pass that word to this method (along with the set). Take the return value and output any words that it contains; these are the suggested correct spellings of the misspelled word. Here for example is part of the output from my program when it was run with the HTML source of this page as input:

Note that I have written my program so that it will not output the same misspelled word more than once. (I do this by keeping a set of misspelled words that have been output.) If my corrections() method returns an empty set, I output the message "(no suggestions)". Since the corrections are stored in a tree set, they are automatically printed out in alphabetical order with no repeats.

The possible corrections that I consider are as follows:

Delete any one of the letters from the misspelled word.

Change any letter in the misspelled word to any other letter.

Insert any letter at any point in the misspelled word.

Swap any two neighboring characters in the misspelled word.

Insert a space at any point in the misspelled word (and check that both of the words that are produced are in the dictionary)

For constructing the possible corrections, you will have to make extensive use of substrings. If w is a string, then w.substring(0,i) is the string consisting of the first i characters in w (not including the character in position i, which would be character number i+1). And w.substring(i) consists of the characters of w from position i through the end of the string. For example, if ch is a character, then you can change the i-th character of w to ch with the statement:

Also, you will find it convenient to use a for loop in which the loop control variable is a char:

Explanation / Answer

SpellCheck .java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.TreeSet;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class SpellCheck {

   private static HashSet<String> dictionary = new HashSet<>();

   public static void main(String[] args) {

       // Call to create dictionary
       //Change the file name as per your directory structure
       createDictionary("C:\Users\MOHAMMAD WASEEM AKRA\Downloads\words.txt");

       // Printing size of the Dictionary
       System.out.println("Size of the dictionary is : " + dictionary.size());
      
       //Calling checkWords
      
       checkWords();
   }

   // This method will be used to read data from file and store it in dictionary ,
   // This dictionary can be further used to check the words whether they are
   // correctly spelled or not.
   static void createDictionary(String fileName) {
       try {
           Scanner filein = new Scanner(new File(fileName));
           while (filein.hasNext()) {
               String tk = filein.next();
               // Convert to lower letter
               tk.toLowerCase();
               // Add to dictionary
               dictionary.add(tk);
           }
       } catch (FileNotFoundException e) {
           JOptionPane.showMessageDialog(null, "Please Enter the correct file.");
       }
   }

   static void checkWords() {
       File file = getInputFileNameFromUser();
       try {
           Scanner in = new Scanner(file);
           // Use delimeter
           in.useDelimiter("[^a-zA-Z]+");
           while (in.hasNext()) {
               String tk = in.next();
               // Convert to lower letter
               tk.toLowerCase();
               // Check if the word is not in dictionary , then print the suggestions
               if (!dictionary.contains(tk)) {

                   TreeSet<String> suggestions = corrections(tk, dictionary);

                   if(suggestions!=null)
                   {
                   // Print the word and the suggestions
                   System.out.println(tk + " : " + suggestions);
                   }
                   else
                   {
                       System.out.println(tk + " : " + "(no suggestions)");
                   }
               }
           }
       } catch (FileNotFoundException e) {
           JOptionPane.showMessageDialog(null, "Please Enter the correct file.");
       }
   }

   /**
   * Lets the user select an input file using a standard file selection dialog
   * box. If the user cancels the dialog without selecting a file, the return
   * value is null.
   */
   static File getInputFileNameFromUser() {
       JFileChooser fileDialog = new JFileChooser();
       fileDialog.setDialogTitle("Select File for Input");
       int option = fileDialog.showOpenDialog(null);
       if (option != JFileChooser.APPROVE_OPTION)
           return null;
       else
           return fileDialog.getSelectedFile();
   }

   static TreeSet<String> corrections(String badWord, HashSet<String> dictionary) {
       TreeSet<String> corrections = new TreeSet<>();
       int badWordLen = badWord.length();
       // Swapping i with i+1
       for (int i = 1; i < badWordLen - 1; i++) {
           corrections.add(
                   badWord.substring(0, i) + badWord.charAt(i + 1) + badWord.charAt(i) + badWord.substring(i + 2));
       }
       // deleting one char, skipping i
       for (int i = 0; i < badWordLen; i++) {
           corrections.add(badWord.substring(0, i) + badWord.substring(i + 1));
       }
       // inserting one char
       for (int i = 0; i < badWordLen + 1; i++) {
           for (char j = 'a'; j <= 'z'; j++)
               corrections.add(badWord.substring(0, i) + j + badWord.substring(i));
       }
       return corrections;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote