【问题标题】:Finding gender related words in a text file在文本文件中查找与性别相关的词
【发布时间】:2021-01-07 11:34:12
【问题描述】:

所以说,我有以下声明str = What recent discussions she has had with the Secretary of State for Work and Pensions on the effect of that Department’s welfare policies on women

在陈述中,你可以清楚地看到这个问题是用“她”这样的词向女人提出的。如何在给定的文本中找到特定于女性的词(例如,参见上面的文本)以及使用 python 使用它们的次数。 例如

maleWords = '[He, his]'
femaleWords = '[She,her]'

word="What recent discussions she has had with the Secretary of State for Work and Pensions on the effect of that Department’s welfare policies on women"

maleCount = sum(如果是maleWords中的单词则为1,否则对于maleWords中的单词为0) femaleCount = sum(1 if word in femaleWords else 0 for word in femaleWords)

target_gender = 'male' if maleCount >= femaleCount else 'female' print(f"文本的目标性别是{target_gender}")

但是结果显示目标性别是男性,而通过查看句子目标性别是女性。

【问题讨论】:

  • 您必须以某种方式定义女性特定词是什么。计算机不知道这一点。要么你有一个女性特定词的列表,要么你从互联网上的某个网站上提取它。

标签: python string text nlp


【解决方案1】:

您可以使用this page 获取特定性别的单词并将它们+他/她添加到相应的列表中。

maleWords = [mw0, m1, m2...]
femaleWords = [fm0, f1, f2...]

然后计算男性单词与女性单词中文本单词的出现次数,并将文本的目标性别设置为更高的计数。例如:

message = 'What recent discussions she has had with the Secretary of State for Work and Pensions on the effect of that Department’s welfare policies on women'

maleWords = ['he', 'his']
femaleWords = ['she', 'her']

maleCount = sum(1 if word in maleWords else 0 for word in message.lower().split())
femaleCount = sum(1 if word in femaleWords else 0 for word in message.lower().split())

target_gender = 'male' if maleCount >= femaleCount else 'female'
print(f"text's target gender is {target_gender}")

输出:

text's target gender is female

您首先将文本设为小写,因此 (He, HE, hE...) 也会被计算在内。

【讨论】:

  • 我尝试使用您的代码运行给定文本。请参阅修改后的问题。但答案并不正确。
  • 您已将maleWords 和femaleWords 定义为字符串而不是列表,因此当您必须检查列表中的每个字符串时,您实际上是在检查字符串的每个字符。maleWords = ['He', 'his'] femaleWords = ['She', 'her']@Ravi跨度>
  • 所以我必须将我的字符串文本转换为列表?对
  • 如果我的回答对您有帮助,您可以接受我的回答。 @Ravi
猜你喜欢
  • 1970-01-01
  • 2013-04-29
  • 2011-07-31
  • 2011-01-17
  • 2019-08-22
  • 2021-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多