【发布时间】:2010-12-26 03:48:54
【问题描述】:
根据NLTK的书,我先应用语法,然后解析。
grammar = r"""
NP: {<DT|PP\$>?<JJ>*<NN>}
{<NNP>+}
"""
cp = nltk.RegexpParser(grammar)
chunked_sent = cp.parse(sentence)
当我打印 chunked_sent 时,我得到了:
(S
i/PRP
use/VBP
to/TO
work/VB
with/IN
you/PRP
at/IN
(NP match/NN)
./.)
我不想只看它。我真的想抽出“NP”名词短语。
我怎样才能打印出“匹配”...这是名词短语? 我想从该 chunked_sent 中取出所有“NP”。
for k in chunked_sents:
print k
(u'i', 'PRP')
(u'use', 'VBP')
(u'to', 'TO')
(u'work', 'VB')
(u'with', 'IN')
(u'you', 'PRP')
(u'at', 'IN')
(NP match/NN)
(u'.', '.')
for k in chunked_sents:
print k[0]
i
use
to
work
with
you
at
(u'match', 'NN')
看,由于某种原因,我失去了“NP”。
另外,如何确定 k[0] 是字符串还是元组(如上例)
【问题讨论】:
-
注意:(NP匹配/NN)是
-
您是否能够在没有解析信息的情况下获得名词短语列表?你最终做了什么?
标签: python list variables types tuples