Б. Ертөнц
Б. Ертөнц

Reputation: 13

Prolog DCG parsing display

I just going to display parse tree. i have a sentence that already parsed and now only i need is display it root by node.here is displaying section

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).

and i want to display this sentence

s(np(d(the),n(boy),rel(rpn(who),vp(iv(sits)))),vp(tv(reads),np(d(a),n(book))))

and my goal is

new(T,tree(text('s'))) 
new(S1, node(text('np'))), 
new(S2, node(text('vp'))), etc,. 

Is there any easy solution???

Upvotes: 1

Views: 197

Answers (1)

CapelliC
CapelliC

Reputation: 60034

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))))).

yields

enter image description here

edit

The code can be simplified, and tree direction is better passed as parameter:

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, ...).

Upvotes: 1

Related Questions