我有一个句子,我想提取词头项目,我可以使用斯坦福 NLP 库进行依赖解析。
我怎样才能在句子中提取主要头部?
在句子的情况下Download and share this tool
,头部将是Download
。
我尝试了以下方法:
def get_head_word(text):
standepparse=StanfordDependencyParser(path_to_jar='/home/stanford_resource/stanford-parser-full-2014-06-16/stanford-parser.jar',path_to_models_jar='/home/stanford_resource/stanford-parser-full-2014-06-16/stanford-parser-3.4-models.jar',model_path='/home/stanford_resource/stanford-parser-full-2014-06-16/stanford-parser-3.4-models/edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz')
parsetree=standepparse.raw_parse(text)
p_tree=list(parsetree)[0]
print p_tree.to_dot()
text = 'Download and share this tool'
get_head_word(text)
output:
digraph G{
edge [dir=forward]
node [shape=plaintext]
0 [label="0 (None)"]
0 -> 1 [label="root"]
1 [label="1 (Download)"]
1 -> 2 [label="cc"]
1 -> 3 [label="conj"]
1 -> 5 [label="dobj"]
2 [label="2 (and)"]
3 [label="3 (share)"]
4 [label="4 (this)"]
5 [label="5 (software)"]
5 -> 4 [label="det"]
}