For this week's lab, you will use two of the classes in the Java Collection Framework:...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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 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 words.txt (in the code directory) 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. You can create scanner, filein, for reading from a file with a statement such as: filein = new Scanner (new File("/classes/s09/cs225/words.txt")); and that a file can be processed, token by token, in a loop such as: while (filein.hasNext()) { String tk = filein.next(); process (tk); // do something with the token } (For the wordlist file, a token is simply a word.) Start your main program by reading the words from words.txt and storing them in a HashSet . 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 72875.) 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: /** * 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 getInput FileNameFromUser() { JFileChooser fileDialog = new JFileChooser(); fileDialog.setDialog Title("Select File for Input"); int option = fileDialog.showOpenDialog(null); if (option != JFileChooser.APPROVE_OPTION) return null; else return fileDialog.getSelected File(); } 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.useDelimiter("[^a-zA-Z]+"); (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 method static TreeSet corrections (String badWord, HashSet dictionary) that creates and returns a TreeSet 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 a sample program when it was run with the HTML source of this page as input: 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 the same misspelled word more than once. (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)". Since the corrections are stored in a tree set, they are automatically printed out in alphabetical order with no repeats. The possible corrections that the program considers 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: String s = w.substring(0,i) + ch + w.substring(i+1); Also, you will find it convenient to use a for loop in which the loop control variable is a char: for (char ch = 'a'; ch 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 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 words.txt (in the code directory) 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. You can create scanner, filein, for reading from a file with a statement such as: filein = new Scanner (new File("/classes/s09/cs225/words.txt")); and that a file can be processed, token by token, in a loop such as: while (filein.hasNext()) { String tk = filein.next(); process (tk); // do something with the token } (For the wordlist file, a token is simply a word.) Start your main program by reading the words from words.txt and storing them in a HashSet . 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 72875.) 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: /** * 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 getInput FileNameFromUser() { JFileChooser fileDialog = new JFileChooser(); fileDialog.setDialog Title("Select File for Input"); int option = fileDialog.showOpenDialog(null); if (option != JFileChooser.APPROVE_OPTION) return null; else return fileDialog.getSelected File(); } 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.useDelimiter("[^a-zA-Z]+"); (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 method static TreeSet corrections (String badWord, HashSet dictionary) that creates and returns a TreeSet 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 a sample program when it was run with the HTML source of this page as input: 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 the same misspelled word more than once. (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)". Since the corrections are stored in a tree set, they are automatically printed out in alphabetical order with no repeats. The possible corrections that the program considers 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: String s = w.substring(0,i) + ch + w.substring(i+1); Also, you will find it convenient to use a for loop in which the loop control variable is a char: for (char ch = 'a'; ch
Expert Answer:
Answer rating: 100% (QA)
1 2 DateTimejava X DateTimeDriverjava X defining class DateTime public class DateT... View the full answer
Related Book For
Fixed Income Securities Valuation Risk and Risk Management
ISBN: 978-0470109106
1st edition
Authors: Pietro Veronesi
Posted Date:
Students also viewed these algorithms questions
-
Modify the implementation of the classes in the ATM simulation in Worked so that the bank manages a collection of bank accounts and a separate collection of customers. Allow joint accounts in which...
-
For each item below, indicate whether FIFO or LIFO will generally result in a higher reported amount when inventory costs are rising versus falling. The first answer is provided as anexample. Higher...
-
In this problem you are to compare reading a file using a single-threaded file server and a multithreaded server. It takes 12 msec to get a request for work, dispatch it, and do the rest of the...
-
In Problems 1158, perform the indicated operation, and write each expression in the standard form a + bi. -3i(7 + 6i)
-
What is the main difference between journal entries in process costing and in job costing?
-
Rocky RV Center's accounting records include the following accounts at December 31, 2018. Requirements 1. Journalize the required closing entries for Rocky. 2. Determine the ending balance in the...
-
Prepare an income statement from relevant financial information and interpret the information that it contains.
-
Ninsuvaan Corporation, a U.S. subsidiary in Bangkok, Thailand, begins and ends its calendar year with an inventory balance of BHT500 million. The dollar/baht exchange rate on January 1 was $0.02 =...
-
1 Required information Trey Monson starts a merchandising business on December 1 and enters into the following three inventory purchases. Also, on December 15, Monson sells 28 units for $50 each. nts...
-
A camera is listed for $599 less 30%, 20%, and 5%. (a) What is the net price? (b) What is the total amount of discount allowed? (c) What is the exact single rate of discount that was allowed?
-
The Taylor Company uses a process costing system. Assume that direct materials are added at the beginning of the period and that direct labor an overhead are added continuously throughout the...
-
Go to https://www.compadre.org/Physlets/electromagnetism/intro24.cfm. In the next set of activities you will see how Gauss's law can be used to find unknown charges. Open Exploration 24.1. The...
-
A boy and a girl of equal mass race up a flight of steps. If the boy takes 15 seconds to get to the top, and the girl takes 11 seconds, you would say _____________________________. -the girl does...
-
Pina Colada Corp. issues $4.30 million, 5-year, 13% bonds at 104, with interest payable on January 1. The straight-line method is used to amortize bond premium. (a) Prepare the journal entry to...
-
If Lead Arranger Credit Suisse decides to invite 4 Co-Arrangers to jointly underwrite US$550m with a final-take of US$25 m each. No praecipium is payable but Arrangement fee is increased to 40 bp...
-
Below is the questions I am having trouble understanding.I am totally confused on #1 because if the nursery sells to the retail division by a transfer cost it will still be sold to outside customers....
-
Confirm the below calculations. Compare how the below calculated WACC is different than 4.36% . Explain how the different WACCs impact the NPV for the Electro-bicycle project. Calculations for...
-
Give the products of the following reaction, where T is tritium: dioldehydrase Ad- CH CH3C-COH CoIII) coenzyme B12
-
On September 25, 2008 a client asks you the price of a call option with maturity November 15, 2010, written on the STRIP maturing on November 15, 2015, and with strike price 82. (a) What is the price...
-
Mortgage backed securities. In this exercise we look at some features of the optimal time of exercise the prepayment option embedded in mortgage pools. To keep things simple, we consider a short...
-
Consider again Exercise 4. Imagine that back on October 16, 2007, the firm decided to sell the equipment for $98.78 million receiving the cash in 6 months. The CFO looks at the 3-month LIBOR at the...
-
An equilibrium mixture of ethylene and propylene is at \(2500 \mathrm{kPa}\) and \(25.0^{\circ} \mathrm{C}\). Find the vapor and liquid mole fractions of ethylene. Use DePriester charts or Eq....
-
We have a mixture that is \(35.0 \mathrm{~mol} \% \mathrm{n}\)-butane with unknown amounts of propane and n-hexane. We are able to operate a flash drum at \(400.0 \mathrm{kPa}\) and \(70.0^{\circ}...
-
Find the diameter and length of a horizontal flash drum for Problem 2.D1c if a hold-up time of 1.0 hours is specified. Problem 2.D1c c. If the feed is \(30.0 \mathrm{~mol} \%\) methanol and we desire...
Study smarter with the SolutionInn App