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])
只需使用nltk的词性标记。你知道吗
您需要下载一些nltk资源,然后标记并找到您想要的。此代码将执行以下操作:
相关问题 更多 >
编程相关推荐