[英]Remove only 'NN' words' from NTLK pos_tag
我有一个代码来使用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']
a = [('Hello', 'NNP'), ('my', 'PRP$'), ('name', 'NN'), ('is', 'VBZ'), ('Abhishek', 'NNP'), ('Mitra', 'NNP')]
c = [b for b in a if b[-1] != 'NN']
我使用过滤功能:
>>> filter(lambda (word, tag): tag != 'NN', sent)
[('Hello', 'NNP'), ('my', 'PRP$'), ('is', 'VBZ'), ('Abhishek', 'NNP'), ('Mitra', 'NNP')]
这是另一种方法(使用元组的优势):
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)
sent_clean = [x for (x,y) in sent if y not in ('NN')]
print(sent_clean)
输出:
['Hello', 'my', 'is', 'Abhishek', 'Mitra']
说明:在代码中:
sent_clean = [x for (x,y) in sent if y not in ('NN')]
在POS标记句子中的每个单词后,您正在尝试提取由于POS标记而创建的元组的单词。 您指定要提取的条件是第二部分
同样,如果你想消除多个POS:
sent_clean2 = [x for (x,y) in sent if y not in ('PRP$', 'VBZ', 'NN')]
print(sent_clean2)
输出:
['Hello', 'Abhishek', 'Mitra']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.