首页 > 解决方案 > 如何在大量文本中替换性别代词?

问题描述

我正在写一部小说,写到一半我决定把一个重要的主人公从男性换成女性。我写了一些简单的 Python 代码,以为我可以轻松地更改特定字符的代词,但它意外地更改了所有字符的代词。这是小说中的一些示例文本:

example_text = "John walked to the market. He was tired, but decided to leg it anyway, he needed the exercise! He met Joe along the way. Joe was a tall man and walked fast, during his heyday, he was an impressive athlete."

我想把乔(男)改成简(女),写了下面的简单代码:

example_text = example_text.replace(" he ", " she ")
example_text = example_text.replace(" He ", " She ")
example_text = example_text.replace(" his ", " her ")
example_text = example_text.replace(" man ", " woman ")
example_text = example_text.replace("Joe", "Jane")

但是上面的代码将 Joe 和 John 的代词更改为女性。我现在意识到我必须使用 NLP 来执行此操作,但是是否有模块或算法可以执行此操作?

标签: pythonnlpnltk

解决方案


推荐阅读