
Insert a space at any point in the misspelled word (and check that both of the words that are produced are in the dictionary)įor constructing the possible corrections, you will have to make extensive use of substrings.Swap any two neighboring characters in the misspelled word.Insert any letter at any point in the misspelled word.Change any letter in the misspelled word to any other letter.Delete any one of the letters from the misspelled word.The possible corrections that the program considers are as follows: Since the corrections are stored in a tree set, they are automatically printed out in alphabetical order with no repeats. (This is done by keeping a set of misspelled words that have been output.) If the corrections() method returns an empty set, the program outputs the message "(no suggestions)". Note that the program was written so that it will not output the same misspelled word more than once. Int: ant, dint, hint, in, ina, inc, ind, ink, inn, ins, inti, into, Getinputfilenamefromuser: (no suggestions) Pre: are, ere, ire, ore, pare, pee, per, pie, poe, pore, prep, pres, Here, for example, is part of the output from a sample program when it was run with the HTML source of this page as input: Take the return value and output any words that it contains these are the suggested correct spellings of the misspelled word. 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). That creates and returns a TreeSet containing variations on badWord that are contained in the dictionary. Static TreeSet corrections(String badWord, HashSet dictionary) Providing a List of Possible Correct SpellingsĪ 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. At this point, just print out any word that you find that is not in the dictionary. You can then go through the file, read each word (converting it to lower case) and check whether the set contains the word. This essentially makes the scanner treat any non-letter the way it would ordinarily treat a space.) (In this statement, "+" is a regular expression that matches any sequence of one or more non-letter characters. 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): Use a Scanner to read the words from the selected file. If (option != JFileChooser.APPROVE_OPTION) Int option = fileDialog.showOpenDialog(null) JFileChooser fileDialog = new JFileChooser() įtDialogTitle("Select File for Input") * without selecting a file, the return value is null. * selection dialog box. If the user cancels the dialog * Lets the user select an input file using a standard file You can either let the user type the name of the file or you can use the following method: 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. (It should be 72875.) You could also use the contains method to check for the presence of some common word in the set. To make sure that you've read all the words, check the size of the set. For the purposes of this program, convert all words to lower case before putting them in the set. Start your main program by reading the words from words.txt and storing them in a HashSet. (For the wordlist file, a token is simply a word.) Process(tk) // do something with the token (new File("/classes/s09/cs225/words.txt")) Īnd that a file can be processed, token by token, in a loop such as: You can create scanner, filein, for reading from a file with a statement such as: Since there is no need to have the words stored in order, you can use a HashSet for maximum efficiency. To make the list easy to use, you can store the words in a set. You will look up words in this list to check whether they are correctly spelled.
Dragon dogma skip silver rarify code#
The file words.txt (in the code directory) contains a list of English words, with one word on each line. You will also need to be able to traverse a set, using either an iterator or a for-each loop. set.isEmpty() - Check whether the set is empty.set.contains(item) - Check whether the set contains the item.set.add(item) - Adds the item to the set, if it is not already there.set.size() - Returns the number of items in the set.Recall that if set is a Set, then the following methods are defined: This lab, you will need to use some of the methods that are defined in the Set interface. You will use these classes to implement a spell checker. Lab 9: Sets in the Java Collection Frameworkįor this week's lab, you will use two of the classes in the Java Collection Framework: HashSet and TreeSet.
