Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is there a software/script or any way I can replace all the her in a document with him and his wherever which one's applicable respectively automatically?

example

Calls her and tells her that her car is …

to

Calls him and tells him that his car is …
share|improve this question

2 Answers 2

up vote 7 down vote accepted

The short answer: Yes, but it's harder than you think.

The long answer: Ordinary find-and-replace code operates on a morphological level, that is, by looking at the form of a text rather than by understanding its meaning. But there is no morphological indication to differentiate the objective and possessive cases of the third-person female pronoun, so ordinary find-and-replace can't tell the difference between them. In order to do so, you need a tool which can analyze your text at a lexical level -- that is, one which can examine the text and derive its meaning.

This is a much harder problem than simple find-and-replace, unless your problem domain is strictly enough constrained that you can hack together a few heuristics and then manually inspect and patch up the result. If you can get away with that, great!

If not, and it's a problem worth the effort of writing code to do the job properly, then you'll do well to start with the Stanford NLP Project's software repository, specifically CoreNLP, which includes an excellent part-of-speech tagger -- the exact tool you need to perform the lexical analysis I described.

To produce an example of what you get from CoreNLP, I fed the CoreNLP online demo the following sentence, based on your examples:

He calls her and tells her that her car is ready for pickup.

which it tokenized thusly:

Id Word   Lemma  Char begin Char end POS  NER Normalized NER Speaker 
—— —————— —————— —————————— ———————— ———— ——— —————————————— ——————— 
1  He     he     0          2        PRP  O                  PER0    
2  calls  call   3          8        VBZ  O                  PER0    
3  her    she    9          12       PRP  O                  PER0    
4  and    and    13         16       CC   O                  PER0    
5  tells  tell   17         22       VBZ  O                  PER0    
6  her    she    23         26       PRP$ O                  PER0    
7  that   that   27         31       DT   O                  PER0    
8  her    she    32         35       PRP$ O                  PER0    
9  car    car    36         39       NN   O                  PER0    
10 is     be     40         42       VBZ  O                  PER0    
11 ready  ready  43         48       JJ   O                  PER0    
12 for    for    49         52       IN   O                  PER0    
13 pickup pickup 53         59       NN   O                  PER0    
14 .      .      59         60       .    O                  PER0    

With reference to a list of the de facto standard part-of-speech tags, we find that CoreNLP has correctly identified the case of each pronoun in which we're interested -- PRP for personal pronouns, PRP$ for possessive pronouns.

Armed with this information, and the knowledge of the opposite-gender equivalents of each pronoun case, we could perform our replacements; in fact, since CoreNLP tells us character positions as well as parts of speech, instead of a find-and-replace semantic we could actually walk the parse tree and reconstruct the sentence word-by-word, filling whitespace between words and replacing pronouns of interest as we encounter them.

And that's how you can do it! Obviously, this is more or less the lightest possible treatment of such a complex subject -- but, if you're inclined to write the necessary code, this should be enough to get you into the starting blocks. Good luck!

share|improve this answer

The obvious approach would be to use the find and replace tool in just about any word processor (or in unix, using sed) to find all instances of 'her' and replace with 'him'; and vice-versa. To do this in sed, you could run sed 's/her/his/g [file]'

However, this wouldn't work in most cases due to the ambiguity of pronoun 'her'. Additionally, there are more subtle problems with this approach such as the following:

She told her friend that her garden was lovely. You could interpret this sentence meaning Sarah told Jamie that Jamie's garden was lovely, that is, that the second 'her' refers to the friend, and not the speaker. In this case a blind find-and-replace wouldn't be able to distinguish who the pronoun refers to. This problem is actually thoroughly studied in natural language processing circles, and is known as co-reference resolution. Unfortunately, it is likely not nearly mature enough to be used for your purposes.

Additionally, if you blindly replaced 'her' with 'him', you would also need to replace 'She' with 'He', because She told his friend implies something completely different!

So in short, a simple set of find and replace instructions (either manually or through some tool such as sed or awk) will not be up to the task, but short of using state of the art tools for syntactic parsing and co-reference resolution, is really the only practical option.

share|improve this answer
    
Your sed example would produce, from the asker's example data, "Calls his and tells his that his car is..." I didn't give such an example in my own answer, precisely because there's no way it can be other than bogus. (To be fair, you do sort of have a point about co-reference resolution; when I threw your sample sentence at CoreNLP, it regarded both instances of 'her' as references to 'she'. On the other hand, absent any other context, a human would have a hard time resolving that ambiguity, too.) –  Aaron Miller Oct 10 '13 at 16:18
    
@Aaron Miller: Did you even read my post? My entire point was that find and replace will not work, but might be "good enough" given the complexity of the task. To actually solve this, you'd have to use a full syntactic parse along with co-reference information and even then you'd be lucky to get an accuracy of even 60% (given that the state of the art syntactic parsers perform miserably in long sentences). Because of this, the clearly incorrect find-and-replace solution is really the only practical approach and actually answers his original question. –  tgood Oct 10 '13 at 17:18
    
I did read your post. Did you read mine? I briefly covered the bogus method you're pushing, and then I went on to give an example of using said state-of-the-art tools, which are nicely packaged and actually pretty easy to use. Unless the asker's problem domain is a lot broader than his examples suggest, I suspect analyzing the CoreNLP POS tagger's output will give him everything he needs to get the job done -- it's certainly a better place to start than 'oh, NLP tools suck, just use sed (and then go back and fix everything by hand)'. –  Aaron Miller Oct 10 '13 at 21:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.