For Programmers: Free Programming Magazines  


Home > Archive > Scheme > December 2005 > Printing cons-trees on character devices?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Printing cons-trees on character devices?
A Little Scheme Monster

2005-12-15, 7:02 pm

Hi,

I have to deal a lot with cons-trees of the following structure:

'((S) ((NP CoD) ((N PN) (Alice)) ((Co) (and)) ((N PN) (Bob))) ((VP)
((V2) (meet))) ((NP) ((N PN) (Bob))))

non-terminal nodes have one or more labels, terminal nodes can be an
atom like in the above example, but can also a list as in

'((S)
((SUBJ NP) ((Det) ((every _1))) ((N) ((sailor _C? _1))))
((VP) ((V2) ((loves _C? _SUBJ? _DO?)))
((DO NP) ((Det) (some _1)) ((N) (woman _C? _1))))))

I have some vague memory that I've once had seen some code to display
such trees in a really nice fashion as real trees (not just
pretty-printed lists), where ASCII symbols - + and | were used to draw
the edges---just like e.g. PC-PATR draws parse trees.

Does anyone have the code?

Pascal Bourguignon

2005-12-15, 7:03 pm

"A Little Scheme Monster" <john@peppermind.com> writes:

> Hi,
>
> I have to deal a lot with cons-trees of the following structure:
>
> '((S) ((NP CoD) ((N PN) (Alice)) ((Co) (and)) ((N PN) (Bob))) ((VP)
> ((V2) (meet))) ((NP) ((N PN) (Bob))))
>
> non-terminal nodes have one or more labels, terminal nodes can be an
> atom like in the above example, but can also a list as in
>
> '((S)
> ((SUBJ NP) ((Det) ((every _1))) ((N) ((sailor _C? _1))))
> ((VP) ((V2) ((loves _C? _SUBJ? _DO?)))
> ((DO NP) ((Det) (some _1)) ((N) (woman _C? _1))))))
>
> I have some vague memory that I've once had seen some code to display
> such trees in a really nice fashion as real trees (not just
> pretty-printed lists), where ASCII symbols - + and | were used to draw
> the edges---just like e.g. PC-PATR draws parse trees.
>
> Does anyone have the code?


I have it, but not in scheme, in Common Lisp. You can download the code from:

http://www.informatimago.com/develop/lisp/index.html
cvs -z3 -d :pserver:anonymous@cvs.informatimago.com:/usr/local/cvs/public/chrooted-cvs/cvs co common/common-lisp



[3]> (asdf:oos 'asdf:load-op :com.informatimago.common-lisp)
; loading system definition from PACKAGES:COM;INFORMATIMAGO;COMMON-LISP;SYSTEM.ASD into #<PACKAGE ASDF4306>
;; ...
[4]> (use-package :com.informatimago.common-lisp.tree-to-ascii)
T
[5]> (tree-to-ascii '((S) ((NP CoD) ((N PN) (Alice))
((Co) (and)) ((N PN) (Bob)))
((VP) ((V2) (meet)))
((NP) ((N PN) (Bob)))) :boxed t)
" +------+ +-----+
+--|(N PN)|--|ALICE|
| +------+ +-----+
|
+--------+ | +----+ +---+
+--|(NP COD)|--+--|(CO)|--|AND|
| +--------+ | +----+ +---+
| |
+---+ | | +------+ +---+
|(S)|--+ +--|(N PN)|--|BOB|
+---+ | +------+ +---+
|
| +----+ +----+ +----+
+--|(VP)|--|(V2)|--|MEET|
| +----+ +----+ +----+
|
| +----+ +------+ +---+
+--|(NP)|--|(N PN)|--|BOB|
+----+ +------+ +---+
"
[6]> (tree-to-ascii '((S) ((NP CoD) ((N PN) (Alice))
((Co) (and)) ((N PN) (Bob)))
((VP) ((V2) (meet)))
((NP) ((N PN) (Bob)))) :boxed nil)
" +--(N PN)--ALICE
|
+--(NP COD)--+--(CO)--AND
| |
(S)--+ +--(N PN)--BOB
|
+--(VP)--(V2)--MEET
|
+--(NP)--(N PN)--BOB
"
[7]>


[12]> (defun clean-tree (tree)
(cond ((atom tree) tree)
((consp (car tree))
(cons '* (mapcar (function clean-tree) tree)))
(t (mapcar (function clean-tree) tree))))
CLEAN-TREE
[13]> (tree-to-ascii (clean-tree '((S) ((NP CoD) ((N PN) (Alice))
((Co) (and)) ((N PN) (Bob)))
((VP) ((V2) (meet)))
((NP) ((N PN) (Bob))))) :boxed nil)
" +--S
|
| +--NP--COD
| |
| | +--N--PN
| +--*--+
| | +--ALICE
| |
+--*--+ +--CO
| +--*--+
| | +--AND
| |
| | +--N--PN
*--+ +--*--+
| +--BOB
|
| +--VP
| |
+--*--+ +--V2
| +--*--+
| +--MEET
|
| +--NP
| |
+--*--+ +--N--PN
+--*--+
+--BOB
"
[14]> (tree-to-ascii (clean-tree '((S) ((NP CoD) ((N PN) (Alice))
((Co) (and)) ((N PN) (Bob)))
((VP) ((V2) (meet)))
((NP) ((N PN) (Bob))))) :boxed t)
" +-+
+--|S|
| +-+
|
| +--+ +---+
| +--|NP|--|COD|
| | +--+ +---+
| |
| | +-+ +--+
| | +--|N|--|PN|
| | +-+ | +-+ +--+
| +--|*|--+
| | +-+ | +-----+
| | +--|ALICE|
| | +-----+
| |
| +-+ | +--+
+--|*|--+ +--|CO|
| +-+ | +-+ | +--+
| +--|*|--+
| | +-+ | +---+
| | +--|AND|
| | +---+
| |
| | +-+ +--+
| | +--|N|--|PN|
+-+ | | +-+ | +-+ +--+
|*|--+ +--|*|--+
+-+ | +-+ | +---+
| +--|BOB|
| +---+
|
| +--+
| +--|VP|
| | +--+
| |
| +-+ | +--+
+--|*|--+ +--|V2|
| +-+ | +-+ | +--+
| +--|*|--+
| +-+ | +----+
| +--|MEET|
| +----+
|
| +--+
| +--|NP|
| | +--+
| |
| +-+ | +-+ +--+
+--|*|--+ +--|N|--|PN|
+-+ | +-+ | +-+ +--+
+--|*|--+
+-+ | +---+
+--|BOB|
+---+
"


--
__Pascal Bourguignon__ http://www.informatimago.com/
Until real software engineering is developed, the next best practice
is to develop with a dynamic system that has extreme late binding in
all aspects. The first system to really do this in an important way
is Lisp. -- Alan Kay
A Little Scheme Monster

2005-12-16, 3:59 am

I'm afraid that converting it from CL to Scheme might take almost as
long as writing it from scratch, but anyway thanks a lot!

Best regards,

Eric

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com