I created a custom classifier based chunker: DigDug_classifier
, which chunks the following sentence:
sentence = "There is high signal intensity evident within the disc at T1."
To create these chunks:
(S
(NP There/EX)
(VP is/VBZ)
(NP high/JJ signal/JJ intensity/NN evident/NN)
(PP within/IN)
(NP the/DT disc/NN)
(PP at/IN)
(NP T1/NNP)
./.)
I need to create a list of just the NP from the above, like this:
NP = ['There', 'high signal intensity evident', 'the disc', 'T1']
I wrote the following code:
output = []
for subtree in DigDug_classifier.parse(pos_tags):
try:
if subtree.label() == 'NP': output.append(subtree)
except AttributeError:
output.append(subtree)
print(output)
But that gives me this answer instead:
[Tree('NP', [('There', 'EX')]), Tree('NP', [('high', 'JJ'), ('signal', 'JJ'), ('intensity', 'NN'), ('evident', 'NN')]), Tree('NP', [('the', 'DT'), ('disc', 'NN')]), Tree('NP', [('T1', 'NNP')]), ('.', '.')]
What can I do to get the desired answer?