import re
import spacy
import nltk
from nltk.corpus import stopwords
stop = stopwords.words('english')
from nltk.corpus import wordnet
inputfile = open('inputfile.txt', 'r')
String= inputfile.read()
nlp = spacy.load('en_core_web_sm')
def candidate_name_extractor(input_string, nlp):
input_string = str(input_string)
doc = nlp(input_string)
# Extract entities
doc_entities = doc.ents
# Subset to person type entities
doc_persons = filter(lambda x: x.label_ == 'PERSON', doc_entities)
doc_persons = filter(lambda x: len(x.text.strip().split()) >= 2, doc_persons)
doc_persons = list(map(lambda x: x.text.strip(), doc_persons))
print(doc_persons)
# Assuming that the first Person entity with more than two tokens is the candidate's name
candidate_name = doc_persons[0]
return candidate_name
if __name__ == '__main__':
names = candidate_name_extractor(String, nlp)
print(names)
我想从文本文件中提取候选者的名字,但是它返回了错误的值。当我用map删除list时,map也不工作并给出错误
从词性标注后获得的词表中,使用正则表达式提取所有带有名词性标记的单词:
对于
^{pr2}$Nouns_List
中的每个单词,检查它是否是英语单词。这可以通过检查synsets
是否可用于wordnet
中的该单词来完成:由于印度名字不能作为英语词典中的词条,这可能是从文本中提取它们的一种可能的方法。在
检查此代码。我得到
Ravana
作为输出。在编辑:
我用简历中的几句话创建了一个文本文件,并将其作为输入输入输入到我的程序中。以下仅显示代码的更改部分:
^{pr2}$它返回所有不在
wordnet
语料库中的名字,比如我的名字,我的房子的名字,地方,大学的名字和地点。在相关问题 更多 >
编程相关推荐