For this week’s lab, you will use two of the classes in the JavaCollection Framework: HashSet and Tr

Need your ASSIGNMENT done? Use our paper writing service to score better and meet your deadline.


Order a Similar Paper Order a Different Paper

For this week’s lab, you will use two of the classes in the JavaCollection Framework: HashSet and TreeSet. Youwill use these classes to implement a spell checker. Set Methods
For this lab, you will need to use some of the methods that aredefined in the Set interface. Recall that if set is a Set, then thefollowing methods are defined: You will also need to be able to traverse a set, using either aniterator or a for-each loop. Reading a Dictionary The file words.txt (in the code directory) contains a list ofEnglish words, with one word on each line. You will look up wordsin this list to check whether they are correctly spelled. To makethe list easy to use, you can store the words in a set. Since thereis no need to have the words stored in order, you can use aHashSet for maximum efficiency. Use a Scanner to read the file. You can create a scanner,filein, for reading from a file with a statement suchas:                      filein = new Scanner
(new File(“/classes/s09/cs225/words.txt”)); and that a file can be processed, token by token, in a loop suchas: while (filein.hasNext()) {
      String tk = filein.next();
      process(tk); // do something withthe token
} (For the wordlist file, a token is simply a word.) Start your main program by reading the words from words.txt andstoring them in a HashSet. For the purposesof this program, convert all words to lower casebefore putting them in the set. To make sure that you’ve read allthe words, check the size of the set. (It should be 72875.) Youcould also use the contains method to check for the presence ofsome 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 thewords from a file and check whether each word is in the set. Startby letting the user select a file. You can either let the user typethe name of the file or you can use the following method:          /**
          * Lets theuser select an input file using a standard file
          * selectiondialog box. If the user cancels the dialog
          * withoutselecting a file, the return value is null.
          */
         static FilegetInputFileNameFromUser() {
           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();
         } Use a Scanner to read the words from the selected file. In order toskip over any non-letter characters in the file, you can use thefollowing command just after creating the scanner (where in is thevariable name for the scanner): in.useDelimiter(“[^a-zA-Z]+”); (In this statement, “[^a-zA-Z]+” is a regular expression thatmatches any sequence of one or more non-letter characters. Thisessentially makes the scanner treat any non-letter the way it wouldordinarily treat a space.)
You can then go through the file, read each word (converting it tolower case) and check whether the set contains the word. At thispoint, just print out any word that you find that is not in thedictionary. Providing a List of Possible CorrectSpellings A spell checker shouldn’t just tell you what words aremisspelled — it should also give you a list of possible correctspellings for that word. Write a method static TreeSet corrections(String badWord, HashSetdictionary) that creates and returns a TreeSetcontaining variations on badWord that are contained in thedictionary. In your main program, when you find a word that is notin the set of legal words, pass that word to this method (alongwith the set). Take the return value and output any words that itcontains; these are the suggested correct spellings of themisspelled word. Here, for example, is part of the output from asample program when it was run with the HTML source of this page asinput: html: (no suggestions)
cpsc: (no suggestions)
hashset: hash set
treeset: tree set
cvs: cs, vs
isempty: is empty
href: ref
txt: tat, tet, text, tit, tot, tut
filein: file in
pre: are, ere, ire, ore, pare, pee, per, pie, poe, pore, prep,pres,
       prey, pro, pry, pure, pyre,re
hasnext: has next
wordlist: word list
getinputfilenamefromuser: (no suggestions)
jfilechooser: (no suggestions)
filedialog: file dialog
setdialogtitle: (no suggestions)
int: ant, dint, hint, in, ina, inc, ind, ink, inn, ins, inti,into,
      it, lint, mint, nit, pint,tint Note that the program was written so that it will not output thesame misspelled word more than once. (This is done by keeping a setof misspelled words that have been output.) If thecorrections() method returns an empty set, the programoutputs the message “(no suggestions)”. Since the corrections arestored in a tree set, they are automatically printed out inalphabetical order with no repeats. The possible corrections that the program considers are asfollows:
• Delete any one of the letters from the misspelled word.
• Change any letter in the misspelled word to any otherletter.
• 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 checkthat both of the words that are produced are in thedictionary) For constructing the possible corrections, you will have to makeextensive use of substrings. If w is a string, thenw.substring(0,i) is the string consisting of the first icharacters in w (not including the character in position i, whichwould be character number i+1). Andw.substring(i) consists of the characters of w fromposition i through the end of the string. For example, ifch is a character, then you can change the i-thcharacter of w to ch with the statement: String s = w.substring(0,i) + ch + w.substring(i+1); Also, you will find it convenient to use a for loop inwhich the loop control variable is a char: for (char ch = ‘a’; ch . . .