在问这个问题之前,我先经过了这些question_1,question_2),但这两个都不完全是我的用例
我正在使用nltk tree.draw()方法来获取一个句子的树形可视化,但是我需要对段落中的所有句子都这样做
所以我想将一个段落的所有句子的输出存储在一个文件中,在这里我可以保留表示形式,这将有助于分析这些结构
我想要文件(文本/图像/。)中段落所有句子的树表示形式,以便于分析
有没有办法实现这一目标?
编辑:使用树形视图输出- https://imgur.com/a/DYgv5qh
答案 0 :(得分:1)
基于this answer的答案 来自您链接的almost duplicate question。
TreeView
构造函数可以采用任意数量的树形参数:
from nltk import Tree
from nltk.draw import TreeView
number_of_trees = 14
# number_of_trees identical trees
trees = [Tree.fromstring('(S (NP this tree) (VP (V is) (AdjP pretty)))') for _ in range(number_of_trees)]
TreeView(*trees)._cframe.print_to_file('output.ps')
Result converted to png from ps
旁注:用于绘制树的nltk的替代方法是discodop的treedraw选项。