Read the Slogan. Marietta Marshmallows Melt in Your Mouth
Lab nine: Sets in the Java Collection Framework For this week's lab,...
Lab 9: Sets in the Java Collection Framework
For this week's lab, you volition utilize two of the classes in the Java Collection Framework:HashSet andTreeSet. You lot will utilize these classes to implement a spell checker.
Set Methods
this lab, you will need to utilise some of the methods that are defined in the Set interface. Recall that if set is a Set up, then the following methods are defined:
- set.size() -- Returns the number of items in the set.
- fix.add(detail) -- Adds the item to the prepare, if information technology is non already there.
- set.contains(item) -- Check whether the prepare contains the detail.
- ready.isEmpty() -- Check whether the set is empty.
Yous will also demand to exist able to traverse a set, using either an iterator or a for-each loop.
Reading a Lexicon
The file words.txt (in the code directory) contains a list of English words, with one word on each line. You lot volition wait up words in this listing to check whether they are correctly spelled. To make the listing easy to use, you can store the words in a prepare. Since in that location is no need to take the words stored in club, you tin can employ aHashSet for maximum efficiency.
Use a Scanner to read the file. You tin 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 candy, token past token, in a loop such as:
while (filein.hasNext()) {
Cord tk = filein.adjacent();
process(tk); // practise something with the token
}
(For the wordlist file, a token is simply a discussion.)
Start your main program by reading the words from words.txt and storing them in aHashSet<String>. For the purposes of this program,convert all words to lower case earlier putting them in the ready. To make sure that you lot've read all the words, check the size of the set. (It should be 72875.) You could also apply the contains method to check for the presence of some mutual word in the set.
Checking the Words in a File
Once y'all take the listing of words in a prepare, it'due south piece of cake to read the words from a file and bank check whether each word is in the set. Start past letting the user select a file. You tin either allow the user blazon the name of the file or yous tin can utilise 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 null.
*/
static File getInputFileNameFromUser() {
JFileChooser fileDialog = new JFileChooser();
fileDialog.setDialogTitle("Select File for Input");
int option = fileDialog.showOpenDialog(goose egg);
if (option != JFileChooser.APPROVE_OPTION)
return naught;
else
return fileDialog.getSelectedFile();
}
Use a Scanner to read the words from the selected file. In order to skip over any non-letter characters in the file, y'all tin can apply the post-obit command just after creating the scanner (where in is the variable proper name for the scanner):
in.useDelimiter("[^a-zA-Z]+");
(In this argument, "[^a-zA-Z]+" is a regular expression that matches any sequence of one or more not-letter characters. This substantially makes the scanner treat any not-letter of the alphabet the way it would commonly treat a infinite.)
Y'all tin and then go through the file, read each give-and-take (converting it to lower case) and check whether the set contains the word. At this bespeak, just print out any give-and-take 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 besides give you a list of possible correct spellings for that give-and-take. Write method
static TreeSet corrections(String badWord, HashSet dictionary)
that creates and returns aTreeSet<String> containing variations onbadWord 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 render 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 programme when it was run with the HTML source of this page every bit input:
html: (no suggestions)
cpsc: (no suggestions)
hashset: hash prepare
treeset: tree ready
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 adjacent
wordlist: word list
getinputfilenamefromuser: (no suggestions)
jfilechooser: (no suggestions)
filedialog: file dialog
setdialogtitle: (no suggestions)
int: emmet, dint, hint, in, ina, inc, ind, ink, inn, ins, inti, into,
it, lint, mint, nit, pint, tint
Notation that the program was written so that it volition not output the aforementioned misspelled word more than one time. (This is done by keeping a set of misspelled words that have been output.) If thecorrections() 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 guild with no repeats.
The possible corrections that the program considers are as follows:
• Delete any ane of the letters from the misspelled word.
• Modify any alphabetic character in the misspelled give-and-take to any other letter.
• Insert any letter at whatsoever signal in the misspelled give-and-take.
• Swap whatsoever two neighboring characters in the misspelled word.
• Insert a space at any indicate in the misspelled discussion (and check that both of the words that are produced are in the dictionary)
For constructing the possible corrections, yous will have to brand extensive apply of substrings. If w is a string, thenwestward.substring(0,i) is the string consisting of the first i characters in w (not including the character in position i, which would be graphic symbol numberi+1). Anddue west.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 tin change thei-th character of west to ch with the statement:
String south = due west.substring(0,i) + ch + w.substring(i+1);
Also, you lot volition find 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 expert
entesq
consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentes
ce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum do
rem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar torto
,ur laoreet. Nam risus ante, dapibus a molestie consten
Unlock full access to Grade Hero
Explore over 16 million step-by-pace answers from our library
Subscribe to view respond
Step-by-stride explanation
a. Fusce dui lec
dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortorconloneclac, dictumrisus sumiametiur licetone thousand ipsum dolo,dictum vitae odio. Donec aliquet. Lorem ipsum dolorsua molestie consequat, ultrices ac magna. Fusce dui lectufng elit. Nia pulvinar tortor nesuac, dictusuec facconsectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque daconsectetur athousand ipslestie consequat, ultrices air-conditioning magna. Fusce dui lectus, congue vel laoreet ac, dictum vitatrimolestie consequat, ultrices ac magna. Fusce dui lectus,sutrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitaitur laoreeusce dui lectus, congue vel laoreet ac, dsuinia pulvinar tortor nec facm 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 air-conditioning, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit down amet,tring elit. Nam lacinia pulvinar tortor nec facilisis. Pellsucongue vel laoreet air conditioning, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consecteturac,lestie consequat, ucea molestie consequctum vitae odio. Donec aliquet. Lorem ipsum dair-conditioning,dictum vimolestie consequa molestie conseqonecfacilisis. Pellentesque dapibusiaDonec al0s ante, dapibusicitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices air-conditioning magazineair-conditioning,ur laoreet. Nam risus ante, dapibus auac, dictum vitae odio. Donec afinia pulvinar tortor nec facac,ipsum dentm ipsum dolor sit ametonecs a molestie consequat, ultrair conditioning,ipsum dusceFusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit apulv, consectetur adipiscing elit. Nam lacinia pulvinar tortair-conditioning,dictum half dozenur laoreet. Nam risus dues, dapibus a molestie consec aliquet. Lorem ipsum dolor0congue vel fifty ametipsumec aliquet. Lorem ipsumuscfacilisis. Pellentesque dapibus eastsuet, consectetur adipiscing elit. Nam lacinia pulinia pulvinar tortor nem ipsumiaor nec facilisis. Pellentesque dapibametone thousand ipsumum risus dues, dapibus a molestie consequat, ultrior nefiftyametinia ponectesque dapor neamet, consectetur adipiscing elit. Nam ametongue velipsum dolorto. Donec aliquet. Lorem ipsum dolo,sum dolor sit amet, consectetur adipiscing elsuitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices air conditioning 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 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 air conditioning magna.usus ante, dapibus a molestie consequat, ultrices ac magna. Fusce duitris dues, dapibus a molestie consequat, ultrices air conditioning magna. Fusce dui 500air conditioning, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit down aicDair-conditioning,onguecefficituriafficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices air-conditioning magna.uac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur atriusce dui lectus, congue vel laoreet air conditioning, dictum vitae odio. Doneac,ongueceac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit ametufficitur laoreet. Nam risus ante, 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 adipiscifs a molestieair-conditioning,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 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 "Read the Slogan. Marietta Marshmallows Melt in Your Mouth"
Post a Comment