Edit: the solution is: there is a label() function! So I just need to find the sublist for which sublist.label() == SBAR.
I have a list that looks like this
(ROOT
(S
(NP
(NP (DT The) (NN dog))
(SBAR
(WHNP (WP who))
(S (VP (VBD ate) (NP (DT the) (NN food))))))
(VP (VBZ is) (ADJP (JJ dead)))
(. .)))
edit: I can also have this output, if this makes it easier:
[Tree('S', [Tree('NP', [Tree('NP', [Tree('DT', ['The']), Tree('NN', ['play'])]), Tree(',', [',']), Tree('SBAR', [Tree('WHNP', [Tree('WDT', ['which'])]), Tree('S', [Tree('VP', [Tree('VBD', ['started']), Tree('NP-TMP', [Tree('JJ', ['last']), Tree('NN', ['week'])])])])]), Tree(',', [','])]), Tree('VP', [Tree('VBZ', ['has']), Tree('VP', [Tree('VBN', ['been']), Tree('VP', [Tree('VBN', ['sold']), Tree('PRT', [Tree('RP', ['out'])]), Tree('ADVP', [Tree('RB', ['ever'])]), Tree('ADVP', [Tree('IN', ['since'])])])])]), Tree('.', ['.'])])]
and I want to search for the sublist
(SBAR
(WHNP (WP who))
(S (VP (VBD ate) (NP (DT the) (NN food))))))
However, I don't know how to find the list with the "root" SBAR. When I code this
print(parse[0][0][1][0])
print(parse[0][0][1][1])
I get
------0010------
(WHNP (WP who))
------0011------
(S (VP (VBD ate) (NP (DT the) (NN food))))
How can I search for SBAR? Or in other words, how can I write a loop that access the list that has the "root" SBAR?
Thanks!
Edit: here's my code
STANFORD = os.path.abspath("stanford-corenlp-full-2018-10-05")
# Create the server
server = CoreNLPServer(
os.path.join(STANFORD, "stanford-corenlp-3.9.2.jar"),
os.path.join(STANFORD, "stanford-corenlp-3.9.2-models.jar"),
)
# Start the server in the background
server.start()
parser = CoreNLPParser()
parse = next(parser.raw_parse(sentence))
print(parse[0][0][1][0])
print(parse[0][0][1][1])
server.stop()
I don't know if that helps a lot... What I get is a (nested?) list
Thanks again