0

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

maeven
  • 21
  • 4
  • your list example is not "pythonic". I find it hard to understand the question, try the following link maybe it will help you. https://stackoverflow.com/questions/4843158/check-if-a-python-list-item-contains-a-string-inside-another-string – helpper Dec 21 '19 at 13:46
  • Thanks! I'm working with the Stanford Parser, maybe this is why my list is not "pythonic"? Thanks for the link. Do you know how I could know where "SBAR" is? I need the whole sub-list... – maeven Dec 21 '19 at 15:04
  • For get best solution, you should **submit the code** you wrote... it is hard to understand if your output is a String or a List or nested list? ```list = ["(ROOT", " (S", "(NP", ]``` – helpper Dec 21 '19 at 16:39
  • I added my code. I checked for isinstance(parse,list) and the ouput is TRUE – maeven Dec 21 '19 at 17:47

0 Answers0