I have a function with which I want to anonymize texts by replacing the name of a person by 'visitor'.
To do so, I have written the following function:
def replaceName(text, name):
newText = text.replace(name, 'visitor')
return str(newText)
And I apply it using:
all_transcripts['msgText'] = all_transcripts.apply(lambda x: replaceName(x['msgText'], x['nameGuest']), axis=1)
However, this also replaces the name if it is a part of another word. Therefore, I want to only replace the instances where this word stands by itself. I have tried it as " "+name+" "
, however, this does not work if the name is at the beginning or end of a sentence.
Furthermore, I have considered: Python regular expression match whole word. However, here they say how to find such words, but not how to replace it. I am having trouble to both find and replace it.
Who can help me with this?