使用StanfordParser从解析的句子中获取类型化的依赖项

时间:2015-04-13 10:24:23

标签: python parsing nlp nltk stanford-nlp

使用NLTK的StanfordParser,我可以解析这样一句话:

os.environ['STANFORD_PARSER'] = 'C:\jars' 
os.environ['STANFORD_MODELS'] = 'C:\jars'  
os.environ['JAVAHOME'] ='C:\ProgramData\Oracle\Java\javapath' 
parser = stanford.StanfordParser(model_path="C:\jars\englishPCFG.ser.gz")
sentences = parser.parse(("bring me a red ball",)) 
for sentence in sentences:
    sentence    

结果是:

Tree('ROOT', [Tree('S', [Tree('VP', [Tree('VB', ['Bring']),
Tree('NP', [Tree('DT', ['a']), Tree('NN', ['red'])]), Tree('NP',
[Tree('NN', ['ball'])])]), Tree('.', ['.'])])])

除了上图之外,我如何使用Stanford解析器获取类型化的依赖项?类似的东西:

  1. root(ROOT-0,bring-1)
  2. iobj(带-1,我-2)
  3. det(ball-5,a-3)
  4. amod(ball-5,red-4)
  5. dobj(bring-1,ball-5)

1 个答案:

答案 0 :(得分:5)

NLTK的StanfordParser模块(目前)没有将树包装到Stanford Dependencies转换代码中。您可以使用我的库PyStanfordDependencies,它包含依赖项转换器。

如果问题的代码段中的nltk_treesentence,则可行:

#!/usr/bin/python3
import StanfordDependencies

# Use str() to convert the NLTK tree to Penn Treebank format
penn_treebank_tree = str(nltk_tree) 

sd = StanfordDependencies.get_instance(jar_filename='point to Stanford Parser JAR file')
converted_tree = sd.convert_tree(penn_treebank_tree)

# Print Typed Dependencies
for node in converted_tree:
    print('{}({}-{},{}-{})'.format(
            node.deprel,
            converted_tree[node.head - 1].form if node.head != 0 else 'ROOT',
            node.head,
            node.form,
            node.index))