在Python中从list中的list中提取“root”

2024-09-27 21:23:42 发布

您现在位置:Python中文网/ 问答频道 /正文

编辑:解决方法是:有一个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))))))

但是,我不知道如何找到带有“根”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()

我不知道这是否有帮助。。。我得到的是一个(嵌套的?)列表

再次感谢


Tags: thetree编辑列表serverparsenpdt

热门问题