Reputation: 251
I'm using NLTK RegexpParser to extract noungroups and verbgroups from tagged tokens.
How do I walk the resulting tree to find only the chunks that are NP or V groups?
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 Carrier/NN) for/IN tissue-/JJ and/CC cell-culture/JJ for/IN (NP the/DT preparation/NN) of/IN (NP implants/NNS) and/CC (NP implant/NN) (V containing/VBG) (NP the/DT carrier/NN) ./.)
Upvotes: 13
Views: 7476
Reputation: 876
def preprocess(sent):
sent = nltk.word_tokenize(sent)
sent = nltk.pos_tag(sent)
return sent
pattern = 'NP: {<JJ>*<NNP.*>*}'
cp = nltk.RegexpParser(pattern)
exp = []
for line in lines:
line = preprocess(line)
cs = cp.parse(line)
for n in cs:
if isinstance(n, nltk.tree.Tree):
if n.label() == 'NP':
if len(n.leaves()) > 1:
req = ''
for leaf in n.leaves():
req += leaf[0]+' '
exp.append(req)
print(exp)
Upvotes: 0
Reputation: 3572
This should work:
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)
Upvotes: 13
Reputation: 101
Savino's answer is great, but it's also worth noting that subtrees can be accessed by index as well, e.g.
for n in range(len(chunked)):
do_something_with_subtree(chunked[n])
Upvotes: 0
Reputation: 8886
Small mistake in 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
Upvotes: 0