ó c;sSc @s5ddlZddlZdefd„ƒYZdefd„ƒYZdefd„ƒYZdefd „ƒYZd efd „ƒYZd eefd „ƒYZdeefd„ƒYZ d„Z d„Z d„Z d„Z ddddddddd d dg Zedkr1ddlZejdejƒndS(i’’’’NtTreecBsÄeZd/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„Zd„Zdd/dddd„Zedddd„Zeedd„Zed „ƒZ ed!„Z!d"„Z"d/d#„Z#ed$d/d/d/d/ed%„ƒZ$ed&„ƒZ%d'„Z&d(„Z'd)„Z(d*dd+d$ed,„Z)d-„Z*d.„Z+RS(0cCs³|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((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyR Zs cCs5t|tƒstS|j|jko4tj||ƒS(N(RRtFalseR Rt__eq__(R tother((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyRmscCs ||k S(N((R R((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyt__ne__pscCs5t|tƒstS|j|jkp4tj||ƒS(N(RRRR Rt__lt__(R R((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyRrscCs5t|tƒstS|j|jkp4tj||ƒS(N(RRRR Rt__le__(R R((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyRuscCs5t|tƒstS|j|jkp4tj||ƒS(N(RRtTrueR Rt__gt__(R R((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyRxscCs5t|tƒstS|j|jkp4tj||ƒS(N(RRRR Rt__ge__(R R((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyR{scCstdƒ‚dS(Ns$Tree does not support multiplication(R(R tv((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyt__mul__ƒscCstdƒ‚dS(Ns$Tree does not support multiplication(R(R R((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyt__rmul__…scCstdƒ‚dS(NsTree does not support addition(R(R R((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyt__add__‡scCstdƒ‚dS(NsTree does not support addition(R(R R((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/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((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyRs cCsĘt|ttfƒr(tj|||ƒSt|ttfƒršt|ƒdkr^tdƒ‚qĀt|ƒdkr|||dst postorder(R/R0((R4R0((R)t enumerateRRt treepositionsR'(R tordert positionsR*tchildpos((R3sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyR6żs    ccsa| s||ƒr|Vnx?|D]7}t|tƒr"x|j|ƒD] }|VqGWq"q"WdS(N(RRtsubtrees(R tfilterR*tsubtree((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyR:s  cCsvt|jtƒstd‚ntt|jƒt|ƒƒg}x0|D](}t|tƒrF||jƒ7}qFqFW|S(NsPProductions can only be generated from trees having node labels that are strings( RR RRt Productiont Nonterminalt _child_namesRt productions(R tprodsR*((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyR@-s ! cCsVg}xI|D]A}t|tƒr8|j|jƒƒq |j||jfƒq W|S(N(RRR'tposR)R (R RBR*((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyRBDs  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((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pytleaf_treepositionXs    #&cCsŒ||krtdƒ‚n|j|ƒ}|j|dƒ}xHtt|ƒƒD]4}|t|ƒks|||||krP|| SqPW|S(Nsend must be greater than starti(t ValueErrorRGRDR!(R tstarttendt start_treepost end_treeposR3((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyttreeposition_spanning_leavesos & trightit|t^cCs-ddlm}|||||||ƒdS(Ni’’’’(tchomsky_normal_form(ttreetransformsRQ(R tfactort horzMarkovt vertMarkovt childChart parentCharRQ((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyRQ…st+cCs*ddlm}||||||ƒdS(Ni’’’’(tun_chomsky_normal_form(RRRY(R t expandUnaryRVRWt unaryCharRY((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyRY scCs'ddlm}|||||ƒdS(Ni’’’’(tcollapse_unary(RRR\(R t collapsePOSt collapseRoottjoinCharR\((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyR\¶scCsIt|tƒrAg|D]}|j|ƒ^q}||j|ƒS|SdS(N(RRtconvertR (tclsRR*R ((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyR`Šs "cCs3|st|ƒ|j|ƒSt|ƒj|ƒSdS(N(RR R`(R tdeep((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pytcopyąscCstS(N(t ImmutableTree(R ((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyt _frozen_classäscCs‡|jƒ}|dkr*|j|ƒ}nO|jdtƒ}x+|jdƒD]}|||ƒ||‰ss %s(%r, [%s])(tjoinRRR (R tchildstr((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyt__repr__ˆscCs |jƒS(N(tpprint(R ((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyt__str__ŒsiFRkc 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 R‚it/s%ss%ri( t _pprint_flatR!RR RRRR RŒ(R tmargintindenttnodeseptparenstquotesRtR*((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyRs  "&!"c Cs d|jdddddd ƒS( Ns\Tree R”iR•RkR–s[.s ](s[.s ](R(R ((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/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(NR‘s%ss%rs %s%s%s %s%siis %s%r%s %s%s( RRR)R’R RŒRR tstring(R R•R–R—t childstrsR*((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyR’Čs N(,Rt __module__RR RRRRRRRRRRRR#R&R(R+R-R6R:R@RBRGRMRQRRYRR\t classmethodR`RcReRjRRrR‰RŽRRR˜R’(((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyRsR@                           k   % RdcBs­eZdd„Zd„Zd„Zd„Zd„Zd„Zd„Z d„Z d„Z dd „Z d „Z d „Zd „Zd „Zd„Zd„ZeeeƒZRS(cCsrtt|ƒj||ƒy"t|jt|ƒfƒ|_Wn0ttfk rmtdt |ƒj ƒ‚nXdS(Ns-%s: node value and children must be immutable( tsuperRdR RfR R t_hashRRHRR(R R R ((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyR Üs "cCstdt|ƒjƒ‚dS(Ns%s may not be modified(RHRR(R R"R%((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyR#ęscCstdt|ƒjƒ‚dS(Ns%s may not be modified(RHRR(R R3tjR%((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyt __setslice__čscCstdt|ƒjƒ‚dS(Ns%s may not be modified(RHRR(R R"((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyR&źscCstdt|ƒjƒ‚dS(Ns%s may not be modified(RHRR(R R3RŸ((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyt __delslice__ģscCstdt|ƒjƒ‚dS(Ns%s may not be modified(RHRR(R R((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyt__iadd__īscCstdt|ƒjƒ‚dS(Ns%s may not be modified(RHRR(R R((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyt__imul__šscCstdt|ƒjƒ‚dS(Ns%s may not be modified(RHRR(R R((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyR)ņscCstdt|ƒjƒ‚dS(Ns%s may not be modified(RHRR(R R((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyR'ōscCstdt|ƒjƒ‚dS(Ns%s may not be modified(RHRR(R R((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyRCöscCstdt|ƒjƒ‚dS(Ns%s may not be modified(RHRR(R R((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pytremoveųscCstdt|ƒjƒ‚dS(Ns%s may not be modified(RHRR(R ((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pytreverseśscCstdt|ƒjƒ‚dS(Ns%s may not be modified(RHRR(R ((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pytsortüscCs|jS(N(Rž(R ((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyt__hash__žscCs|jS(N(t_node(R ((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyt _get_nodescCs8t|dƒr+tdt|ƒjƒ‚n||_dS(NR s%s may not be modified(thasattrRHRRRØ(R R%((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyt _set_nodesN(RR›RR R#R R&R”R¢R£R)R'RCR¤R„R¦R§R©R«tpropertyR (((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyRdŪs"               tAbstractParentedTreecBs˜eZdd„Zed„Zd„Zd„Zd„Zd„Z d„Z d„Z dd „Z d „Z eed ƒr–d „Zd „Zd„ZnRS(cCs°tt|ƒj||ƒ|dk r¬xBt|ƒD]4\}}t|tƒr2|j||dtƒq2q2Wx?t|ƒD].\}}t|tƒrw|j||ƒqwqwWndS(Ntdry_run( RR­R RR5RRt _setparentR(R R R R3R*((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyR ,s cCs tƒ‚dS(N(tNotImplementedError(R R*R"R®((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyRÆ?scCs tƒ‚dS(N(R°(R R*R"((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyt _delparentUs cCs±t|tƒrt||dtƒ\}}}xDt|||ƒD]0}t||tƒr@|j|||ƒq@q@Wtt|ƒj |ƒn t|t ƒr|dkr»|t |ƒ7}n|dkrÖt dƒ‚nt||tƒr|j|||ƒntt|ƒj |ƒn”t|t tfƒr…t |ƒdkrOt dƒ‚q­t |ƒdkro||d=q­||d|d=n(tdt|ƒjt|ƒjfƒ‚dS(Nt allow_stepisindex out of ranges(The tree position () may not be deleted.is#%s indices must be integers, not %s(RRt slice_boundsRtxrangeRR±RR­R&RR!R$RR RRR(R R"RItstoptstepR3((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyR&is,  cCs«t|tƒrHt||dtƒ\}}}t|ttfƒsQt|ƒ}nxJt|ƒD]<\}}t|tƒr^|j||||dtƒq^q^WxDt |||ƒD]0}t||tƒr±|j |||ƒq±q±WxDt|ƒD]6\}}t|tƒrņ|j||||ƒqņqņWt t |ƒj ||ƒn_t|tƒr |dkrv|t|ƒ7}n|dkr‘tdƒ‚n|||kr„dSt|tƒrĒ|j||ƒnt||tƒrń|j |||ƒnt t |ƒj ||ƒnšt|ttfƒrt|ƒdkrCtdƒ‚q§t|ƒdkrf|||dR (RtnamesR*((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyR?s  cCstdƒ‚dS(Ns<Use Tree.parse(s, remove_empty_top_bracketing=True) instead.(t NameError(Rt((sN/home/yorickpeterse/Private/Projects/opener/constituent-parser-nl/core/tree.pyt bracket_parse%scCstjd|ƒ}xŚtt|ƒƒD]Ę}||dkrc||||d||d<||s*  ’’Ć8åu¤  $ G