JL ia dZddlZ ddlmZddlmZddlm Z ddl m Z ddl m Z dd d d d d dddZGddZdZdgZedk(reyy#e$r ddlmZYNwxYw)a{ Pretty-printing of discontinuous trees. Adapted from the disco-dop project, by Andreas van Cranenburgh. https://github.com/andreasvc/disco-dop Interesting reference (not used for this code): T. Eschbach et al., Orth. Hypergraph Drawing, Journal of Graph Algorithms and Applications, 10(2) 141--157 (2006)149. https://jgaa.info/accepted/2006/EschbachGuentherBecker2006.10.2.pdf N)escape) defaultdict) itemgetter)Tree) OrderedDict !"#$%)blackredgreenyellowbluemagentacyanwhitecVeZdZdZd dZdZdZedZ d dZ d dZ y) TreePrettyPrintera Pretty-print a tree in text format, either as ASCII or Unicode. The tree can be a normal tree, or discontinuous. ``TreePrettyPrinter(tree, sentence=None, highlight=())`` creates an object from which different visualizations can be created. :param tree: a Tree object. :param sentence: a list of words (strings). If `sentence` is given, `tree` must contain integers as leaves, which are taken as indices in `sentence`. Using this you can display a discontinuous tree. :param highlight: Optionally, a sequence of Tree objects in `tree` which should be highlighted. Has the effect of only applying colors to nodes in this sequence (nodes should be given as Tree objects, terminals as indices). >>> from nltk.tree import Tree >>> tree = Tree.fromstring('(S (NP Mary) (VP walks))') >>> print(TreePrettyPrinter(tree).text()) ... # doctest: +NORMALIZE_WHITESPACE S ____|____ NP VP | | Mary walks Nc|;|j}|rKtd|jDr+td|Dr|Dcgc] }t|}}n|j d}g}|jD]}t |dk(r,|j t ||j d=td|DsPt|D][\}}t|trt |||<t|tk(rdj|}|j d|z]|j|||\|_|_|_|_ycc}w)Nc38K|]}t|dkDywrNlen.0as ]/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/nltk/tree/prettyprinter.py z-TreePrettyPrinter.__init__..Ns.Os;q 1c*;Trc3>K|]}t|t ywr%)r'r)r bs r"r#z-TreePrettyPrinter.__init__..[s@At!44@s/z%s)leavesallsubtreesstrcopyrappendany enumerater'rtypetuplejoin nodecoordsnodescoordsedges highlight)selftreesentencer<r-r!nr+s r"__init__zTreePrettyPrinter.__init__Is2  [[]F)rr9rDs r"__repr__zTreePrettyPrinter.__repr__is2S_DDrFc (  ! !fd}fdj}td|Ds tdt|tt |k7r tdtfd|Ds(tdtjfzd\!j d j D]}|jd d t j}ttt|d z}tt}dgt zzgi} t|D cic]\} }||  c}} jD chc]\}} |r||vs| } }} t|d z D cic]} | g} } g} |D]N}|}t|t r&| ||j#z j%|>| j%|P| D]} | | jfd | jt |}| D]t' z}d|Jd||fd|<| <| d| <| j)|j+|ddj-d|ft/| d D]W} | | }t}j%dDcgc] }|dfvr!ndc}|D]| |d z kr|rt1|t3d  \}}ddDchc]!}|d|D]}t|t4r|dd#c}}ddDchc]!}||dD]}t|t4r|dd#c}}zrj-|||\}}|j+|D] \}}||<||<| <tdkDs|ddj-||fZt|dk(sJt tzd z ddD]!t7fdDrD]}|=#t9Dcgc]}t!fd|Ds|c}i}tD]9\} }t| D]#\}t|t&s|dk\s| f||<%;t/d| j;Dfd }t=}t9|D]'}t|D]\}}||||fz<)| ||| fScc}} wcc} }wcc} wcc}wcc}}wcc}}wcc}w)a Produce coordinates of nodes on a grid. Objective: - Produce coordinates for a non-overlapping placement of nodes and horizontal lines. - Order edges so that crossing edges cross a minimal number of previous horizontal lines (never vertical lines). Approach: - bottom up level order traversal (start at terminals) - at each level, identify nodes which cannot be on the same row - identify nodes which cannot be in the same column - place nodes into a grid at (row, column) - order child-parent edges with crossing edges last Coordinates are (row, column); the origin (0, 0) is at the top left; the root node is on row 0. Coordinates do not consider the size of a node (which depends on font, &c), so the width of a column of the grid should be automatically determined by the element with the greatest width in that column. Alternatively, the integer coordinates could be converted to coordinates in which the distances between adjacent nodes are non-uniform. Produces tuple (nodes, coords, edges, highlighted) where: - nodes[id]: Tree object for the node with this integer id - coords[id]: (n, m) coordinate where to draw node with id in the grid - edges[id]: parent id of node with this id (ordered dictionary) - highlighted: set of ids that should be highlighted c 6||Dcgc]\}}| }}}t|t|}}|j} t| zt | z} ||kr$|| cxkr|ksnt|t |z} t|t|z dzkDr| | zz} ||kr|| cxkr|ksn| z } |dk(r t |}t |t |dzD]} | t |k(r*|j |dDcgc] }|dfvrndc}|| } t ||dk(r| tt||dfcStfd| t|t|dzDst D]\} | | zx}}||kDs||ks||kr|| |||vr| |fccS||kDr|| |||vr| |fccS|z }|z}||kDrB||krH^ td|||fzcc}}wcc}w)a Find vacant row, column index for node ``m``. Iterate over current rows for this level (try lowest first) and look for cell between first and last child of this node, add new row to level if no free row available. rNc34K|]}|duxs|k(ywr%r r!vertlines r"r#zATreePrettyPrinter.nodecoords..findcell..s)I.h.z3could not find a free cell for: %s %smin=%d; max=%d) minmaxr-sumrranger2nextiterr. ValueError)mmatrix startoflevelchildren_r! candidatesminidxmaxidxr-centerrowidxrowr@ijcorner dumpmatrixidsscaler>rQs r"findcellz.TreePrettyPrinter.nodecoords..findcellsm)1 41!4J4 _c*oFF!W^^%FS[(CK7Fv'?'?ZC O;:Z01u9<&5.(F?6F+CV+CeOF1v{"6{  c&kAo> 'S[(MM&,BZ !)*&$(?HTI Vnx{#q(!4Xa[(9#:1#=== Z3z?Q3FG #5\ ' & *A&jAJ 6z &vq 1 9Q*_'-qy 0!"V &vq 1 9Q*_'-qy 0JAJA &jAJ '! ':!$(GVVZ\#JK S5s HH cFdjdtDS)z,Dump matrix contents for debugging purposes. c3\K|]$\}}d|djd|Dfz&yw)z%2d: %s c3,K|] }d|zddyw)z%2rNrKrO)r res r"r#zMTreePrettyPrinter.nodecoords..dumpmatrix...s(FQ%!)Ra(FsNr7)r r@rds r"r#zCTreePrettyPrinter.nodecoords..dumpmatrix..s5AsQ(F#(F FGGs*,)r7r4)r[sr"rhz0TreePrettyPrinter.nodecoords..dumpmatrixs&99'/ rFc3<K|]}t|tywr%r&)r r@s r"r#z/TreePrettyPrinter.nodecoords..s6!:a%6r)z#All leaves must be integer indices.z Indices must occur at most once.c3PK|]}d|cxkxrtkncywrr)r r@r?s r"r#z/TreePrettyPrinter.nodecoords..s":a1)CM)):#&z`All leaves must be in the interval 0..n with n=len(sentence) tokens: %d indices: %r sentence: %s)rMTcXt|trt|jS|Sr%)r'rrSr-)r@s r"z.TreePrettyPrinter.nodecoords..sJq$4GQXXZQrF)keyrKrLNcvt|jt|jz Sr%)rTr-rS)r@r>s r"rwz.TreePrettyPrinter.nodecoords..s,T!W^^-=)>T!W^^EUAV)VrFrz...rMreversec3PK|]}t|ttfywr%)r'rr()r rdrZs r"r#z/TreePrettyPrinter.nodecoords..+s I3z#a&4+6Irtc34K|]}|duxs|k(ywr%rOrPs r"r#z/TreePrettyPrinter.nodecoords..3s!?aqDy1AM1?rRc3.K|] }|D]}|ywr%rO)r levelr!s r"r#z/TreePrettyPrinter.nodecoords..?s ;5U ;Q ;Q ;sc|ddvS)NrMrO)r!crosseds r"rwz.TreePrettyPrinter.nodecoords..@s!CR&G+rF)r-r.rYrsetr1r/sort treepositionsrTmaprr4itemsrVr'rheightr2r(discardremoveaddsortedrSrr6r3reversedvaluesr)"r>r?r<rkr-r! positionsmaxdepth childcolsr9r@highlighted_nodeslevels terminalsnodere nodesatdepthr\r^pivotrdrcxr:r;rfrgrrhrirZr[rjrQs"`` @@@@@@@@r"r8zTreePrettyPrinter.nodecoordslsKH3 3 j 6v66BC C v;#c&k* *?@ @:6::#&)(mT[[]H%MN  "&yy PA FFNF O P%&&( s3 *+a/$ &CME123 )) 451q!t5))+ !QY$q'Y:NA  "'x!|!45A!R%55  $A7D$%x$++-/077:  #  $ XA 1INNVN W X N  *ADG u$A!9Q<' =&)A,1)= ='q6F1IaL$T!W-E#a&MSV}$ %c!f !))#a&1   Q  af  ! !1a& ) *-# 7A!!9Lv;L MMHNr S1Qvtn4$>S " 7x!|# ! "9Q s0 U-:U3 U3# U9U> &V &V Vc  0|dk(rd}|r#d} d} d} d} | dz| z}| dz| z}| d z| z}d }nd } d x} } d } d| z}dx}}d}| fd}g}tt}td}td}d}i}i}tt}i}tjd| dz | | fz}|j D]}|j |\}}||||<t||}t|j |tr|j |jn|j |} |rt| |kDr| d||z} | r.t| | kDr |jd| j} | jd} t||t| ||<t||ttt| ||<| ||<||j vr%|j |}!||!j#||ft%|j'|!||||!<t|j'|!||||!<t)|dD]}t+||D"#cgc]0}"t+|dzD#cgc]}#dj-||#c}#2}$}"}#t+|dzD#cgc]}#dj-||#}%}#||D]9}#|||#}&|j |&}'||&}(t|'tr|&|vr||&||&kr||&||&c0})|0dzdzdz ||)dz}*}d |z| zj/|0| |%0<| d |*zzj1||)| |%|)<t+||&dz||&D]]00|#k(rt30fd||&Dr|}+n$0|#k(r|}+nt30fd||&Dr|}+n| }+|+j-|0| |%0<_n||%|#|%|#<|(Dcgc]}|j-||#}(}t|'tr|n|},t|'tr!|'jj5d r|},|r?|(Dcgc]}t7|d!"}(}|&|j8vrB|(Dcgc] }d#|,d$|d% }(}n+|r)|&|j8vr|(Dcgc]}d&t:|,|fz}(}t+||D]7}-|-t|(kr|(|-n||&r| nd j-||#d |$|-|#<9<|t|k7r|j j=D]e\}&\}.}#|&dkDs|j |j |&d|cxkr|.ks8n;||%|#|%|#<|#||vsQ|$D]}/||/|#|/|#<gt?|%|%ddd gzD*cgc]\}}*||d'd k7r|d'n|*dzz}%}}*|jAdjC|%|jEfd(tG|$DdjCtG|dzScc}#wcc}#}"wcc}#wcc}wcc}wcc}wcc}wcc}*}w))a :return: ASCII art for a discontinuous tree. :param unicodelines: whether to use Unicode line drawing characters instead of plain (7-bit) ASCII. :param html: whether to wrap output in html code (default plain text). :param ansi: whether to produce colors with ANSI escape sequences (only effective when html==False). :param leafcolor, nodecolor: specify colors of leaves and phrasal nodes; effective when either html or ansi is True. :param abbreviate: if True, abbreviate labels longer than 5 characters. If integer, abbreviate labels longer than `abbr` characters. :param maxwidth: maximum number of characters before a label starts to wrap; pass None to disable. Tu─u┌u┐u │ u┬u┴u┼u…r^roz | z_|_.ct|t|dzz t|dzz dz }t|}t||||t|zdj|S)z5Overwrite center of this cell with a vertical branch.rKrL)rlistr7)currsplitllsts r" crosscellz)TreePrettyPrinter.text..crosscellysYXCA -A! ;a?Fs)C,0GC#a& )773< rFcy)NrrOrOrFr"rwz(TreePrettyPrinter.text..rFcy)NrLrOrOrFr"rwz(TreePrettyPrinter.text..rrFrz(.{%d,%d}\b\W*|.{%d})Nz\1\nrmrzrLrrKc3.K|] \}}|k(ywr%rOr r^r!res r"r#z)TreePrettyPrinter.text..s/P41aQ/Pc3.K|] \}}|k(ywr%rOrs r"r#z)TreePrettyPrinter.text..s$E1Q!V$Er-Fquotez zz [%d;1m%srMc3FK|]}dzj|yw)roNrq)r noderownodedists r"r#z)TreePrettyPrinter.text..s%3:x%%g.s!)$rdictrrecompiler9r:rTr'rlabelrsubstripsplitrr;rrSgetrrVrbljustrjustr3 startswithrr< ANSICOLORrzipr2r7extendr)1r=r unicodelineshtmlansi nodecolor leafcolor funccolor abbreviatemaxwidthhorzline leftcorner rightcornerrQteebottomcrossellipsisrresultr[ maxnodewith maxnodeheightmaxcol minchildcol maxchildcolrlabelswraprer!rdcolumnrparentr^colnoderows branchrowr@rrCrfr+linecolorrchildrowrres1 ` @r"rCzTreePrettyPrinter.textKs6  J H!J"K!HX%0C(83Fx'(2EHH'* *JHh,C" "EFH% T"!), #I.   $  %Ax(J J  OA++a.KC"#F3K (FdjjmT2 1 ##%ZZ]  c%j:5kz*X5CJ1 7E288:KK%E!$]3%7U!DM# "%k&&93s3;O"PK F1I "ZZ]F f  ! !3- 0"%kooff&Ev"NK "%kooff&Ev"NK - O0&$/E C}S129>fqj8IJ;s+,JHAFfqj@QR;s#34RIRc{. 3K$zz!}aydD)K'KN[^,K*1~{1~1 +A 2q81Q1),qJ(>'E'E'NH( ! )4sQw(?'F'F'NH( ! "'{1~'9;q>!J QA CxC/P9Q)B #<@AqS!12AA%/d%; dD)djjl.E.Ec.J%E<@AqF1E2ADADNN*KOPa,ugQqc APPa4>>1SWXa1Yu5Eq4IIXDX}S12 A s4y=Q*3A,hCGG',cQK$ K. bc&k!*.++*;*;*=G&A#1uTZZ]!;A!>!Ox!O)29S>)B #fSk1+3G/8/F G G!$Iy}u/D E11R5CFx>P GE Lyy&)*T11IKS8B BPX(s<X83X3X8&X>,YY<Y #Y+"Y3X8c d}d}d}dx}}td|jjD} td|jjD} d| dz| d z| | | |zd|zz| |zd|zzfzg} tt} |j D]&} | s| |j | j| (|j D]}| |s |j|\}}||z}||z}||z }|||d zzz }| |Dcgc]}|j|d }}||t|zz}||t|zz}| jd ||||fz| jd |||||dzz fz|j jD]h\}}|j|\}}||z}|||d zzz }|j|\}}||z}||z}||z }|||z z }| d ||||dzfzd ||||fzgz } j|jjD]\} \}}|j | }||z|z}||z|z}| |jvrFt|tr|n|}t|tr$|jjdr|}nd}| d||||t!t|tr|jn|dfzgz } | dgz } dj#| Scc}w)z8 :return: SVG representation of a tree. (c3&K|] \}}| ywr%rO)r r^rs r"r#z(TreePrettyPrinter.svg..s;FAsC;c3&K|] \}}| ywr%rO)r rdr^s r"r#z(TreePrettyPrinter.svg..srg@rKrLzS zT rrrzV %sFrzrm)rTr:rrrr9r;rrSr2rr<r'rrrrr7)r=rrrfontsizehscalevscalehstartvstartwidthrrr]r@ryrcchildxxminxmaxchildrr^childyrdrrs r"svgzTreePrettyPrinter.svgs;dkk&8&8&:;;;;t$DAq KA KA KA (a-' 'A19$@Adkk!nQ'@F@FS[00DFS[00D MM*-11dA,>?  MM*-.1a(a-6G,HI  *"ZZ--/ ME6;;v&DAq KA (a-' 'A![[/NFF f F f F f F fx' 'F +.4ffa!e-LM+.4ffa-HI F "!% 1 1 3  A}V::a=D&(Af v%ADNN"%/d%; dD)djjl.E.Ec.J%E <(24(> DPU    F 0 8*yy  oAsK5)NrO) rLFFFrrrN)rrr) __name__ __module__ __qualname____doc__rArErH staticmethodr8rCrrOrFr"rr-sX6 :E\7\7@  `2D\!rFrcDdd}ddlm}dD]!}|j|}|||dd#ttd tt j d t jd t }d j}|d||d y)zDo some tree drawing tests.Nc vttdj|dj|xs|jt|tt ||} t|j d||d|y#t tf$r!t|j dddd|YywxYw)Nz{}: "{}"ro)rrFrO)printformatr7r-rrCUnicodeDecodeErrorUnicodeEncodeError)r@r>r?rxargsdrawtrees r" print_treeztest..print_treeOs  j388H,E #FGH d  $T84 J -(--FTFF G"$67 J -(--HUH%H I Js)B-B87B8r)treebank)rii7i izrK)rrzASCII version:)rz(top (punct 8) (smain (noun 0) (verb 1) (inf (verb 5) (inf (verb 6) (conj (inf (pp (prep 2) (np (det 3) (noun 4))) (verb 7)) (inf (verb 9)) (vg 10) (inf (verb 11)))))) (punct 12))) read_leafzDZe had met haar moeder kunnen gaan winkelen , zwemmen of terrassen .zDiscontinuous tree)NT) nltk.corpusr parsed_sentsrrrCr fromstringr(r)rrr@r>r?s r"testr Ls J% (4$$&q)1dQ34 G  D ! & & & 23 ?? 2  D ""'%' #T8a@rF__main__)rrrr ImportErrorcgi collectionsroperatorrnltk.tree.treer nltk.utilrrrr __all__rrOrFr"rs  $!      \!\!~!AH   zFusA AA