gpt4 book ai didi

python - 从 Python 中的非结构化文本中提取一个人的年龄

转载 作者:太空狗 更新时间:2023-10-30 01:24:34 26 4
gpt4 key购买 nike

我有一个包含简短传记的行政文件数据集。我正在尝试通过使用 python 和一些模式匹配来提取人们的年龄。一些句子的例子是:

  • “邦德先生,67 岁,是英国的一名工程师”
  • “Amanda B. Bynes,34 岁,是一名 Actor ”
  • “Peter Parker(45 岁)将成为我们的下一任管理员”
  • “迪伦先生今年 46 岁。”
  • “史蒂夫·琼斯,年龄:32,”

这些是我在数据集中确定的一些模式。我想补充一点,还有其他模式,但我还没有遇到过它们,也不确定我如何才能做到这一点。我编写了以下代码,效果很好,但效率很低,因此在整个数据集上运行会花费太多时间。

#Create a search list of expressions that might come right before an age instance
age_search_list = [" " + last_name.lower().strip() + ", age ",
" " + clean_sec_last_name.lower().strip() + " age ",
last_name.lower().strip() + " age ",
full_name.lower().strip() + ", age ",
full_name.lower().strip() + ", ",
" " + last_name.lower() + ", ",
" " + last_name.lower().strip() + " \(",
" " + last_name.lower().strip() + " is "]

#for each element in our search list
for element in age_search_list:
print("Searching: ",element)

# retrieve all the instances where we might have an age
for age_biography_instance in re.finditer(element,souptext.lower()):

#extract the next four characters
age_biography_start = int(age_biography_instance.start())
age_instance_start = age_biography_start + len(element)
age_instance_end = age_instance_start + 4
age_string = souptext[age_instance_start:age_instance_end]

#extract what should be the age
potential_age = age_string[:-2]

#extract the next two characters as a security check (i.e. age should be followed by comma, or dot, etc.)
age_security_check = age_string[-2:]
age_security_check_list = [", ",". ",") "," y"]

if age_security_check in age_security_check_list:
print("Potential age instance found for ",full_name,": ",potential_age)

#check that what we extracted is an age, convert it to birth year
try:
potential_age = int(potential_age)
print("Potential age detected: ",potential_age)
if 18 < int(potential_age) < 100:
sec_birth_year = int(filing_year) - int(potential_age)
print("Filing year was: ",filing_year)
print("Estimated birth year for ",clean_sec_full_name,": ",sec_birth_year)
#Now, we save it in the main dataframe
new_sec_parser = pd.DataFrame([[clean_sec_full_name,"0","0",sec_birth_year,""]],columns = ['Name','Male','Female','Birth','Suffix'])
df_sec_parser = pd.concat([df_sec_parser,new_sec_parser])

except ValueError:
print("Problem with extracted age ",potential_age)

我有几个问题:

  • 是否有更有效的方法来提取这些信息?
  • 我应该改用正则表达式吗?
  • 我的文本文档很长,而且有很多。我可以一次搜索所有项目吗?
  • 检测数据集中其他模式的策略是什么?

从数据集中提取的一些句子:

  • “2010 年授予 Love 的股权奖励占其总薪酬的 48%”
  • “George F. Rubin(14)(15) 68 岁,自 1997 年起担任受托人。”
  • “INDRA K. NOOYI,56 岁,自 2006 年以来一直担任百事公司首席执行官 (CEO)”
  • “47 岁的 Lovallo 先生于 2011 年被任命为财务主管。”
  • “Charles Baker 先生,79 岁,是生物技术公司的商业顾问。”
  • “Botein 先生,43 岁, self 们成立以来一直是我们董事会的成员。”

最佳答案

由于必须处理您的文本,而不仅仅是模式匹配,正确的方法是使用现有的众多 NLP 工具之一。

您的目标是使用通常基于机器学习模型完成的命名实体识别 (NER)。 NER 事件尝试识别文本中确定的一组实体类型。例如:地点、日期、组织和人名

虽然不是 100% 精确,这比简单的模式匹配要精确得多(尤其是对于英语),因为它依赖于模式以外的其他信息,例如词性 (POS),依赖解析等

看看我使用 Allen NLP Online Tool 为您提供的短语获得的结果(使用细粒度 NER 模型):

  • “邦德先生,67 岁,是英国的一名工程师”:

Mr Bond, 67, is an engineer in the UK

  • “Amanda B. Bynes,34 岁,是一名 Actor ”

Amanda B. Bynes, 34, is an actress

  • “Peter Parker(45 岁)将成为我们的下一任管理员”

Peter Parker (45) will be our next administrator

  • “迪伦先生今年 46 岁。”

Mr. Dylan is 46 years old.

  • “史蒂夫·琼斯,年龄:32,”

Steve Jones, Age: 32,

请注意,最后一个是错误的。正如我所说,不是 100%,但易于使用。

这种方法的一大优势:您不必为数百万种可用可能性中的每一种都制作特殊模式。

最棒的是:您可以将它集成到您​​的 Python 代码中:

pip install allennlp

和:

from allennlp.predictors import Predictor
al = Predictor.from_path("https://s3-us-west-2.amazonaws.com/allennlp/models/fine-
grained-ner-model-elmo-2018.12.21.tar.gz")
al.predict("Your sentence with date here")

然后,查看“日期”实体的结果字典。

Spacy 也是如此:

!python3 -m spacy download en_core_web_lg
import spacy
sp_lg = spacy.load('en_core_web_lg')
{(ent.text.strip(), ent.label_) for ent in sp_lg("Your sentence with date here").ents}

(但是,我有一些糟糕的经历,那里有错误的预测 - 尽管它被认为更好)。

有关更多信息,请阅读 Medium 上这篇有趣的文章:https://medium.com/@b.terryjack/nlp-pretrained-named-entity-recognition-7caa5cd28d7b

关于python - 从 Python 中的非结构化文本中提取一个人的年龄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57395165/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com