Replace words in string
I'm looking to write a program that will take a user input (a sentence) and make it less sexist.
For ex., User input is -> He who laughs last, laughs best.
Program output -> She or he who laughs last, laughs best.
"A student can get a discount if he shows his ID."
should be replaced by
"A student can get a discount if he or she shows his or her ID."
I'm a little lost on how to scan the string to pick up the pronouns and replace them. I don't expect someone to write this for me, but am looking for a push in the right direction. Any info, source code examples, links, or hints would be most appreciated.
Thanks in advance.
You can use the string::replace overloads to do this.
Is this just a classroom problem, or are you planning to use this in the real world? In the latter case, consider that it's cumbersome to use the "his or her" version everywhere. Usually people 1) use something like s/he, 2) pick "he" or "she" randomly to keep it balanced overall, or 3) just accept that the English language itself is sexist, and maybe consider pushing to get it changed. I think 2) is probably the most popular. However, this wouldn't be practical to implement in code since you would have to consistently pick the male or female version in each situation referred to in the text, and you would need AI for this.
This is a classroom problem, and I'd also like to try it out on a couple of word files to see its practical use. I'm pretty sure that I could figure out how to read in from file if I can get the scan and recplace part working.
Another problem I thought of, which I think is insurmountable, is what if the "he" or "she" in the text really does specifically refer to one gender? In that case you can't change it, and you would need AI again to know if this was the case. Unless your text was known in advance to be gender-neutral.
That's true for practical use. When it's graded, I assume that the instructor will input a gender specific sentence first to check to see if the replace is correctly implemented. We get bonus points if we can write the code to just output the same string if the input is gender-nuetral already.
He or she who laughs last, laughs best <- user input
+10 points if we can write code to make sure that the program outputs the same thing instead of...
He or he or she who laughs last, laughs best.
This is not crucial though, I can attempt that later.
Don't forget "they".
"A student can get a discount if he shows his ID." becoming "A student can get a discount if they show their ID." That would be cool. But much harder to achieve.
The "he or she" version is of course much more simple. It's a simple string replacement on words like he, she, him, her, his and hers... that's it I guess.
As for the extra points, again when you find one of he, she, him, her, his, or hers. Then you check for "or" plus the gender counterpart. If it exists you don't change it.
Yeah it would be easy to keep adding different pronouns such as "they" and "it" to be replaced by something else. I just need a starting point really on how to scan the input and make it "pickup" one of the pronouns so I will be able to replace it.
You do look for pronouns. You just look for "he", "she", "him", "her", "his" and "hers".
I'm not sure if I understand your doubt.
Thats the problem... I'm unsure of what code/functions to use to scan the input to look for pronouns. Is it something like, string::find(string str) ??
I'm just not sure how to begin the statement.
Ah. Of course! Though you wanted to detect what words where pronouns... Too late. Time I go to bed.
Anyway, http://www.cppreference.com/cppstring/index.html lists string member functions, among other things.
You will want to use string.find(), yes.
You take the user input to a string, and then process it. You process it by looking at those occurrences and making the changes to another string.
Try something based on this and after you come up with the code, you will help you fix it if there is any need.
Should I continue to post here or pm you?
Here's what I've got so far... Doesn't really work.
I have the user input as strInput and a function that will need to loop until the user input = "I'm done".
I also have a function at the beginning to keep running the search and replacing as long as the user keeps putting in sentences.
I'm not really sure if this helps a great deal, I'm just at a loss of how to get it going. The link you sent before was pretty helpful. Any further helpwould be most appreciated.
using namespace std;
string::size_type loc = strInput.find("he", "him", 0)
if( loc = string::npos )
string strInput.replace("he", "he or she", "him", "him or her")
cout << strInput;
cout << "\nPlease enter a gender specific sentence\n";
cout << "or enter `I'm done' to exit the program.\n";
cin >> strInput
(strInput != "I'm Done")
"He or she is done" ;)
What about "He'd do it!"? One could interpret it as "He should do it!", "He could do it!", "He would do it!"... this assignment isn't very fundementally sound to me.
you should start with the code that compiles withoout calling function
just reading input from the user in the loop till the exit phase is entered
then - add the empty function
that accepts an entered string and printing it out as is.
and only then - start to add code that does the work
Currently you have open issues with the loop and function definition and calling