如何在带有模式的Spacy模型中从简历中解析出生日期 [英] How can parse the date_of_birth from resume in spacy model with patterns
本文介绍了如何在带有模式的Spacy模型中从简历中解析出生日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我添加了多个取数模式date_of_birth
,有时取数正确,但有时得到的日期不是出生日期。
您可以看到我添加的patterns
DOB_PATTERN = [
# dates of the form 23-12-2018
[{'IS_DIGIT': True}, {'ORTH': '/'}, {'IS_DIGIT': True}, {'ORTH': '/'}, {'IS_DIGIT': True}],
# dates of the form 10-Aug-2018
[{'IS_DIGIT': True}, {'ORTH': '/'}, {'IS_ALPHA': True}, {'ORTH': '/'}, {'IS_DIGIT': True}],
# MM-DD-YYYY and YYYY-MM-DD
[{'IS_DIGIT': True}, {'ORTH': '-'}, {'IS_DIGIT': True}, {'ORTH': '-'}, {'IS_DIGIT': True}],
# dates of the form 10-Aug-2018
[{'IS_DIGIT': True}, {'ORTH': '-'}, {'IS_ALPHA': True}, {'ORTH': '-'}, {'IS_DIGIT': True}],
# dates of the form Aug-10-2018
[{'IS_ALPHA': True}, {'ORTH': '-'}, {'IS_DIGIT': True}, {'ORTH': '-'}, {'IS_DIGIT': True}]
]
def custom_tokenizer(nlp, infix_reg):
return Tokenizer(nlp.vocab, infix_finditer = infix_reg.finditer)
def dob(nlp_text):
infix_re = re.compile(r'''[-/,]''')
nlp = spacy.load('en_core_web_sm')
nlp.tokenizer = custom_tokenizer(nlp, infix_re)
DATE = nlp.vocab.strings['DATE']
matcher = Matcher(nlp.vocab)
matcher.add("MATCHING", DOB_PATTERN)
doc = nlp(nlp_text)
matches = matcher(doc)
for match_id, start, end in matches:
span = doc[start:end]
return span.text
我有一个date_of_birth
这样的Date of Birth: 12th OCT 1988
在没有被检测到的简历中,请给我一个解决我问题的模式列表,并可以从简历中获取出生日期。谢谢
推荐答案
,因为您已经在dob_pattern
中介绍了其他用例。我想就你所询问的特殊花样提出一些建议。您可以这样做:
import spacy
nlp = spacy.load("en_core_web_sm")
text = ("Sam was born on 12th OCT 1988. ")
doc = nlp(text)
for entity in doc.ents:
if entity.label_=='DATE':
print(entity)
#output 12th OCT 1988
这篇关于如何在带有模式的Spacy模型中从简历中解析出生日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文