Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

3 Censored Copy ( 6 0 points ) Write a function named ReplacementCensor in a censor implementation and header file , that takes 3 function

3 Censored Copy (60 points)
Write a function named ReplacementCensor in a censor implementation and header file , that
takes 3 function arguments. The first argument is an istream of text to be processed. The
second argument is an ostream to where the processed text should be copied to. The last argument
is a map of strings to strings, where the key is a string that needs to be replaced with the
value in the text of the first argument's contents. This function should return a set containing
the words that were replaced. The keys should be matched in a case insensitive manner, and
the text to replace may not be white space delimited, meaning they can appear in longer words.
Example istream (First argument):
note: this is a line with multiple WORds that should be rePLACEd.
all instances of word eveninlargerWordsshould be repLAced.
Example map (Third argument):
{"word", "Grouped-Letter-Unit"},
{"be", "wasp"},
{"not found", "not appearing"},
{"PlaCe", "Location"}
Expected ostream (Second argument) after being processed:
note: this is a line with multiple Grouped-Letter-Units that should wasp
reLoCationd.
all instances of Grouped-Letter-Unit eveninlargerGrouped-Letter-Unitsshould wasp
reLoCationd.
Expected set returned by ReplacementCensor:
{"PLACE", "WORd", "Word", "be", "pLAce", "word"}
main.cpp
#include "censor.hpp"
#include
#include
#include
#include
#include
#include
int main(){
std::map bad_to_good {
{ "word" , "Grouped-Letter-Unit" },
{"be", "wasp" },
{ "not found" , "not appearing" },
{ "PlaCe" , "Location" }
};
std::istringstream iss("note: this is a line with multiple WORds that should be rePLACEd./n all instances of word eveninlargerWordsshould be repLAced.");
std::ostringstream oss;
std::set result = ReplacementCensor(iss, oss, bad_to_good);
std::set expected_return ={ "PLACE", "WORd", "Word", "be", "pLAce", "word" };
assert(result == expected_return);
assert(oss.str()== "note: this is a line with multiple Grouped-Letter-Units that should wasp reLoCationd./n all instances of Grouped-Letter-Unit eveninlargerGrouped-Letter-Unitsshould wasp reLoCationd.");
std::cout "All tests passed!" std::endl;
return 0;
}
censor.hpp
#pragma once
#include
#include
#include
#include
#include
std::set ReplacementCensor(std::istream &input, std::ostream &output, const std::map &replacements);
censor.cpp
#include "censor.hpp"
#include
#include
#include
#include
#include
// Convert a string to lowercase
std::string toLowerCase(const std::string &str){
std::string lowerStr = str;
std::transform(lowerStr.begin(), lowerStr.end(), lowerStr.begin(), ::tolower);
return lowerStr;
}
std::set ReplacementCensor(std::istream &input, std::ostream &output, const std::map &replacements){
std::set replacedWords;
std::string inputText((std::istreambuf_iterator(input)), std::istreambuf_iterator());
std::string lowerText = toLowerCase(inputText);
std::unordered_map lowerReplacements;
for (const auto &pair : replacements){
lowerReplacements[toLowerCase(pair.first)]= pair.second;
}
size_t pos =0;
while (pos lowerText.size()){
bool replaced = false;
for (const auto &pair : lowerReplacements){
const std::string &lowerFrom = pair.first;
const std::string &to = pair.second;
if (lowerText.substr(pos, lowerFrom.length())== lowerFrom){
// Replace in original text
inputText.replace(pos, lowerFrom.length(), to);
// Replace in lowerText for further comparisons
lowerText.replace(pos, lowerFrom.length(), toLowerCase(to));
// Add original case-sensitive key to replacedWords
replacedWords.insert(pair.first);
pos += to.length();
replaced = true;
break;
}
}
if (!replaced){
++pos;
}
}
output inputText;
return replacedWords;
}
I am getting this error: Assertion failed: result == expected_return, file .\main.cpp, line 23
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security

Authors: Alfred Basta, Melissa Zgola

1st Edition

1435453905, 978-1435453906

More Books

Students also viewed these Databases questions

Question

4-7. What are the purposes of strategic planning?

Answered: 1 week ago

Question

=+3. Explain the interactions in the TV market!

Answered: 1 week ago

Question

=+1. Of what is the value chain in the music industry composed?

Answered: 1 week ago

Question

=+2. Explain the manufacturing model of radio management!

Answered: 1 week ago