【问题标题】:How to extract age and gender of the person from unprocessed text/data?如何从未处理的文本/数据中提取人的年龄和性别?
【发布时间】:2020-08-17 01:30:59
【问题描述】:

我有一个带有文本列表的 CSV 文件(带有行的列),我想从每一行中提取患者的年龄,我不能用“是数字”做,因为还有一些其他数字在文本中。我怎么能做这样的事情?谢谢你

额外:我也想提取性别 - 患者有时被称为男性/女性,有时被称为男性/女性,有时被称为绅士/女士。

如果文本是 17 岁,是否有编写 findall 的方法,如果后面是 -year-old,则打印我的数字

re.findall("[\d].", '-year-old')

文本中的行示例:

This 23-year-old white female presents with...

...pleasant gentleman who is 42 years old...

...The patient is a 10-1/2-year-old born with...

...A 79-year-old Filipino woman...

Patient, 37,...

我如何获得年龄/性别列表

即:

Age:

    ['23','42','79','37'...]

Gender:

    ['female','male','male','female','male'...]

【问题讨论】:

  • 看看使用正则表达式。
  • 您的问题已经得到解答。检查here

标签: python pandas nlp data-science re


【解决方案1】:
re_list = [
    '\d*\-year-old',
    '\d*\ year old'
]

matches = []
for r in re_list:
    matches += re.findall( r, 'pleasant gentleman who is 42 years old, This 23-year-old white female presents with')
print(matches)

打印出来:

['23-year-old', '42 year old']

【讨论】:

    【解决方案2】:

    您可以使用 regex(正则表达式)轻松做到这一点。

    import re
    
    # returns all numbers
    age = re.findall("[\d].", your_text)
    
    # returns all words related to gender
    gender = re.findall("female|gentleman|woman", your_text)
    

    你可以用字典来对待的性别部分得到你的正确答案

    gender_dict = {"male": ["gentleman", "man", "male"],
                   "female": ["female", "woman", "girl"]}
    gender_aux = []
    for g in gender:
        if g in gender_dict['male']:
            gender_aux.append('male')
        elif g in gender_dict['female']:
            gender_aux.append('female')
    

    【讨论】:

    • 谢谢,但关键是文本中还有一些其他数字,所以它对我 re.findall 不起作用。是否有一种方法可以编写 findall,例如,如果文本是 17 岁,如果后面是 -year-old re.findall("[\d].", '-year-old'),则打印我的数字
    • 'he is male, and the male is 25 years" 在这种情况下,男性将在列表中被追加 2 次?所以我应该通过删除重复项来避免它,还是还有其他方法?
    猜你喜欢
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-15
    • 2017-03-27
    • 2020-05-15
    • 2016-06-19
    相关资源
    最近更新 更多