ó j;sSc@s2ddlZddlZdefd„ƒYZdS(iÿÿÿÿNtTreecBsjeZd%d„Zd„Zd„Zd„Zd„Zd„Zd„Z d„Z d„Z d „Z d „Z d „Zd „Zd „Zd„Zd„Zd„Zdd„Zd%d„Zd„Zd„Zd„Zed„ƒZed„Zd%d„Zedd%d%d%d%ed„ƒZed„ƒZd„Z d„Z!dd d!ded"„Z"d#„Z#d$„Z$RS(&cCs³|dkrkt|tƒs7tdt|ƒjƒ‚nt|ƒj|ƒ}tj||ƒ|j |_ nDt|tƒr–tdt|ƒjƒ‚ntj||ƒ||_ dS(Ns;%s: Expected a node value and child list or a single strings.%s() argument 2 should be a list, not a string( tNonet isinstancet basestringt TypeErrorttypet__name__tparsetlistt__init__tnode(tselft node_or_strtchildrenttree((scorefgraph/resources/tree.pyR ]s cCs5t|tƒstS|j|jko4tj||ƒS(N(RRtFalseR Rt__eq__(R tother((scorefgraph/resources/tree.pyRpscCs ||k S(N((R R((scorefgraph/resources/tree.pyt__ne__sscCs5t|tƒstS|j|jkp4tj||ƒS(N(RRRR Rt__lt__(R R((scorefgraph/resources/tree.pyRuscCs5t|tƒstS|j|jkp4tj||ƒS(N(RRRR Rt__le__(R R((scorefgraph/resources/tree.pyRxscCs5t|tƒstS|j|jkp4tj||ƒS(N(RRtTrueR Rt__gt__(R R((scorefgraph/resources/tree.pyR{scCs5t|tƒstS|j|jkp4tj||ƒS(N(RRRR Rt__ge__(R R((scorefgraph/resources/tree.pyR~scCstdƒ‚dS(Ns$Tree does not support multiplication(R(R tv((scorefgraph/resources/tree.pyt__mul__†scCstdƒ‚dS(Ns$Tree does not support multiplication(R(R R((scorefgraph/resources/tree.pyt__rmul__ˆscCstdƒ‚dS(NsTree does not support addition(R(R R((scorefgraph/resources/tree.pyt__add__ŠscCstdƒ‚dS(NsTree does not support addition(R(R R((scorefgraph/resources/tree.pyt__radd__ŒscCs±t|ttfƒr%tj||ƒSt|ttfƒr…t|ƒdkrP|St|ƒdkrn||dS||d|dSn(tdt|ƒj t|ƒj fƒ‚dS(Niis#%s indices must be integers, not %s( RtinttsliceRt __getitem__ttupletlenRRR(R tindex((scorefgraph/resources/tree.pyR“s cCsÆt|ttfƒr(tj|||ƒSt|ttfƒršt|ƒdkr^tdƒ‚qÂt|ƒdkr|||dst postorder(spreorderR0((R4R0((R)t enumerateRRt treepositionsR'(R tordert positionsR*tchildpos((R3scorefgraph/resources/tree.pyR6s    ccsa| s||ƒr|Vnx?|D]7}t|tƒr"x|j|ƒD] }|VqGWq"q"WdS(N(RRtsubtrees(R tfilterR*tsubtree((scorefgraph/resources/tree.pyR:s  cCsVg}xI|D]A}t|tƒr8|j|jƒƒq |j||jfƒq W|S(N(RRR'tposR)R (R R=R*((scorefgraph/resources/tree.pyR=Gs  cCsÊ|dkrtdƒ‚n|dfg}x|r¹|jƒ\}}t|tƒsq|dkrd|S|d8}q-xBtt|ƒdddƒD]$}|j||||ffƒqŽWq-Wtdƒ‚dS(Nisindex must be non-negativeiiÿÿÿÿs-index must be less than or equal to len(self)((R$tpopRRtrangeR!R)(R R"tstackR%ttreeposR3((scorefgraph/resources/tree.pytleaf_treeposition[s    #&cCsŒ||krtdƒ‚n|j|ƒ}|j|dƒ}xHtt|ƒƒD]4}|t|ƒks|||||krP|| SqPW|S(Nsend must be greater than starti(t ValueErrorRBR?R!(R tstarttendt start_treepost end_treeposR3((scorefgraph/resources/tree.pyttreeposition_spanning_leavesrs & cCsIt|tƒrAg|D]}|j|ƒ^q}||j|ƒS|SdS(N(RRtconvertR (tclsRR*R ((scorefgraph/resources/tree.pyRIÓs "cCs3|st|ƒ|j|ƒSt|ƒj|ƒSdS(N(RR RI(R tdeep((scorefgraph/resources/tree.pytcopyãscCs‡|jƒ}|dkr*|j|ƒ}nO|jdtƒ}x+|jdƒD]}|||ƒ||R (RJtstbracketst parse_nodet parse_leaft node_patternt leaf_patterntremove_empty_top_bracketingtopen_btclose_bt open_patternt close_patternttoken_reR@tmatchttokenR R R((scorefgraph/resources/tree.pyRùsT5"     ,  ! ' cCsü|dkr"t|ƒd}}n|jƒ|jƒ}}d|j||d |f}|jddƒjddƒ}|}t|ƒ|dkr¦||d d}n|dkrÍd||d}d }n|d d|dd |f7}t|ƒ‚dS(Ns end-of-strings1%s.parse(): expected %r but got %r %sat index %d.t i s s i s...i s %s"%s" %s^iis s (R!RDRYRtreplaceRC(RJR\Rht expectingR=Ritmsgtoffset((scorefgraph/resources/tree.pyRZgs   cCs6djd„|Dƒƒ}dt|ƒj|j|fS(Ns, css|]}t|ƒVqdS(N(trepr(R1tc((scorefgraph/resources/tree.pys ss %s(%r, [%s])(tjoinRRR (R tchildstr((scorefgraph/resources/tree.pyt__repr__ŒscCs |jƒS(N(tpprint(R ((scorefgraph/resources/tree.pyt__str__siFiRSc Csd|j|||ƒ}t|ƒ||kr/|St|jtƒr^d|d|j|f}nd|d|j|f}xÝ|D]Õ}t|tƒrÊ|dd|d|j||d|||ƒ7}qt|tƒrÿ|dd|ddj|ƒ7}qt|tƒr6| r6|dd|dd|7}q|dd|dd |7}qW||d S( Ns%s%s%sis%s%r%ss Rjit/s%ss%ri( t _pprint_flatR!RR RRRtR Rq(R tmargintindenttnodeseptparenstquotesR\R*((scorefgraph/resources/tree.pyRt“s  "&!"c Cs d|jdddddd ƒS( Ns\Tree RyiRzRSR{s[.s ](s[.s ](Rt(R ((scorefgraph/resources/tree.pytpprint_latex_qtree¸scCsg}xŸ|D]—}t|tƒrA|j|j|||ƒƒq t|tƒri|jdj|ƒƒq t|tƒr“| r“|jd|ƒq |jd|ƒq Wt|jtƒråd|d|j|tj|ƒ|dfSd|d|j|tj|ƒ|dfSdS(NRvs%ss%rs %s%s%s %s%siis %s%r%s %s%s( RRR)RwR RqRR tstring(R RzR{R|t childstrsR*((scorefgraph/resources/tree.pyRwÌs N(%Rt __module__RR RRRRRRRRRRRR#R&R(R+R-R6R:R=RBRHt classmethodRIRRLRRRRZRsRuRtR}Rw(((scorefgraph/resources/tree.pyRsF@                   /   a   k%  % (RTR~RR(((scorefgraph/resources/tree.pyts