Thread: Replace words in string

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    10

    Replace words in string

    Hello,

    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.
    or
    "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.
    WR

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You can use the string::replace overloads to do this.

    http://www.sgi.com/tech/stl/basic_string.html

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    10
    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.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    10
    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.
    Ex.
    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.

    Thanks

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    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.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    10
    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.

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    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.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Posts
    10
    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.

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    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.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #12
    Registered User
    Join Date
    Dec 2006
    Posts
    10
    Thanks,
    Should I continue to post here or pm you?

  13. #13
    Registered User
    Join Date
    Dec 2006
    Posts
    10
    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.



    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void searchSTR
    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;
    
    
    int main()
    {
    
    	cout << "\nPlease enter a gender specific sentence\n";
    	
    	cout << "or enter `I'm done' to exit the program.\n";
    		  
    		  cin >> strInput
    		  
    		  do
    		  	{
    				searchSTR
    			}
    			while 
    				(strInput != "I'm Done")
    				
    	return 0;
    	)
    		  
    	
    
    }
    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.
    Thanks.
    WR

  14. #14
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    "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.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. Replies: 2
    Last Post: 05-05-2002, 01:38 PM