【问题标题】:How do I use this information in Python? I don't know how to use this data-type如何在 Python 中使用这些信息?我不知道如何使用这种数据类型
【发布时间】: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


【解决方案1】:

你可能已经找到答案了。我将它发布给将来可能面临这种情况的人。

for subtree in chunked_sent.subtrees():
    if subtree.node == 'NP': print subtree

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    相关资源
    最近更新 更多