2011-10-01 36 views
11

我正在使用NLTK RegexpParser從標記的標記中提取noungroups和verbgroups。NLTK分塊和散步結果樹

如何遍歷結果樹才能找到NP或V組的塊?

from nltk.chunk import RegexpParser 

grammar = ''' 
NP: {<DT>?<JJ>*<NN>*} 
V: {<V.*>}''' 
chunker = RegexpParser(grammar) 
token = [] ## Some tokens from my POS tagger 
chunked = chunker.parse(tokens) 
print chunked 

#How do I walk the tree? 
#for chunk in chunked: 
# if chunk.??? == 'NP': 
#   print chunk 

(S (NP載波/ NN) 爲/ IN 組織 -/JJ 和/ CC 細胞培養/ JJ 爲/ IN (NP的/ DT製劑/ NN) 的/ IN (NP植入物/ NNS) 和/ CC (NP植入物/ NN) (含有/ VBG V) (NP的/ DT載體/ NN) ./。)

回答

11

這應該工作:

for n in chunked: 
    if isinstance(n, nltk.tree.Tree):    
     if n.label() == 'NP': 
      do_something_with_subtree(n) 
     else: 
      do_something_with_leaf(n) 
+0

給我 AttributeError的: '元組' 對象有沒有屬性 '節點' n是<類型 '元組'> –

+0

編輯答案... –

+1

就像一個魅力的 - 謝謝! –

0

小的失誤在token

from nltk.chunk import RegexpParser 
grammar = ''' 
NP: {<DT>?<JJ>*<NN>*} 
V: {<V.*>}''' 
chunker = RegexpParser(grammar) 
token = [] ## Some tokens from my POS tagger 
//chunked = chunker.parse(tokens) // token defined in the previous line but used tokens in chunker.parse(tokens) 
chunked = chunker.parse(token) // Change in this line 
print chunked 
0

薩維諾的答案是偉大的,但它也是值得注意的是,子樹可以通過索引訪問爲好,例如

for n in range(len(chunked)): 
    do_something_with_subtree(chunked[n])