如何在 Python/Jupyter Notebook 中漂亮地打印或可视化 class 'CoreNLP_pb2.ParseTree' 的对象?

How do I pretty print or visualise an object of class 'CoreNLP_pb2.ParseTree' in Python/Jupyter Notebook?

我在 Jupyter 笔记本中使用 Stanza 的 CoreNLP 客户端对字符串进行选区解析。最终输出以 class 'CoreNLP_pb2.ParseTree'.

对象的形式出现
>>> print type(result)
<class 'CoreNLP_pb2.ParseTree'>

我应该如何以可见的方式打印它?当我直接调用print(result)时,没有输出

您可以将 CoreNLP_pb2.ParseTree 转换为 nltk.tree.Tree 并调用 pretty_print() 以可见的方式打印解析树。

from nltk.tree import Tree
def convert_parse_tree_to_nltk_tree(parse_tree):
    return Tree(parse_tree.value, [get_nltk_tree(child) for child in parse_tree.child]) if parse_tree.child else parse_tree.value

convert_parse_tree_to_nltk_tree(constituency_parse).pretty_print()

结果如下:

                      ROOT                    
                       |                       
                       S                      
        _______________|____________________   
       |                    VP              | 
       |            ________|___            |  
       NP          |            NP          | 
   ____|_____      |    ________|_____      |  
 NNP        NNP   VBZ  DT       JJ    NN    .
  |          |     |   |        |     |     |  
Chris     Manning  is  a       nice person  .