Question

C++ program: can you help create a autocorrect code using the cpp code provided and the...

C++ program: can you help create a autocorrect code using the cpp code provided and the words below using pairs, vectors and unordered map:

Objectives

  • To practice using C++ std::pair, std::vector, and std::unordered_map
  • To tie together what we've learned into the context of a real-world application used by millions of people every day

Instructions

For Full Credit

You're given a short list of words in known_words_short.txt that contains a handful of very different words. Assume this short list of words are all words that exist, so any word that's not in the list is assumed to be misspelled. If you were given a word that is misspelled, the program should output as such. If the misspelled word is close to one of the known words, it should also suggest that word.

For example, if the user types something completely different from any of the known words, such as "asdf", your program should output something like

> "asdf" is misspelled.

However, if "google" is one of the known words, and the user types "goggle", your program should output something like

> "goggle" is misspelled. Did you mean "google"?

Assume the misspelling is wrong in at most 1 of the characters, so words that are missing letters or have extra letters don't need to be considered. Assume the wrong letter can show up anywhere in the word.

known_word_short.txt:

apple
bitcoin
computer
data
efficient
file
google
hello
input
java
kernel
latency
memory
net
operator
print
query
return
stack
type
unsigned
vector
write
xerox
yahoo
zip

CPP code to use:

#include <fstream>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
vector<string> GetKnownWords(string file_name) {
vector<string> known_words;
ifstream words_file(file_name);
for (string word; getline(words_file, word);) {
known_words.push_back(word);
}
return known_words;
}
int main(int argc, char* argv[]) {
vector<string> known_words = GetKnownWords("known_words_short.txt");
// TODO: Repeatedly prompt the user for words, and see what the spelling suggestions are.
// You may use an infinite loop exit the program with ctrl + c, or prompt the user whether
// they would like to enter more words after each one.
return 0;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

All the explanations is in the code comments. Hope this helps.

Code:

#include <fstream>
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

vector<string> GetKnownWords(string file_name) {
  
vector<string> known_words;
ifstream words_file(file_name);
string word;
  
// read all the words in a loop
while(getline(words_file, word)) {
known_words.push_back(word);
}
  
// return vector of words
return known_words;
}

int main(int argc, char* argv[]) {
  
// call GetKnownWords() to get vetor of words
vector<string> known_words = GetKnownWords("known_words_short.txt");
  
// create an unordered_map for each word
unordered_map<string, int> map;
for(int i=0; i<known_words.size(); i++) {
map.insert(make_pair(known_words[i], 1));
}
  
// to store correct spellings corresponding to misspellings
// update it whenever a misspelled word is found
vector<pair<string, string>> misspellings;
  
string word;
// Use an infinite loop and exit the program with ctrl + c
while(true) {
  
cout << "Enter a word: ";
cin >> word;
  
// cout << map.find(word) << endl;
if(map.find(word) == map.end())
{
int flag = 0;
cout << "\"" << word << "\" is misspelled.";
  
// first check in the misspellings vector
for(int i=0; i<misspellings.size(); i++)
{
if(misspellings[i].first == word)
{
flag = 1;
cout << " Did you mean \"" << misspellings[i].second << "\"?\n";
break;
}
}
  
if(flag != 1)
{
// now search for similar words in known_words
for(int i=0; i<known_words.size(); i++)
{
// skip if length does not match
if(word.size() != known_words[i].size())
continue;
  
int count = 0;
for(int j=0; known_words[i][j]!='\0'; j++)
{
// skip if characters match
if(word[j] == known_words[i][j])
continue;
  
// increase count of different character by 1
count++;
  
// if different characters are more than 1
// then word is not similar
if(count > 1)
break;
}
// if only one character is different
if(count == 1)
{
// add it to the misspellings vector
misspellings.push_back(make_pair(word, known_words[i]));
  
// also print the output
cout << " Did you mean \"" << known_words[i] << "\"?";
break;
}
}
cout << endl;
}
}
else // correctly spelled words are found in map
{
cout << "\"" << word << "\" is spelled correctly" << endl;
}
}
  
return 0;
}

Sample run:

Code screenshots:

Add a comment
Know the answer?
Add Answer to:
C++ program: can you help create a autocorrect code using the cpp code provided and the...
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
  • Create a new program, WordGuessingGame. Using a while loop, create a game for the user. The...

    Create a new program, WordGuessingGame. Using a while loop, create a game for the user. The game requires the user to guess a secret word. The game does not end until the user guesses the word. After they win the game, they are prompted to choose to play again. The secret word that user must guess is "valentine". It is a case-sensitive analysis With each guess... If the guess does not have an equal number of characters, tell the user...

  • C++ SECTION 1) (15 points) Write a full C++ program that reads an integer, a list...

    C++ SECTION 1) (15 points) Write a full C++ program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character. Ex: If the input is: 4 hello zoo sleep drizzle z then the output is: 200 drizzle To...

  • Write this program in c++:

    test.txtLab7.pdfHeres the main.cpp:#include "Widget.h"#include <vector>#include <iostream>#include <string>#include <iomanip>#include <fstream>using std::ifstream;using std::cout;using std::cin;using std::endl;using std::vector;using std::string;using std::setprecision;using std::setw;bool getWidget(ifstream& is, Widget& widget){ string name; int count; float unitCost; if (is.eof())  { return false; } is >> count; is >> unitCost; if (is.fail())  { return false; } is.ignore(); if (!getline(is, name))  { return false; } widget.setName(name); widget.setCount(count); widget.setUnitCost(unitCost); return true;}// place the definitions for other functions here// definition for function mainint main(){ // Declare the variables for main here  // Prompt the...

  • Write a program IN PYTHON that checks the spelling of all words in a file. It...

    Write a program IN PYTHON that checks the spelling of all words in a file. It should read each word of a file and check whether it is contained in a word list. A word list available below, called words.txt. The program should print out all words that it cannot find in the word list. Requirements Your program should implement the follow functions: main() The main function should prompt the user for a path to the dictionary file and a...

  • I need my c++ code converted to MASM (assembly language). The instructions below: write an assembly...

    I need my c++ code converted to MASM (assembly language). The instructions below: write an assembly program that does the following; 1. count and display the number of words in the user input string. 2. Flip the case of each character from upper to lower or lower to upper. For example if the user types in:   "Hello thEre. How aRe yOu?" Your output should be: The number of words in the input string is: 5 The output string is : hELLO...

  • I need help writing this code in C++ Proj11.cpp is provided as well as the randomdata.txt thank you in advance! Objectives: The main objectives of this project is to introduce you to recursion,...

    I need help writing this code in C++ Proj11.cpp is provided as well as the randomdata.txt thank you in advance! Objectives: The main objectives of this project is to introduce you to recursion, and test your ability to work with some STL functionalities. A review of your knowledge on working with templates, dynamic data structures, as well as manipulating dynamic memory, classes, pointers and iostream to all extents, is also included. Description: For the entirety of this project, you will...

  • solve using the code provided Write a program that will continuously read words from the user,...

    solve using the code provided Write a program that will continuously read words from the user, word by word till the user enter "quit”, the program should print each word in reverse order. Sample Session: (User input in Red color) Enter a word: hello olleh Enter a word: Ahmad damhA Enter a word: quit Good Bye 9 dinclude <iostream> 10 dinclude <cstring> 11 using namespace std; 12 13 int main() { 14 15 char word(100); 16 cin>>word; 17 while (strcmp(word,...

  • Assignment 1 In this assignment you will be writing a tool to help you play the...

    Assignment 1 In this assignment you will be writing a tool to help you play the word puzzle game AlphaBear. In the game, certain letters must be used at each round or else they will turn into rocks! Therefore, we want to create a tool that you can provide with a list of letters you MUST use and a list of available letters and the program returns a list of all the words that contain required letters and only contain...

  • The program needs to be written in C. Write a function void camelCase(char* word) where word...

    The program needs to be written in C. Write a function void camelCase(char* word) where word consists of more than two words separated by underscore such as “random_word” or "hello_world_my_name_is_sam". camelCase() should remove underscores from the sentence and rewrite in lower camel case” (https:// en.wikipedia.org/wiki/Camel_case). Watch out for the end of the string, which is denoted by ‘\0’. You have to ensure that legal strings are given to the camelCase() function. The program should only run when the input is...

  • Can you help me to create this program, please? Prompt the user for the user for...

    Can you help me to create this program, please? Prompt the user for the user for a number of degrees in Fahrenheit as an int. That number should be passed to a function named ffocREFO. This function will do the necessary calculation to convert the passed temperature to degrees Celsius as a double. The function MUST have a void return type and have the following prototype: void ffccREF(double& celsius, int Faren); In your main, display both the entered temperature and...

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