python - 如何在大量文本中替换性别代词?
问题描述
我正在写一部小说,写到一半我决定把一个重要的主人公从男性换成女性。我写了一些简单的 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 来执行此操作,但是是否有模块或算法可以执行此操作?
解决方案
推荐阅读
- django - Django使用复选框小部件从视图中运行python函数
- git - Build.Reason 在 azure devop 中显示除 PullRequest 之外的 IndividualCI
- twitter-bootstrap - 在 Bootstrap 5 中更改导航链接的字体系列?
- c++ - 插入时调用 std::map 值对象的成员函数
- javascript - jQuery根据选择字段隐藏表单?
- snowflake-cloud-data-platform - 除法运算符导致大量精度
- r - 如何同时在两个数据集中使用 - R
- c# - 使用 MVC 加载视图时如何解决此错误
- python - 列表上的逻辑函数 - 奇怪的 python 行为
- xml - 使用 XSLT 将元素转换为属性时如何在 XML 中设置命名空间?