从 NLP 中的名词阶段提取名词 [英] Extracting nouns from Noun Phase in NLP

查看:19
本文介绍了从 NLP 中的名词阶段提取名词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我如何从以下输出中只提取名词:

我使用以下过程根据给定的语法对字符串Give me the review of movie"进行了标记和解析:-

sent=nltk.word_tokenize(msg)解析器=nltk.ChartParser(语法)树=parser.nbest_parse(发送)对于树中的树:打印树令牌=find_all_NP(树)token1=nltk.word_tokenize(tokens[0])打印令牌1

并获得以下输出:

<预><代码>>>>(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', '电影']>>>

现在我只想获取名词.我该怎么做?

解决方案

您不需要使用完整的解析器来获取名词.您可以简单地使用标记器.您可以使用的一个函数是 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

每个地方都有很多代码示例.

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?

解决方案

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.

这篇关于从 NLP 中的名词阶段提取名词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆