ó Ÿ Sc @s;dZddlZddlZdefd„ƒYZdefd„ƒYZdefd„ƒYZd efd „ƒYZd efd „ƒYZd eefd„ƒYZ deefd„ƒYZ d„Z d„Z d„Z d„Zdddddddd d d dg Zedkr7ddlZejdejƒndS(sh Class for representing hierarchical language structures, such as syntax trees and morphological trees. i’’’’NtTreecBsŹeZdZd0d„Zd„Zd„Zd„Zd„Zd„Z d„Z d„Z d „Z d „Z d „Zd „Zd „Zd„Zd„Zd„Zd„Zdd„Zd0d„Zd„Zd„Zd„Zd„Zdd0dddd„Zedddd„Zeedd „Ze d!„ƒZ!ed"„Z"d#„Z#d0d$„Z$e d%d0d0d0d0ed&„ƒZ%e d'„ƒZ&d(„Z'd)„Z(d*„Z)d+dd,d%ed-„Z*d.„Z+d/„Z,RS(1sÓ A Tree represents a hierarchical grouping of leaves and subtrees. For example, each constituent in a syntax tree is represented by a single Tree. A tree's children are encoded as a list of leaves and subtrees, where a leaf is a basic (non-tree) value; and a subtree is a nested Tree. >>> from nltk.tree import Tree >>> print Tree(1, [2, Tree(3, [4]), 5]) (1 2 (3 4) 5) >>> vp = Tree('VP', [Tree('V', ['saw']), ... Tree('NP', ['him'])]) >>> s = Tree('S', [Tree('NP', ['I']), vp]) >>> print s (S (NP I) (VP (V saw) (NP him))) >>> print s[1] (VP (V saw) (NP him)) >>> print s[1,1] (NP him) >>> t = Tree("(S (NP I) (VP (V saw) (NP him)))") >>> s == t True >>> t[1][1].node = "X" >>> print t (S (NP I) (VP (V saw) (X him))) >>> t[0], t[1,1] = t[1,1], t[0] >>> print t (S (X him) (VP (V saw) (NP I))) The length of a tree is the number of children it has. >>> len(t) 2 Any other properties that a Tree defines are known as node properties, and are used to add information about individual hierarchical groupings. For example, syntax trees use a NODE property to label syntactic constituents with phrase tags, such as "NP" and "VP". Several Tree methods use "tree positions" to specify children or descendants of a tree. Tree positions are defined as follows: - The tree position *i* specifies a Tree's *i*\ th child. - The tree position ``()`` specifies the Tree itself. - If *p* is the tree position of descendant *d*, then *p+i* specifies the *i*\ th child of *d*. I.e., every tree position is either a single index *i*, specifying ``tree[i]``; or a sequence *i1, i2, ..., iN*, specifying ``tree[i1][i2]...[iN]``. Construct a new tree. This constructor can be called in one of two ways: - ``Tree(node, children)`` constructs a new tree with the specified node value and list of children. - ``Tree(s)`` constructs a new tree by parsing the string ``s``. It is equivalent to calling the class method ``Tree.parse(s)``. 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((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyR Zs cCs5t|tƒstS|j|jko4tj||ƒS(N(RRtFalseR Rt__eq__(R tother((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyRmscCs ||k S(N((R R((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyt__ne__pscCs5t|tƒstS|j|jkp4tj||ƒS(N(RRRR Rt__lt__(R R((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyRrscCs5t|tƒstS|j|jkp4tj||ƒS(N(RRRR Rt__le__(R R((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyRuscCs5t|tƒstS|j|jkp4tj||ƒS(N(RRtTrueR Rt__gt__(R R((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyRxscCs5t|tƒstS|j|jkp4tj||ƒS(N(RRRR Rt__ge__(R R((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyR{scCstdƒ‚dS(Ns$Tree does not support multiplication(R(R tv((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyt__mul__ƒscCstdƒ‚dS(Ns$Tree does not support multiplication(R(R R((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyt__rmul__…scCstdƒ‚dS(NsTree does not support addition(R(R R((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyt__add__‡scCstdƒ‚dS(NsTree does not support addition(R(R R((sB/Users/johnmelidis/Sites/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((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyRs cCsĘt|ttfƒr(tj|||ƒSt|ttfƒršt|ƒdkr^tdƒ‚qĀt|ƒdkr|||d>> t = Tree("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))") >>> t.leaves() ['the', 'dog', 'chased', 'the', 'cat'] :return: a list containing this tree's leaves. The order reflects the order of the leaves in the tree's hierarchical structure. :rtype: list (RRtextendtleavestappend(R R(tchild((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyR(æs  cCst|j|jƒƒS(s© Return a flat version of the tree, with all non-root non-terminals removed. >>> t = Tree("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))") >>> print t.flatten() (S the dog chased the cat) :return: a tree consisting of this tree's root connected directly to its leaves, omitting all intervening non-terminal nodes. :rtype: Tree (RR R((R ((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pytflattenŌs cCsUd}xD|D]<}t|tƒr:t||jƒƒ}q t|dƒ}q Wd|S(s; Return the height of the tree. >>> t = Tree("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))") >>> t.height() 5 >>> print t[0,0] (D the) >>> t[0,0].height() 2 :return: The height of this tree. The height of a tree containing no children is 1; the height of a tree containing only leaves is 2; and the height of any other tree is one plus the maximum of its children's heights. :rtype: int ii(RRtmaxtheight(R tmax_child_heightR*((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyR-ās  tpreordercs­g}|dkr"|jdƒnxht|ƒD]Z\‰}t|tƒry|j|ƒ}|j‡fd†|Dƒƒq/|jˆfƒq/W|dkr©|jdƒn|S( s" >>> t = Tree("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))") >>> t.treepositions() # doctest: +ELLIPSIS [(), (0,), (0, 0), (0, 0, 0), (0, 1), (0, 1, 0), (1,), (1, 0), (1, 0, 0), ...] >>> for pos in t.treepositions('leaves'): ... t[pos] = t[pos][::-1].upper() >>> print t (S (NP (D EHT) (N GOD)) (VP (V DESAHC) (NP (D EHT) (N TAC)))) :param order: One of: ``preorder``, ``postorder``, ``bothorder``, ``leaves``. R/t bothorderc3s|]}ˆf|VqdS(N((t.0tp(ti(sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pys st postorder(R/R0((R4R0((R)t enumerateRRt treepositionsR'(R tordert positionsR*tchildpos((R3sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyR6żs    ccsa| s||ƒr|Vnx?|D]7}t|tƒr"x|j|ƒD] }|VqGWq"q"WdS(sś Generate all the subtrees of this tree, optionally restricted to trees matching the filter function. >>> t = Tree("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))") >>> for s in t.subtrees(lambda t: t.height() == 2): ... print s (D the) (N dog) (V chased) (D the) (N cat) :type filter: function :param filter: the function to filter all local trees N(RRtsubtrees(R tfilterR*tsubtree((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyR:s  cCsvt|jtƒstd‚ntt|jƒt|ƒƒg}x0|D](}t|tƒrF||jƒ7}qFqFW|S(sż Generate the productions that correspond to the non-terminal nodes of the tree. For each subtree of the form (P: C1 C2 ... Cn) this produces a production of the form P -> C1 C2 ... Cn. >>> t = Tree("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))") >>> t.productions() [S -> NP VP, NP -> D N, D -> 'the', N -> 'dog', VP -> V NP, V -> 'chased', NP -> D N, D -> 'the', N -> 'cat'] :rtype: list(Production) sPProductions can only be generated from trees having node labels that are strings( RR RRt Productiont Nonterminalt _child_namesRt productions(R tprodsR*((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyR@-s ! cCsVg}xI|D]A}t|tƒr8|j|jƒƒq |j||jfƒq W|S(sš Return a sequence of pos-tagged words extracted from the tree. >>> t = Tree("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))") >>> t.pos() [('the', 'D'), ('dog', 'N'), ('chased', 'V'), ('the', 'D'), ('cat', 'N')] :return: a list of tuples containing leaves and pre-terminals (part-of-speech tags). The order reflects the order of the leaves in the tree's hierarchical structure. :rtype: list(tuple) (RRR'tposR)R (R RBR*((sB/Users/johnmelidis/Sites/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(s, :return: The tree position of the ``index``-th leaf in this tree. I.e., if ``tp=self.leaf_treeposition(i)``, then ``self[tp]==self.leaves()[i]``. :raise IndexError: If this tree contains fewer than ``index+1`` leaves, or if ``index<0``. isindex must be non-negativeii’’’’s-index must be less than or equal to len(self)N((R$tpopRRtrangeR!R)(R R"tstackR%ttreeposR3((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pytleaf_treepositionXs    #&cCsŒ||krtdƒ‚n|j|ƒ}|j|dƒ}xHtt|ƒƒD]4}|t|ƒks|||||krP|| SqPW|S(sŗ :return: The tree position of the lowest descendant of this tree that dominates ``self.leaves()[start:end]``. :raise ValueError: if ``end <= start`` send must be greater than starti(t ValueErrorRGRDR!(R tstarttendt start_treepost end_treeposR3((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyttreeposition_spanning_leavesos & trightit|t^cCs-ddlm}|||||||ƒdS(sł This method can modify a tree in three ways: 1. Convert a tree into its Chomsky Normal Form (CNF) equivalent -- Every subtree has either two non-terminals or one terminal as its children. This process requires the creation of more"artificial" non-terminal nodes. 2. Markov (vertical) smoothing of children in new artificial nodes 3. Horizontal (parent) annotation of nodes :param factor: Right or left factoring method (default = "right") :type factor: str = [left|right] :param horzMarkov: Markov order for sibling smoothing in artificial nodes (None (default) = include all siblings) :type horzMarkov: int | None :param vertMarkov: Markov order for parent smoothing (0 (default) = no vertical annotation) :type vertMarkov: int | None :param childChar: A string used in construction of the artificial nodes, separating the head of the original subtree from the child nodes that have yet to be expanded (default = "|") :type childChar: str :param parentChar: A string used to separate the node representation from its vertical annotation :type parentChar: str i’’’’(tchomsky_normal_formN(ttreetransformsRQ(R tfactort horzMarkovt vertMarkovt childChart parentCharRQ((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyRQ…st+cCs*ddlm}||||||ƒdS(sl This method modifies the tree in three ways: 1. Transforms a tree in Chomsky Normal Form back to its original structure (branching greater than two) 2. Removes any parent annotation (if it exists) 3. (optional) expands unary subtrees (if previously collapsed with collapseUnary(...) ) :param expandUnary: Flag to expand unary or not (default = True) :type expandUnary: bool :param childChar: A string separating the head node from its children in an artificial node (default = "|") :type childChar: str :param parentChar: A sting separating the node label from its parent annotation (default = "^") :type parentChar: str :param unaryChar: A string joining two non-terminals in a unary production (default = "+") :type unaryChar: str i’’’’(tun_chomsky_normal_formN(RRRY(R t expandUnaryRVRWt unaryCharRY((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyRY scCs'ddlm}|||||ƒdS(s Collapse subtrees with a single child (ie. unary productions) into a new non-terminal (Tree node) joined by 'joinChar'. This is useful when working with algorithms that do not allow unary productions, and completely removing the unary productions would require loss of useful information. The Tree is modified directly (since it is passed by reference) and no value is returned. :param collapsePOS: 'False' (default) will not collapse the parent of leaf nodes (ie. Part-of-Speech tags) since they are always unary productions :type collapsePOS: bool :param collapseRoot: 'False' (default) will not modify the root production if it is unary. For the Penn WSJ treebank corpus, this corresponds to the TOP -> productions. :type collapseRoot: bool :param joinChar: A string used to connect collapsed node values (default = "+") :type joinChar: str i’’’’(tcollapse_unaryN(RRR\(R t collapsePOSt collapseRoottjoinCharR\((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyR\¶scCsIt|tƒrAg|D]}|j|ƒ^q}||j|ƒS|SdS(s Convert a tree between different subtypes of Tree. ``cls`` determines which class will be used to encode the new tree. :type tree: Tree :param tree: The tree that should be converted. :return: The new Tree. N(RRtconvertR (tclsRR*R ((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyR`Šs "cCs3|st|ƒ|j|ƒSt|ƒj|ƒSdS(N(RR R`(R tdeep((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pytcopyąscCstS(N(t ImmutableTree(R ((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyt _frozen_classäscCs‡|jƒ}|dkr*|j|ƒ}nO|jdtƒ}x+|jdƒD]}|||ƒ|| value For example, these functions could be used to parse nodes and leaves whose values should be some type other than string (such as ``FeatStruct``). Note that by default, node strings and leaf strings are delimited by whitespace and brackets; to override this default, use the ``node_pattern`` and ``leaf_pattern`` arguments. :type node_pattern: str :type leaf_pattern: str :param node_pattern, leaf_pattern: Regular expression patterns used to find node and leaf substrings in ``s``. By default, both nodes patterns are defined to match any sequence of non-whitespace non-bracket characters. :type remove_empty_top_bracketing: bool :param remove_empty_top_bracketing: If the resulting tree has an empty node label, and is length one, then return its single child instead. This is useful for treebank trees, which sometimes contain an extra level of bracketing. :return: A tree corresponding to the string representation ``s``. If this class method is called using a subclass of Tree, then it will return a tree of that type. :rtype: Tree is"brackets must be a length-2 strings\sswhitespace brackets not alloweds [^\s%s%s]+s%s\s*(%s)?|%s|(%s)iis end-of-stringi’’’’tN(RRR!RtretsearchtescapeRtcompiletfinditertgroupt _parse_errortlstripR)RCtAssertionErrorR (Ratstbracketst parse_nodet parse_leaft node_patternt leaf_patterntremove_empty_top_bracketingtopen_btclose_bt open_patternt close_patternttoken_reREtmatchttokenR R R((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyRõsX5"     ,  !  ' 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|ƒ‚d S(sō Display a friendly error message when parsing a tree string fails. :param s: The string we're parsing. :param match: regexp match of the problem token. :param expecting: what we expected to see instead. s end-of-strings1%s.parse(): expected %r but got %r %sat index %d.t i s s i s...i s %s"%s" %s^iiNs s (R!RIRqRtreplaceRH(RaRuRt expectingRBR‚tmsgtoffset((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyRrcs   cCsddlm}||ƒdS(sP Open a new window containing a graphical diagram of this tree. i’’’’(t draw_treesN(tnltk.draw.treeRˆ(R Rˆ((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pytdrawscCs6djd„|Dƒƒ}dt|ƒj|j|fS(Ns, css|]}t|ƒVqdS(N(trepr(R1tc((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pys ‰ss %s(%r, [%s])(tjoinRRR (R tchildstr((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyt__repr__ˆscCs |jƒS(N(tpprint(R ((sB/Users/johnmelidis/Sites/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( sI :return: A pretty-printed string representation of this tree. :rtype: str :param margin: The right margin at which to do line-wrapping. :type margin: int :param indent: The indentation level at which printing begins. This number is used to decide how far to indent subsequent lines. :type indent: int :param nodesep: A string that is used to separate the node from the children. E.g., the default value ``':'`` gives trees like ``(S: (NP: I) (VP: (V: saw) (NP: it)))``. s%s%s%sis%s%r%ss Rƒit/s%ss%ri( t _pprint_flatR!RR RRRR R(R tmargintindenttnodeseptparenstquotesRuR*((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyRs  "&!"c Cs d|jdddddd ƒS( s­ Returns a representation of the tree compatible with the LaTeX qtree package. This consists of the string ``\Tree`` followed by the parse tree represented in bracketed notation. For example, the following result was generated from a parse tree of the sentence ``The announcement astounded us``:: \Tree [.I'' [.N'' [.D The ] [.N' [.N announcement ] ] ] [.I' [.V'' [.V' [.V astounded ] [.N'' [.N' [.N us ] ] ] ] ] ] ] See http://www.ling.upenn.edu/advice/latex.html for the LaTeX style file for the qtree package. :return: A latex qtree representation of this tree. :rtype: str s\Tree R•iR–RkR—s[.s ](s[.s ](R(R ((sB/Users/johnmelidis/Sites/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*((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyR“Čs N(-Rt __module__t__doc__RR RRRRRRRRRRRR#R&R(R+R-R6R:R@RBRGRMRQRRYRR\t classmethodR`RcReRjRRrRŠRR‘RR™R“(((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyRsT?                           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 ((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyR Üs "cCstdt|ƒjƒ‚dS(Ns%s may not be modified(RHRR(R R"R%((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyR#ęscCstdt|ƒjƒ‚dS(Ns%s may not be modified(RHRR(R R3tjR%((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyt __setslice__čscCstdt|ƒjƒ‚dS(Ns%s may not be modified(RHRR(R R"((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyR&źscCstdt|ƒjƒ‚dS(Ns%s may not be modified(RHRR(R R3R”((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyt __delslice__ģscCstdt|ƒjƒ‚dS(Ns%s may not be modified(RHRR(R R((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyt__iadd__īscCstdt|ƒjƒ‚dS(Ns%s may not be modified(RHRR(R R((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyt__imul__šscCstdt|ƒjƒ‚dS(Ns%s may not be modified(RHRR(R R((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyR)ņscCstdt|ƒjƒ‚dS(Ns%s may not be modified(RHRR(R R((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyR'ōscCstdt|ƒjƒ‚dS(Ns%s may not be modified(RHRR(R R((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyRCöscCstdt|ƒjƒ‚dS(Ns%s may not be modified(RHRR(R R((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pytremoveųscCstdt|ƒjƒ‚dS(Ns%s may not be modified(RHRR(R ((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pytreverseśscCstdt|ƒjƒ‚dS(Ns%s may not be modified(RHRR(R ((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pytsortüscCs|jS(N(R (R ((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyt__hash__žscCs|jS(sGet the node value(t_node(R ((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyt _get_nodescCs8t|dƒr+tdt|ƒjƒ‚n||_dS(s› Set the node value. This will only succeed the first time the node value is set, which should occur in ImmutableTree.__init__(). R s%s may not be modifiedN(thasattrRHRRRŖ(R R%((sB/Users/johnmelidis/Sites/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 (((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyRdŪs"               tAbstractParentedTreecBsžeZdZdd„Zed„Zd„Zd„Zd„Z d„Z d„Z d„Z d d „Z d „Zeed ƒrœd „Zd„Zd„ZnRS(sĆ An abstract base class for a ``Tree`` that automatically maintains pointers to parent nodes. These parent pointers are updated whenever any change is made to a tree's structure. Two subclasses are currently defined: - ``ParentedTree`` is used for tree structures where each subtree has at most one parent. This class should be used in cases where there is no"sharing" of subtrees. - ``MultiParentedTree`` is used for tree structures where a subtree may have zero or more parents. This class should be used in cases where subtrees may be shared. Subclassing =========== The ``AbstractParentedTree`` class redefines all operations that modify a tree's structure to call two methods, which are used by subclasses to update parent information: - ``_setparent()`` is called whenever a new child is added. - ``_delparent()`` is called whenever a child is removed. 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*((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyR ,s cCs tƒ‚dS(s” Update the parent pointer of ``child`` to point to ``self``. This method is only called if the type of ``child`` is ``Tree``; i.e., it is not called when adding a leaf to a tree. This method is always called before the child is actually added to the child list of ``self``. :type child: Tree :type index: int :param index: The index of ``child`` in ``self``. :raise TypeError: If ``child`` is a tree with an impropriate type. Typically, if ``child`` is a tree, then its type needs to match the type of ``self``. This prevents mixing of different tree types (single-parented, multi-parented, and non-parented). :param dry_run: If true, the don't actually set the child's parent pointer; just check for any error conditions, and raise an exception if one is found. N(tNotImplementedError(R R*R"R°((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyR±?scCs tƒ‚dS(s» Update the parent pointer of ``child`` to not point to self. This method is only called if the type of ``child`` is ``Tree``; i.e., it is not called when removing a leaf from a tree. This method is always called before the child is actually removed from the child list of ``self``. :type child: Tree :type index: int :param index: The index of ``child`` in ``self``. N(R²(R R*R"((sB/Users/johnmelidis/Sites/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((sB/Users/johnmelidis/Sites/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*((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyR?s  cCstdƒ‚dS(sF Use Tree.parse(s, remove_empty_top_bracketing=True) instead. s<Use Tree.parse(s, remove_empty_top_bracketing=True) instead.N(t NameError(Ru((sB/Users/johnmelidis/Sites/opener/constituent-parser-nl/core/tree.pyt bracket_parse%scCstjd|ƒ}xŚtt|ƒƒD]Ę}||dkrc||||d||d<||s,  ’’Ć8åu¤  $ G