import nltk, re
cp = nltk.RegexpParser('CHUNK: {<V.*><TO><V.*>}')
brown = nltk.corpus.brown
for sent in brown.tagged_sents():
tree = cp.parse(sent)
for subtree in tree.subtrees():
if subtree.label() == "CHUNK":
print(subtree)
以上为修改好的程序,在使用sbutree.node时发现该方法已经不存在,推荐使用subtree.label()
help(subtree.label)
Help on method label in module nltk.tree:
label() method of nltk.tree.Tree instance
Return the node label of the tree.
>>> t = Tree.fromstring('(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))')
>>> t.label()
'S'
:return: the node label (typically a string)
>>> help(subtree.node)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/nltk/tree.py", line 202, in _get_node
raise NotImplementedError("Use label() to access a node label.")
NotImplementedError: Use label() to access a node label.
以下是我作出修改的证据,如果什么时候我的这个代码无效,别举报我故意糊弄人就行