The Book of Revelations Antichrist Smote in the Eye and Rises Again
Lab ix: Sets in the Java Collection Framework For this week's lab,...
Lab 9: Sets in the Java Collection Framework
For this week'south lab, you will use 2 of the classes in the Java Collection Framework:HashSet andTreeSet. Y'all will use these classes to implement a spell checker.
Set up Methods
this lab, you will need to apply 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 detail to the prepare, if it is not already there.
- set.contains(particular) -- Check whether the fix contains the item.
- set.isEmpty() -- Check whether the fix is empty.
You will likewise need to exist able to traverse a set up, 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 volition look up words in this list to check whether they are correctly spelled. To brand the list easy to use, you tin can store the words in a ready. Since at that place is no need to have the words stored in lodge, you lot can use 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 as:
filein = new Scanner
(new File("/classes/s09/cs225/words.txt"));
and that a file tin can be candy, token by token, in a loop such equally:
while (filein.hasNext()) {
String tk = filein.next();
process(tk); // practise something with the token
}
(For the wordlist file, a token is simply a discussion.)
Kickoff your main program by reading the words from words.txt and storing them in aHashSet<String>. For the purposes of this plan,convert all words to lower example before putting them in the set. To make certain that you've read all the words, check the size of the set. (It should exist 72875.) You could likewise apply the contains method to check for the presence of some common word in the ready.
Checking the Words in a File
Once you lot 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. Starting time past letting the user select a file. Y'all can either let the user type the name of the file or you can utilize the post-obit 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 goose egg.
*/
static File getInputFileNameFromUser() {
JFileChooser fileDialog = new JFileChooser();
fileDialog.setDialogTitle("Select File for Input");
int pick = fileDialog.showOpenDialog(nix);
if (option != JFileChooser.APPROVE_OPTION)
render null;
else
render fileDialog.getSelectedFile();
}
Employ a Scanner to read the words from the selected file. In order to skip over whatever non-letter characters in the file, you can use the post-obit control but 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 care for any non-letter the style it would ordinarily care for a space.)
Y'all tin can then go through the file, read each word (converting it to lower instance) and check whether the set contains the give-and-take. At this point, but print out any give-and-take that y'all find that is not in the dictionary.
Providing a List of Possible Right Spellings
A spell checker shouldn't just tell you what words are misspelled -- it should also give y'all a list of possible right spellings for that word. 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 plan, when you notice a word that is not in the set of legal words, pass that word to this method (along with the set up). Accept the return value and output any words that it contains; these are the suggested right spellings of the misspelled give-and-take. Hither, for example, is part of the output from a sample program when it was run with the HTML source of this folio as input:
html: (no suggestions)
cpsc: (no suggestions)
hashset: hash gear up
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,
casualty, 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,
information technology, lint, mint, nit, pint, tint
Note that the program was written so that it will not output the same misspelled word more than than in one case. (This is done past keeping a gear up 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 fix, they are automatically printed out in alphabetical order with no repeats.
The possible corrections that the program considers are as follows:
• Delete whatever ane of the letters from the misspelled word.
• Change whatever letter in the misspelled word to whatever other letter of the alphabet.
• Insert any letter of the alphabet at any bespeak in the misspelled give-and-take.
• Bandy any two neighboring characters in the misspelled word.
• Insert a space at whatever bespeak in the misspelled give-and-take (and check that both of the words that are produced are in the dictionary)
For constructing the possible corrections, you lot will have to make all-encompassing use of substrings. If westward is a string, thenwest.substring(0,i) is the string consisting of the outset i characters in w (non including the character in position i, which would be character numberi+1). Andw.substring(i) consists of the characters of w from position i through the end of the string. For case, ifch is a grapheme, then you can change thei-th graphic symbol of w to ch with the argument:
String s = west.substring(0,i) + ch + w.substring(i+1);
Also, you will find it convenient to apply afor loop in which the loop command 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 down amet, consectetur adipiscing elit. Nam lacinia pulvinar torto
,ur laoreet. Nam risus dues, dapibus a molestie consten
Unlock full access to Course Hero
Explore over 16 million step-by-footstep answers from our library
Subscribe to view answer
Step-by-pace explanation
a. Fusce dui lec
dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortorconloneclac, dictumrisus sumiametiur licetm ipsum dolo,dictum vitae odio. Donec aliquet. Lorem ipsum dolorsua molestie consequat, ultrices air conditioning magna. Fusce dui lectufng elit. Nia pulvinar tortor nesuac, dictusuec facconsectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque daconsectetur am ipslestie consequat, ultrices air-conditioning 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 air conditioning, 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 down amet, consectetuuFusce dui lectus, congue vel laoreet air conditioning, 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 amet, consecteturac,lestie consequat, ucea molestie consequctum vitae odio. Donec aliquet. Lorem ipsum dac,dictum vimolestie consequa molestie conseqonecfacilisis. Pellentesque dapibusiaDonec al0s ante, dapibusicitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magair conditioning,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 down apulv, consectetur adipiscing elit. Nam lacinia pulvinar tortac,dictum sixur laoreet. Nam risus dues, dapibus a molestie consec aliquet. Lorem ipsum dolor0congue vel l ametipsumec aliquet. Lorem ipsumuscfacilisis. Pellentesque dapibus due eastsuet, consectetur adipiscing elit. Nam lacinia pulinia pulvinar tortor negrand ipsumiaor nec facilisis. Pellentesque dapibametgrand ipsumuk risus ante, 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 ante, 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 amet, cuitur laoreet. Nam risus ante, 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 ac magna. Fusce duitrisouth dues, dapibus a molestie consequat, ultrices air conditioning magna. Fusce dui l0ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit aicDair-conditioning,onguecefficituriafficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices air-conditioning magna.uair-conditioning, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit down amet, consectetur atriusce dui lectus, congue vel laoreet air-conditioning, dictum vitae odio. Washedair-conditioning,ongueceair-conditioning, 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 ipfdue south a molestieair-conditioning,amet, consectetur adipiscifs a molestieac,fficitur laoreet. Nam risus dues, dapibus a molestie consequat, ultricf, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consecteuitur laoreet. Nam risus dues, 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 "The Book of Revelations Antichrist Smote in the Eye and Rises Again"
Post a Comment