gpt4 book ai didi

python - 从 NTLK pos_tag 中仅删除 'NN' 个单词

转载 作者:行者123 更新时间:2023-11-28 22:54:15 25 4
gpt4 key购买 nike

我有一个使用 NLTK 查找名词和动词的代码。

from nltk.corpus import wordnet as wn
from nltk import pos_tag
import nltk


sentence = "Hello my name is Abhishek Mitra"
sentence = nltk.word_tokenize(sentence)
sent = pos_tag(sentence)
print sent

它返回:

[('Hello', 'NNP'), ('my', 'PRP$'), ('name', 'NN'), ('is', 'VBZ'), ('Abhishek', 'NNP'), ('Mitra', 'NNP')]

如何从列表中仅删除“NN”字词。

最佳答案

您可以使用列表理解来删除“NN”元素:

from nltk.corpus import wordnet as wn
from nltk import pos_tag
import nltk

sentence = "Hello my name is Abhishek Mitra"
sentence = nltk.word_tokenize(sentence)
sent = pos_tag(sentence)
print [s for s in sent if s[1] != 'NN']

关于python - 从 NTLK pos_tag 中仅删除 'NN' 个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18252076/

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