我用StanfordCoreNLP做了依赖性解析,用的是下面的代码。
from stanfordcorenlp import StanfordCoreNLP
nlp = StanfordCoreNLP('stanford-corenlp-full-2018-10-05', lang='en')
sentence = 'The clothes in the dressing room are gorgeous. Can I have one?'
tree_str = nlp.parse(sentence)
print(tree_str)
而且我得到了输出。
(S
(NP
(NP (DT The) (NNS clothes))
(PP (IN in)
(NP (DT the) (VBG dressing) (NN room))))
(VP (VBP are)
(ADJP (JJ gorgeous)))
(. .)))
我怎么才能在Stanza中得到同样的输出?
import stanza
from stanza.server import CoreNLPClient
classpath='/stanford-corenlp-full-2020-04-20/*'
client = CoreNLPClient(be_quite=False, classpath=classpath, annotators=['parse'], memory='4G', endpoint='http://localhost:8900')
client.start()
text = 'The clothes in the dressing room are gorgeous. Can I have one?'
ann = client.annotate(text)
sentence = ann.sentence[0]
dependency_parse = sentence.basicDependencies
print(dependency_parse)
在Stanza中,我似乎必须把组成句子的句子拆开。我是不是做错了什么?
请注意,我的目标是提取名词短语。
这里有一些关于使用的文档。https:/stanfordnlp.github.iostanzacorenlp_client.html#usage。
这显示了如何获得选区解析(就是你的输出示例的形式)。 依赖解析是一个词之间的边缘列表。
# set up the client
with CoreNLPClient(annotators=['tokenize','ssplit','pos','lemma','ner', 'parse'], timeout=30000, memory='16G') as client:
# submit the request to the server
ann = client.annotate(text)
# get the first sentence
sentence = ann.sentence[0]
# get the constituency parse of the first sentence
print('---')
print('constituency parse of first sentence')
constituency_parse = sentence.parseTree
print(constituency_parse)
# get the first subtree of the constituency parse
print('---')
print('first subtree of constituency parse')
print(constituency_parse.child[0])