Python how to get a list of (word, tag) tuples from StanfordCoreNLP subtree?

Viewed 16

Python how to get a list of (word, tag) tuples from StanfordCoreNLP subtree?

like: [('they', 'PRP'), ('still', 'RB'), ('work', 'VBP'), ('in', 'IN'), ('the', 'DT'), ('factory', 'NN')].

Thanks in advance.

It was found that tree.leaves() just give a list of word, like:

def test():

from nltk import Tree

s = '(ROOT (S (NP (PRP I)) (VP (VBP want) (S (VP (TO to) (VP (VB know) (SBAR (IN whether) (S (NP (PRP they)) (ADVP (RB still)) (VP (VBP work) (PP (IN in) (NP (DT the) (NN factory)))))))))) (. .)))'

tree = Tree.fromstring(s)

print('Output:', tree.leaves())

#Output:['I', 'want', 'to', 'know', 'whether', 'they', 'still', 'work', 'in', 'the', 'factory', '.']

0 Answers
Related