我只是要显示解析树。我有一个已经被解析过的句子,现在我只需要通过node.here显示它根目录。
display_tree :-
sformat(A, 'Display tree ~w', vertical),
new(D, window(A)),
send(D, size, size(350,200)),
new(T, tree(text('Root'))),
send(T, neighbour_gap, 10),
new(S1, node(text('Child1'))),
new(S2, node(text('Child2'))),
send_list(T, son,[S1,S2]),
new(S11, node(text('Grandchild1'))),
new(S12, node(text('Grandchild2'))),
send_list(S1, son, [S11, S12]),
new(S21, node(text('Grandchild3'))),
new(S22, node(text('Grandchild4'))),
send_list(S2, son, [S21, S22]),
send(T, direction, vertical),
send(D, display, T),
send(D, open).我想展示这句话
s(np(d(the),n(boy),rel(rpn(who),vp(iv(sits)))),vp(tv(reads),np(d(a),n(book))))我的目标是
new(T,tree(text('s')))
new(S1, node(text('np'))),
new(S2, node(text('vp'))), etc,. 有什么简单的解决办法吗?
发布于 2016-04-09 17:13:36
show_parse_tree(S) :-
sformat(A, 'Display tree ~w', vertical),
new(D, window(A)),
send(D, size, size(800, 600)),
S =.. [F|Args],
new(T, tree(text(F))),
send(T, neighbour_gap, 10),
send(T, direction, vertical),
maplist(show_parse_tree, Args, Children),
send_list(T, son, Children),
send(D, display, T),
send(D, open).
show_parse_tree(Arg, Child) :-
Arg =.. [F|Args],
new(Child, node(text(F))),
( Args == []
-> true
; maplist(show_parse_tree, Args, Children),
send_list(Child, son, Children)
).
show_parse_tree :-
show_parse_tree(s(np(d(the),n(boy),rel(rpn(who),vp(iv(sits)))),vp(tv(reads),np(d(a),n(book))))).收益率

编辑
代码可以简化,树的方向最好作为参数传递:
show_parse_tree(Direction, SyntaxTree) :-
sformat(A, 'Display tree ~w', Direction),
new(D, window(A)),
send(D, size, size(800, 600)),
new(T, tree),
send(T, neighbour_gap, 10),
send(T, direction, Direction),
show_node(SyntaxTree, Root),
send(T, root, Root),
send(D, display, T),
send(D, open).
show_node(Node, Child) :-
Node =.. [F|Args],
new(Child, node(text(F))),
maplist(show_node, Args, Children),
send_list(Child, son, Children).
show_parse_tree :-
show_parse_tree(vertical, ...).https://stackoverflow.com/questions/36518740
复制相似问题