Great Soups a Thursday With Hotels and Blammer Wong Cornish College of the Arts
Lab 9: Sets in the Coffee Collection Framework For this week'southward lab,...
Lab 9: Sets in the Java Collection Framework
For this week'due south lab, you will use two of the classes in the Java Drove Framework:HashSet andTreeSet. You will employ these classes to implement a spell checker.
Set Methods
this lab, you lot volition demand to use some of the methods that are divers in the Set interface. Retrieve that if set is a Set, then the following methods are divers:
- fix.size() -- Returns the number of items in the ready.
- set.add(detail) -- Adds the item to the set, if it is non already there.
- set.contains(item) -- Check whether the prepare contains the item.
- gear up.isEmpty() -- Check whether the set is empty.
You will likewise 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 give-and-take on each line. Yous volition look upwardly words in this listing to cheque whether they are correctly spelled. To make the listing easy to use, you can shop the words in a set. Since at that place is no need to have the words stored in order, you can utilize aHashSet for maximum efficiency.
Employ a Scanner to read the file. You lot can create scanner,filein, for reading from a file with a statement such every bit:
filein = new Scanner
(new File("/classes/s09/cs225/words.txt"));
and that a file can be processed, token past token, in a loop such equally:
while (filein.hasNext()) {
Cord tk = filein.adjacent();
procedure(tk); // exercise something with the token
}
(For the wordlist file, a token is simply a word.)
Start your master plan by reading the words from words.txt and storing them in aHashSet<Cord>. For the purposes of this program,convert all words to lower case before putting them in the fix. To make certain that you've read all the words, check the size of the set. (It should be 72875.) You lot could also use the contains method to check for the presence of some mutual word in the prepare.
Checking the Words in a File
Once y'all accept the list of words in a set, it's like shooting fish in a barrel to read the words from a file and cheque whether each word is in the fix. Start by letting the user select a file. Yous tin can either let the user type the name of the file or you tin can use the following method:
/**
* Lets the user select an input file using a standard file
* pick dialog box. If the user cancels the dialog
* without selecting a file, the render value is nada.
*/
static File getInputFileNameFromUser() {
JFileChooser fileDialog = new JFileChooser();
fileDialog.setDialogTitle("Select File for Input");
int option = fileDialog.showOpenDialog(zip);
if (option != JFileChooser.APPROVE_OPTION)
return zero;
else
return fileDialog.getSelectedFile();
}
Apply a Scanner to read the words from the selected file. In club to skip over any non-letter characters in the file, you tin can use the following command only after creating the scanner (where in is the variable name for the scanner):
in.useDelimiter("[^a-zA-Z]+");
(In this argument, "[^a-zA-Z]+" is a regular expression that matches any sequence of 1 or more than non-letter characters. This essentially makes the scanner care for any non-letter the way it would normally treat a space.)
Yous tin then become through the file, read each word (converting information technology to lower case) and check whether the set contains the word. At this point, just impress out whatever word that you find that is not in the dictionary.
Providing a Listing of Possible Correct Spellings
A spell checker shouldn't just tell you what words are misspelled -- it should likewise give y'all a list of possible correct spellings for that word. Write method
static TreeSet corrections(String badWord, HashSet lexicon)
that creates and returns aTreeSet<String> containing variations onbadWord that are contained in the dictionary. In your main program, when you observe a discussion 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 role 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, skin, pee, per, pie, poe, pore, prep, pres,
prey, pro, pry, pure, pyre, re
hasnext: has side by side
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 plan was written so that it will not output the aforementioned misspelled word more than than in one case. (This is done past keeping a gear up of misspelled words that take been output.) If thecorrections() method returns an empty ready, the programme 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 plan considers are equally follows:
• Delete any one of the letters from the misspelled give-and-take.
• Modify any alphabetic character in the misspelled word to any other letter.
• Insert whatever alphabetic character at any point in the misspelled word.
• Swap whatsoever 2 neighboring characters in the misspelled word.
• Insert a infinite at any point in the misspelled word (and cheque that both of the words that are produced are in the lexicon)
For constructing the possible corrections, you will have to make extensive utilize of substrings. If w is a string, thenwestward.substring(0,i) is the string consisting of the commencement i characters in w (not including the character in position i, which would be character numberi+ane). Andw.substring(i) consists of the characters of westward from position i through the end of the string. For example, ifch is a character, then you can change thei-thursday graphic symbol of w to ch with the statement:
String s = w.substring(0,i) + ch + due west.substring(i+1);
Also, you volition notice it convenient to use afor loop in which the loop control variable is achar:
for (char ch = 'a'; ch <= 'z'; ch++) { ...
Answer & Explanation
Solved by verified adept
entesq
consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentes
ce dui lectus, congue vel laoreet air-conditioning, dictum vitae odio. Donec aliquet. Lorem ipsum do
rem ipsum dolor sit down amet, consectetur adipiscing elit. Nam lacinia pulvinar torto
,ur laoreet. Nam risus ante, dapibus a molestie consx
Unlock full access to Grade Hero
Explore over xvi million step-by-step answers from our library
Subscribe to view answer
Step-by-stride caption
a. Fusce dui lec
dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortorconloneclac, dictumrisus sumiametiur fiftyicetm ipsum dolo,dictum vitae odio. Donec aliquet. Lorem ipsum dolorsua molestie consequat, ultrices air-conditioning magna. Fusce dui lectufng elit. Nia pulvinar tortor nesuair conditioning, dictusuec facconsectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque daconsectetur achiliad ipslestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitatrimolestie consequat, ultrices ac magna. Fusce dui lectus,sutrices air conditioning magna. Fusce dui lectus, congue vel laoreet air-conditioning, dictum vitaitur laoreeusce dui lectus, congue vel laoreet ac, dsuinia pulvinar tortor nec facyard ipsat, ultrices ac magna. Fusce dui lectus, congue vel laoreet air conditioning, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetuuFusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet,tring elit. Nam lacinia pulvinar tortor nec facilisis. Pellsucongue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit down amet, consecteturac,lestie consequat, ucea molestie consequctum vitae odio. Donec aliquet. Lorem ipsum dac,dictum 6molestie consequa molestie conseqonecfacilisis. Pellentesque dapibusiaDonec al0due south ante, dapibusicitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magac,ur laoreet. Nam risus ante, dapibus auac, dictum vitae odio. Donec afinia pulvinar tortor nec facac,ipsum dentm ipsum dolor sit ametonecsouth a molestie consequat, ultrac,ipsum dusceFusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit apulv, consectetur adipiscing elit. Nam lacinia pulvinar tortac,dictum viur laoreet. Nam risus ante, dapibus a molestie consec aliquet. Lorem ipsum dolor0congue vel l ametipsumec aliquet. Lorem ipsumuscfacilisis. Pellentesque dapibus esuet, consectetur adipiscing elit. Nam lacinia pulinia pulvinar tortor nem ipsumiaor nec facilisis. Pellentesque dapibamet1000 ipsumugrand risus dues, dapibus a molestie consequat, ultrior nelametinia ponectesque dapor neamet, consectetur adipiscing elit. Nam ametongue velipsum dolorto. Donec aliquet. Lorem ipsum dolo,sum dolor sit down amet, consectetur adipiscing elsuitur laoreet. Nam risus dues, dapibus a molestie consequat, ultrices ac magnicDiallen0entesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie conse0cing elit. Nam lacinia pulvinar tortor nec facilisfctum vitae odio. Donec aliquet. Lorem ipsum dolor sit down amet, cuitur laoreet. Nam risus dues, dapibus a molestie consequat, ultrtrictum vitae odio. Donec aliicDair conditioning,onguecefficituriafficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna.usus ante, dapibus a molestie consequat, ultrices air conditioning magna. Fusce duitris dues, dapibus a molestie consequat, ultrices ac magna. Fusce dui l0air-conditioning, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit down aicDac,onguecefficituriafficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna.uair-conditioning, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur atriusce dui lectus, congue vel laoreet ac, dictum vitae odio. Washedac,ongueceac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit ametufficitur laoreet. Nam risus dues, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, conguetrinec facilisis. Pellentesque dapibus efficitur laoreeticDiallenfentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie conse0rem ipfsouth a molestieac,amet, consectetur adipiscifsouth a molestieac,fficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultricf, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consecteuitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrtring elit. Nam lacinia pulvinaor neo. Donec aliquet. Lorem ipsu
, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit down amet, consectetur adipiscing elit. Nam lacin
sum do
Source: https://www.coursehero.com/tutors-problems/Java-Programming/21112512-Lab-9-Sets-in-the-Java-Collection-Framework-For-this-weeks-lab-you/
0 Response to "Great Soups a Thursday With Hotels and Blammer Wong Cornish College of the Arts"
Publicar un comentario