Question

Write a spell checker class that stores a set of words, W, in a hash table...

Write a spell checker class that stores a set of words, W, in a hash table and implements a function, spellCheck(s), which performs a Spell Check on the string s with respect to the set of words, W. If s is in W, then the call to spellCheck(s) returns an iterable collection that contains only s, since it is assumed to be spelled correctly in this case. Otherwise, if s is not in W, then the call to spellCheck(s) returns a list of every word in W that could be a correct spelling of s. Your program should be able to handle all the common ways that s might be a misspelling of a word in W, including swapping adjacent characters in a word , inserting a single character inbetween two adjacent characters in a word, deleting a single character from a word, and replacing a character in a word with another character. for an extra challenge, consider phonetic substitutions as well.

I am having a hard time following this,I've seen other posts that answer this, but when I try to run it I get a lot of errors. The language is C++. Id love it to be easy to understand as in teling me this part is for the header file , or this textfile needs to be added under the source file. Thankyou so much!!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The following code uses famous algorithm to find out the matching words using distance.

Code:

#include <string>
#include <vector>
#include <numeric>
#include <algorithm>
#include <iostream>

std::size_t nearestMatchAlg( std::string word, std::string match )
{
if( match.size() < word.size() ) std::swap(word,match) ;
std::vector<std::size_t> temp( match.size()+1 ), previous(temp) ;
std::iota( previous.begin(), previous.end(), 0 ) ;

for( std::size_t i = 0 ; i < word.size() ; ++i )
{
temp[0] = i+1 ;
for( std::size_t j = 0 ; j < match.size() ; ++j )
{
temp[j+1] = std::min( std::min( temp[j], previous[j+1]) + 1,
previous[j] + ( word[i] != match[j] ) ) ;
}
temp.swap(previous);
}
return previous.back() ;
}

std::vector<std::string> nearestMatchingWords( const std::string& word, const std::vector<std::string>& dict )
{
constexpr std::size_t MAX_DIFF = 5 ;
std::vector<std::string> nearest[MAX_DIFF+1] ;
for( const std::string match : dict )
{
std::size_t dist = nearestMatchAlg( word, match ) ;
if( dist <= MAX_DIFF ) nearest[dist].push_back(match) ;
}

for( const auto& seq : nearest ) if( !seq.empty() ) return std::move(seq) ;
return {} ;
}

int main()
{
const std::vector<std::string>& dict { "alpha", "alphine", "algae", "ball", "bell", "beat", "flu", "plu" } ;
const std::string test[] = { "aloaa", "bsll", "belk"} ;
for( const std::string& word : test )
{
std::cout << word << " => " ;
for( const auto& nearest : nearestMatchingWords( word, dict ) ) std::cout << nearest << ' ' ;
std::cout << '\n' ;
}
}

Sample output:

aloaa => alpha algae
bsll => ball bell
belk => bell

Screenshots:

1 #include <string> #include <vector> 3 #include <numeric> 4 #include <algorithm» 5 #include <iostream» 7 std::size t nearestMatchAlg std::string word, std::string match) ifC match.size) < word.sizeO) std::swap(word,match); std: :vector<std: :size_t> temp match.sizeO+1), previous(temp) std::iota( previous.begin), previous.endO, 0 12 forC std::size t i-0; i < word.size); ++i ) 14 ▼ 15 16 17 18 19 20 21 temp[0] 1+1 ; for( std ::size-t j 0 ; j match . size() ; ++j ) - temp[j+1] std: :min std::minC temp[j], previous[j+1]) 1, previous[j] + ( word[i] != match [j] ) ) ; temp.swap(previous); return previous.back) 23 24 25 26 std: :vector<std::string> nearestMatchingWords( const std::string& word, const std::vector<std::string>& dict 27- 28 29 30 31 32 constexpr std::size_t MAX DIFF5 std: :vector<std::string> nearestDMAX DIFF+1]; forC const std::string match dict) std::size t distnearestMatchAlg word, match): ifC distMAX DIFF nearest[dist].push_back(match); 34 35 36 37 38 39 40 int main 41- 42 43 forC const auto& seqnearest) if Iseq.emptyO return return std: move(sea) const std::vector<std::string-& dictf alpha, alphine, algae, ball, bell, beat, flu, plu ; const std::string testO aloaa, bsll, belk; forC const std::string& word: test) 46 48 50 std::cout << word << forC const auto& nearest: nearestMatchingWords( word, dict) std: :cout << nearest << std::cout <<An; 51 O Execute Exit Focus View Result.. . compiled and executed in 1.574 sec(s) aloaa alpha algae bsll ball bell belk -» bell'

In case of any doubts, please comment.

Add a comment
Know the answer?
Add Answer to:
Write a spell checker class that stores a set of words, W, in a hash table...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN JAVA: Write a spell checker class that stores a set of words, W, in a...

    IN JAVA: Write a spell checker class that stores a set of words, W, in a hash table and implements a function, spellCheck(s), which performs a Spell Check on the string s with respect to the set of words, W. If s is in W, then the call to spellCheck(s) returns an iterable collection that contains only s, since it is assumed to be spelled correctly in this case. Otherwise, if s is not in W, then the call to...

  • Write a spell checker that stores a set of words, W, in a hash table and...

    Write a spell checker that stores a set of words, W, in a hash table and implements a function, spellCheck(s), which performs a spell check on the string s with respect to the set of words, W. If s is in W, then the call to spellCheck(s) returns an iterable collection that contains only s, because it is assumed to be spelled correctly in this case. Otherwise, if s is not in W, then the call to spellCheck(s) returns a...

  • create a hash table to implement spell checker in java 1. Create a file of 100...

    create a hash table to implement spell checker in java 1. Create a file of 100 words of varying length (max 8 characters). 2. All the words are in lower case. 3. design a hash table. 4. enter each word in the hash table. Once the hash table is created, run it as a spell checker.enter a word (interactive), find the word in the hash table. If not found enter an error message. Then prompt for next word. End the...

  • In C++ and not Java: Implement a spelling checker by using a hash table. Assume that...

    In C++ and not Java: Implement a spelling checker by using a hash table. Assume that the dictionary comes from two sources: an existing large dictionary and a second file containing a personal dictionary. Output all misspelled words and the line numbers on which they occur. Also, for each misspelled word, list any words in the dictionary that are obtainable by applying any of the following rules: 1. Add one character. 2. Remove one character. 3. Exchange adjacent characters The...

  • Overview: The goal of this assignment is to implement a simple spell checker using a hash...

    Overview: The goal of this assignment is to implement a simple spell checker using a hash table. You will be given the basic guidelines for your implementation, but other than that you are free to determine and implement the exact classes and methods that you might need. Your spell-checker will be reading from two input files. The first file is a dictionary containing one word per line. The program should read the dictionary and insert the words into a hash...

  • In this lab you will write a spell check program. The program has two input files:...

    In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the document to be spellchecked. The program will read in the words for the dictionary, then will read the document and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is or type in a replacement word and add...

  • For this week's lab, you will use two of the classes in the Java Collection Framework:...

    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 For 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...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • For this week's lab, you will use two of the classes in the Java Collection Framework:...

    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 For 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...

  • C++ please! (1) Prompt the user to enter the name of the input file. The file...

    C++ please! (1) Prompt the user to enter the name of the input file. The file will contain a text on a single line. Store the text in a string. Output the string. Ex: Enter file name: input1.txt File content: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue! (2) Implement a PrintMenu() function,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT