从 NLP 中的名词阶段提取名词
谁能告诉我如何从以下输出中仅提取名词:
我已经使用以下过程根据给定语法标记并解析了字符串“Give me the review of movie”:-
sent=nltk.word_tokenize(msg)
parser=nltk.ChartParser(grammar)
trees=parser.nbest_parse(sent)
for tree in trees:
print tree
tokens=find_all_NP(tree)
tokens1=nltk.word_tokenize(tokens[0])
print tokens1
并获得了以下输出:
>>>
(S
(VP (V Give) (Det me))
(NP (Det the) (N review) (PP (P of) (N movie))))
(S
(VP (V Give) (Det me))
(NP (Det the) (N review) (NP (PP (P of) (N movie)))))
['the', 'review', 'of', 'movie']
>>>
现在我会喜欢只获取名词。我该怎么做?
Could anyone please tell me how to extract only the nouns from the following output:
I have tokenized and parsed the string "Give me the review of movie" based on a given grammar using following procedure:-
sent=nltk.word_tokenize(msg)
parser=nltk.ChartParser(grammar)
trees=parser.nbest_parse(sent)
for tree in trees:
print tree
tokens=find_all_NP(tree)
tokens1=nltk.word_tokenize(tokens[0])
print tokens1
and obtained the following output:
>>>
(S
(VP (V Give) (Det me))
(NP (Det the) (N review) (PP (P of) (N movie))))
(S
(VP (V Give) (Det me))
(NP (Det the) (N review) (NP (PP (P of) (N movie)))))
['the', 'review', 'of', 'movie']
>>>
Now I would like to only obtain the nouns. How do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要使用完整的解析器来获取名词。您可以简单地使用标记器。您可以使用的一个函数是 nltk.tag.pos_tag()。这将返回包含单词和词性的元组列表。您将能够迭代元组并找到带有“NN”或“NNS”标记的名词或复数名词的单词。
NLTK 有一个如何使用其标记器的文档。可以在这里找到:https://nltk.googlecode.com/svn/trunk/doc/ howto/tag.html,这里是 NLTK 书中有关使用标记器的章节的链接:https://nltk.googlecode.com/svn/trunk/doc/book/ch05.html
每个地方都有很多代码示例。
You don't need to use a full parser to get nouns. You can simply use a tagger. One function you can use is nltk.tag.pos_tag(). This will return a list of tuples with the word and part of speech. You'll be able to iterate over the tuples and find words tagged with 'NN' or 'NNS' for noun or plural noun.
NLTK has a how to document for how to use their taggers. It can be found here: https://nltk.googlecode.com/svn/trunk/doc/howto/tag.html and here is a link to the chapter in the NLTK book about using taggers: https://nltk.googlecode.com/svn/trunk/doc/book/ch05.html
There are many code examples in each of those places.