Need help with code- reading POS and words from parse tree

Parse tree

(TOP (S (NP (DT The)
(NNP Fulton)
(NNP County)
(NNP Grand)
(NNP Jury))
(VP (VBD said)
(NP (NNP Friday))
(SBAR (-NONE- 0)
(S (NP (DT an)
(NN investigation)
(PP (IN of)
(NP (NP (NNP Atlanta))
(POS ‘s)
(JJ recent)
(JJ primary)
(NN election))))
(VP (VBD produced)
(NP ( )
(DT no)
(NN evidence)
(’’ ‘’)
(SBAR (IN that)
(S (NP (DT any)
(NNS irregularities))
(VP (VBD took)
(NP (NN place)))))))))))
(. .))


(TOP (S (NP (DT The)
(NN jury))
(ADVP (RB further))
(VP (VBD said)
(PP (IN in)
(NP (JJ term-end)
(NNS presentments)))
(SBAR (IN that)
(S (NP (NP (DT the)
(NNP City)
(NNP Executive)
(NNP Committee))
(, ,)
(SBAR (WHNP (WDT which))
(S (NP (-NONE- T))
(VP (VBD had)
(NP (ADJP (JJ over-all))
(NN charge)
(PP (IN of)
(NP (DT the)
(NN election))))))))
(, ,)
( )
(VP (VBZ deserves)
(NP (DT the)
(NN praise)
(CC and)
(NNS thanks)
(PP (IN of)
(NP (DT the)
(NNP City)
(PP (IN of)
(NP (NNP Atlanta)))))
(’’ ‘’)
(PP (IN for)
(NP (NP (DT the)
(NN manner))
(WHNP (WDT which)))
(S (NP (DT the)
(NN election))
(AUX (VBD was))
(VP (VBN conducted))))))))))))
(. .))
On this parse tree, I need to read only POS and words and write it in sentence. like this
DT The NNP Fulton NNP County NNP Grand NNP Jury VBD said NNP Friday DT
an NN investigation

The quickest way is probably using NLTK’s Tree object. It has a pos method that will return a list of tuples containing each word paired with its pos tag. The order of the list contents is derived from the tree’s hierarchy.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.