使用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解析器获取类型化的依赖项?类似的东西:
答案 0 :(得分:5)
NLTK的StanfordParser模块(目前)没有将树包装到Stanford Dependencies转换代码中。您可以使用我的库PyStanfordDependencies,它包含依赖项转换器。
如果问题的代码段中的nltk_tree
为sentence
,则可行:
#!/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))