编辑:解决方法是:有一个label()函数!所以我只需要找到sublist.label()== SBAR的子列表。
我有一个看起来像这样的列表
(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)))
(. .)))
编辑:如果这样更容易,我也可以得到此输出:
[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('.', ['.'])])]
我想搜索子列表
(SBAR
(WHNP (WP who))
(S (VP (VBD ate) (NP (DT the) (NN food))))))
但是,我不知道如何使用“ root” SBAR查找列表。 当我编写代码
print(parse[0][0][1][0])
print(parse[0][0][1][1])
我明白了
------0010------
(WHNP (WP who))
------0011------
(S (VP (VBD ate) (NP (DT the) (NN food))))
如何搜索SBAR?换句话说,我该如何编写一个循环来访问具有“根” SBAR的列表?
谢谢!
编辑: 这是我的代码
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()
我不知道这是否有帮助...我得到的是(嵌套的)列表
再次感谢