The aim is to extract the sub-tree (phrases) from the sentence if the 'nsubj' exists in the given sentence.
Here is the code which I am using:
import spacy
nlp = spacy.load('en')
piano_doc = nlp('The alarm clock is, to many high school students, a wailing monstrosity whose purpose is to torture all who are sleep-deprived')
for token in piano_doc:
if token.dep_ == 'nsubj':
print (token.text, token.tag_, token.head.text, token.dep_)
subtree = token.subtree
print([(t.text) for t in subtree])
print('*' * 50)
The output we get is: clock NN is nsubj
['The', 'alarm', 'clock']
purpose NN is nsubj
['whose', 'purpose']
who WP are nsubj
['who']
But the output i am expecting in the case of nsubj is the whole subtree i.e.
purpose NN is nsubj
['whose', 'purpose','is','to','torture']
who WP are nsubj
['who' ,'are' ,'sleep-deprived']