Python 如何制作NLTK pos_标记词而不是字符?

Python 如何制作NLTK pos_标记词而不是字符?,python,nltk,Python,Nltk,我有这个代码来查找句子中的名词和动词 # -*- coding: utf-8 -*- from nltk.corpus import wordnet as wn from nltk import pos_tag import nltk syno =[] sentence = '''His father suggested he study to become a parson instead, but Darwin was far more inclined to study natura

我有这个代码来查找句子中的名词和动词

 # -*- coding: utf-8 -*-
from nltk.corpus import wordnet as wn
from nltk import pos_tag
import nltk
syno =[]


sentence = '''His father suggested he study to become a parson instead, but Darwin was far more inclined to study natural history.DarwinDar·win (där'wĭn),Charles Robert.1809-1882.British naturalist who revolutionized the study of biology with his theory ofevolutionbased on natural selection
Like several scientists before him, Darwin believed all the life on earth evolved (developed gradually) over millions of years from a few common ancestors.'''

sent = pos_tag(word_tokenize(sentence))
这是回报

[('H', 'NNP'), ('e', 'VBP'), ('l', 'NN'), ('l', 'NN'), ('o', 'NN'), (' ', ':'), ('m', 'NN'), ('y', 'NN'), (' ', ':'), ('n', 'NN'), ('a', 'DT'), ('m', 'NN'), ('e', 'NN'), (' ', ':'), ('i', 'PRP'), ('s', 'VBZ'), (' ', ':'), ('A', 'DT'), ('b', 'NN'), ('h', 'NN'), ('i', 'PRP'), ('s', 'VBZ'), ('h', 'JJ'), ('e', 'NN'), ('k', 'NN'), (' ', ':'), ('M', 'NNP'), ('i', 'PRP'), ('t', 'VBP'), ('r', 'JJ'), ('a', 'DT')]

我希望它能操作文字,而不是字符!如何做到这一点?

您需要首先标记化:

>>> from nltk import pos_tag, word_tokenize
>>> sentence = "Hello my name is Derek. I live in Salt Lake city."
>>> pos_tag(word_tokenize(sentence))
[('Hello', 'NNP'), ('my', 'PRP$'), ('name', 'NN'), ('is', 'VBZ'), ('Derek.', 'NNP'), ('I', 'PRP'), ('live', 'VBP'), ('in', 'IN'), ('Salt', 'NNP'), ('Lake', 'NNP'), ('city', 'NN'), ('.', '.')]

谢谢我明白了。现在,我如何通过列表理解仅删除NN单词。@user2675742:?这是基本的Python,您可以在中找到它。我得到'NameError:name'word\u tokenize'未定义'。我已经编辑了我的代码。