我正在使用Python,并且想匹配“需要”之前的最后一个名词。
text = "Charles and Kim are needing a good hot dog"
我必须使用re.findall和nltk做到这一点
我尝试了以下方法,但是显示了之前的所有信息,我只需要最后一个名词
post = re.findall(r'.*needing', text)[0]
我希望得到
Kim
答案 0 :(得分:1)
只需使用nltk中的POS标记即可。
您需要下载一些nltk资源,然后标记并找到所需的内容。这段代码可以做到:
import nltk
# You'll need to run these two resource downloads the first time you do this.
# So uncomment the following two lines
# nltk.download('punkt')
# nltk.download('averaged_perceptron_tagger')
text = "Charles and Kim are needing a good hot dog"
tokens = nltk.word_tokenize(text)
tags = nltk.pos_tag(tokens)
# You are interested in splitting the sentence here
sentence_split = tokens.index("needing")
# Find the words where tag meets your criteria (must be a noun / proper noun)
nouns_before_split = [word for (word, tag) in tags[:sentence_split] if tag.startswith('NN')]
# Show the result. The last such noun
print(nouns_before_split[-1])