>> 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.fromstring("(S (NP I) (VP (V saw) (NP him)))") >>> s == t True >>> t[1][1].set_label('X') >>> t[1][1].label() '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 The set_label() and label() methods allow individual constituents to be labeled. For example, syntax trees use this label to specify 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(label, children)`` constructs a new tree with the specified label and list of children. - ``Tree.fromstring(s)`` constructs a new tree by parsing the string ``s``. cCsp|dkr(tdt|jnDt|trStdt|jntj||||_dS(Nu)%s: Expected a node value and child list u.%s() argument 2 should be a list, not a string( tNonet TypeErrorttypet__name__t isinstanceRtlistt__init__t_label(tselftnodetchildren((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyR`s cCs:|j|jko9|jt|f|jt|fkS(N(t __class__RR(Rtother((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt__eq__oscCsyt|ts%|jj|jjkS|j|jkr_|jt|f|jt|fkS|jj|jjkSdS(N(RR RRRR(RR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt__lt__ss (cCs ||k S(N((RR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pytscCs||kp||k S(N((RR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyRscCs||kp||kS(N((RR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyRscCs ||k S(N((RR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyRscCstddS(Nu$Tree does not support multiplication(R (Rtv((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt__mul__scCstddS(Nu$Tree does not support multiplication(R (RR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt__rmul__scCstddS(NuTree does not support addition(R (RR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt__add__scCstddS(NuTree does not support addition(R (RR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt__radd__scCst|ttfr%tj||St|ttfrt|dkrP|St|dkrn||dS||d|dSn(tdt|j t|j fdS(Niiu#%s indices must be integers, not %s( RtinttsliceRt __getitem__ttupletlenR R R(Rtindex((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyR"s cCst|ttfr(tj|||St|ttfrt|dkr^tdqt|dkr|||d>> t = Tree.fromstring('(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))') >>> t.label() 'S' :return: the node label (typically a string) :rtype: any (R(R((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pytlabels cCs ||_dS(uo Set the node label of the tree. >>> t = Tree.fromstring("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))") >>> t.set_label("T") >>> print(t) (T (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat)))) :param label: the node label (typically a string) :type label: any N(R(RR-((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt set_labels cCsMg}x@|D]8}t|tr8|j|jq |j|q W|S(u Return the leaves of the tree. >>> t = Tree.fromstring("(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 (RR textendtleavestappend(RR0tchild((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyR0s  cCst|j|jS(u Return a flat version of the tree, with all non-root non-terminals removed. >>> t = Tree.fromstring("(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 (R R-R0(R((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pytflattens cCsUd}xD|D]<}t|tr:t||j}q t|d}q Wd|S(uG Return the height of the tree. >>> t = Tree.fromstring("(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(RR tmaxtheight(Rtmax_child_heightR2((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyR5 s  upreordercsg}|dkr"|jdnxht|D]Z\}t|try|j|}|jfd|Dq/|jfq/W|dkr|jdn|S( u. >>> t = Tree.fromstring("(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``. upreorderu bothorderc3s|]}f|VqdS(N((t.0tp(ti(s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pys 7su postorder(upreorderu bothorder((u postorderu bothorder((R1t enumerateRR t treepositionsR/(Rtordert positionsR2tchildpos((R9s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyR;%s    ccsa| s||r|Vnx?|D]7}t|tr"x|j|D] }|VqGWq"q"WdS(u Generate all the subtrees of this tree, optionally restricted to trees matching the filter function. >>> t = Tree.fromstring("(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(RR tsubtrees(RtfilterR2tsubtree((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyR?=s  cCsyt|jts!tdntt|jt|g}x0|D](}t|trI||j7}qIqIW|S(u 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.fromstring("(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) uPProductions can only be generated from trees having node labels that are strings( RRRR RRt _child_namesR t productions(RtprodsR2((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyRCUs! cCsVg}xI|D]A}t|tr8|j|jq |j||jfq W|S(u Return a sequence of pos-tagged words extracted from the tree. >>> t = Tree.fromstring("(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) (RR R/tposR1R(RRER2((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyREls  cCs|dkrtdn|dfg}x|r|j\}}t|tsq|dkrd|S|d8}q-xBtt|dddD]$}|j||||ffqWq-WtddS(u, :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``. iuindex must be non-negativeiiu-index must be less than or equal to len(self)N((R'tpopRR trangeR$R1(RR%tstackR(ttreeposR9((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pytleaf_treepositions    #&cCs||krtdn|j|}|j|d}xHtt|D]4}|t|ks|||||krP|| SqPW|S(u :return: The tree position of the lowest descendant of this tree that dominates ``self.leaves()[start:end]``. :raise ValueError: if ``end <= start`` uend must be greater than starti(t ValueErrorRJRGR$(Rtstarttendt start_treepost end_treeposR9((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyttreeposition_spanning_leavess & urightiu|u^cCs-ddlm}|||||||dS(u 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(tnltk.treetransformsRQ(Rtfactort horzMarkovt vertMarkovt childChart parentCharRQ((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyRQsu+cCs*ddlm}||||||dS(ul 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(RRRX(Rt expandUnaryRVRWt unaryCharRX((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyRXscCs'ddlm}|||||dS(u 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[(Rt collapsePOSt collapseRoottjoinCharR[((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyR[scCsIt|trAg|D]}|j|^q}||j|S|SdS(u 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(RR tconvertR(tclsttreeR2R((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyR_s "cCs3|st||j|St|j|SdS(N(R RR_(Rtdeep((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pytcopyscCstS(N(t ImmutableTree(R((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt _frozen_class scCs|j}|dkr*|j|}nO|jdt}x+|jdD]}||||| value For example, these functions could be used to process 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 iu"brackets must be a length-2 stringu\suwhitespace brackets not allowedu [^\s%s%s]+u%s\s*(%s)?|%s|(%s)iiu end-of-stringiuN(RRR$R tretsearchtescapeR tcompiletfinditertgroupt _parse_errortlstripR1RFtAssertionErrorR(R`tstbracketst read_nodet read_leaft node_patternt leaf_patterntremove_empty_top_bracketingtopen_btclose_bt open_patternt close_patternttoken_reRHtmatchttokenR-RRa((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt fromstringsX5"     ,  !  ' cCs|dkr"t|d}}n|j|j}}d|j||d|f}|jddjdd}|}t||dkr||d d}n|dkrd||d}d }n|d d|dd |f7}t|d S(u 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. u end-of-stringu0%s.read(): expected %r but got %r %sat index %d.u i u u i u...i u %s"%s" %s^iiNu u (R$RLRqRtreplaceRK(R`RuRt expectingRERtmsgtoffset((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyRrs   cCsddlm}||dS(uP Open a new window containing a graphical diagram of this tree. i(t draw_treesN(tnltk.draw.treeR(RR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pytdrawscKs9ddlm}t||||j|d|dS(u Pretty-print this tree as ASCII or Unicode art. For explanation of the arguments, see the documentation for `nltk.treeprettyprinter.TreePrettyPrinter`. i(tTreePrettyPrintertfileN(tnltk.treeprettyprinterRtprintttext(Rtsentencet highlighttstreamtkwargsR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt pretty_printscCs<djd|D}dt|jt|j|fS(Nu, css|]}t|VqdS(N(R(R7tc((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pys su %s(%s, [%s])(tjoinR RRR(Rtchildstr((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt__repr__sc Csddl}ddl}ddl}ddl}ddlm}ddlm}ddlm }|}||j |} |j | | j \} } } } dd| | f|j d<|j }dj|j}d j|j}|j||j| |j|d d d d gddgdtgdj||jt|d}|j}WdQX|j||j||j|jSWdQXdS(u Draws and outputs in PNG for ipython. PNG is used instead of PDF, since it can be displayed in the qt console and has wider browser support. iN(ttree_to_treesegment(t CanvasFrame(t find_binaryiu scrollregionu{0:}.psu{0:}.pngugst binary_namesu gswin32c.exeu gswin64c.exetenv_varsuPATHtverboseuz-q -dEPSCrop -sDEVICE=png16m -r90 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dSAFER -dBATCH -dNOPAUSE -sOutputFile={0:} {1:}urb(tostbase64t subprocessttempfileRRtnltk.draw.utilRtnltk.internalsRtcanvast add_widgettbboxtNamedTemporaryFiletformattnamet print_to_filetdestroy_widgettcalltFalsetsplittopentreadtremovet b64encodetdecode(RRRRRRRRt _canvas_frametwidgettxtytwthRtin_pathtout_pathtsrtres((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt _repr_png_s2        -  cCs |jS(N(tpformat(R((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt__str__scKsCd|kr |d}|d=nd}t|j|d|dS(uH Print a string representation of this Tree to 'stream' ustreamRN(R RR(RRR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pytpprints    iFuc Csl|j|||}t|||kr/|St|jtr^d|d|j|f}n d|dt|j|f}x|D]}t|tr|dd|d|j||d|||7}qt|tr|dd|ddj |7}qt|tr<| r<|dd|dd|7}q|dd|dt|7}qW||dS( uI :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)))``. u%s%s%siu u iu/u%si( t _pformat_flatR$RRRRR RR#R(RtmargintindenttnodeseptparenstquotesRuR2((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyRs   "&!$cCsDtjd}|jdddddd }d tj|d |S( u Returns a representation of the tree compatible with the LaTeX qtree package. This consists of the string ``\Tree`` followed by the 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 u([#\$%&~_\{\}])RiRuRu[.u ]u\Tree u\\\1(u[.u ](RlRoRtsub(Rtreserved_charsR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pytpformat_latex_qtreescCsg}x|D]}t|trA|j|j|||q t|tri|jdj|q t|tr| r|jd|q |jt|q Wt|jtrd|d|j|dj||dfSd|dt|j|dj||dfSdS(Nu/u%su %s%s%s %s%siu i( RR R1RR#RRRR(RRRRt childstrsR2((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyR*s N((6Rt __module__t__doc__R RRRt__ne__t__gt__t__le__t__ge__RRRRR"R&R)R+R,tpropertyRR-R.R0R3R5R;R?RCRERJRPRQRfRXRR[t classmethodR_RcReRkRRrRRRRRRRRR(((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyR sd?                             k   !  % RdcBseZddZdZdZdZdZdZdZ dZ dZ dd Z d Z d Zd Zd ZdZRS(cCsrtt|j||y"t|jt|f|_Wn0ttfk rmtdt |j nXdS(Nu-%s: node value and children must be immutable( tsuperRdRRgRR#t_hashR RKR R(RRR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyR>s "cCstdt|jdS(Nu%s may not be modified(RKR R(RR%R(((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyR&HscCstdt|jdS(Nu%s may not be modified(RKR R(RR9tjR(((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt __setslice__JscCstdt|jdS(Nu%s may not be modified(RKR R(RR%((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyR)LscCstdt|jdS(Nu%s may not be modified(RKR R(RR9R((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt __delslice__NscCstdt|jdS(Nu%s may not be modified(RKR R(RR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt__iadd__PscCstdt|jdS(Nu%s may not be modified(RKR R(RR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt__imul__RscCstdt|jdS(Nu%s may not be modified(RKR R(RR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyR1TscCstdt|jdS(Nu%s may not be modified(RKR R(RR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyR/VscCstdt|jdS(Nu%s may not be modified(RKR R(RR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyRFXscCstdt|jdS(Nu%s may not be modified(RKR R(RR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyRZscCstdt|jdS(Nu%s may not be modified(RKR R(R((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pytreverse\scCstdt|jdS(Nu%s may not be modified(RKR R(R((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pytsort^scCs|jS(N(R(R((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt__hash__`scCs8t|dr+tdt|jn||_dS(u Set the node label. This will only succeed the first time the node label is set, which should occur in ImmutableTree.__init__(). u_labelu%s may not be modifiedN(thasattrRKR RR(RR(((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyR.csN(RRR RR&RR)RRRR1R/RFRRRRR.(((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyRd=s             tAbstractParentedTreecBseZdZddZedZdZdZdZ dZ dZ dZ d d Z d Zeed rd ZdZdZnRS(u 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. cCstt|j|||dk rxBt|D]4\}}t|tr2|j||dtq2q2Wx?t|D].\}}t|trw|j||qwqwWndS(Ntdry_run( RRRR R:RR t _setparentRf(RRRR9R2((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyRs cCs tdS(u 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(R*(RR2R%R((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyRscCs tdS(u 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*(RR2R%((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt _delparents cCst|trt||dt\}}}xDt|||D]0}t||tr@|j|||q@q@Wtt|j |n t|t r|dkr|t |7}n|dkrt dnt||tr|j|||ntt|j |nt|t tfrt |dkrOt dqt |dkro||d=q||d|d=n(tdt|jt|jfdS(Nt allow_stepiuindex out of rangeu(The tree position () may not be deleted.iu#%s indices must be integers, not %s(RR!RRfRGR RRRR)R R$R'RR#R R R(RR%RLtstoptstepR9((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyR)s,  cCst|trHt||dt\}}}t|ttfsQt|}nxJt|D]<\}}t|tr^|j||||dtq^q^WxDt |||D]0}t||tr|j |||qqWxDt|D]6\}}t|tr|j||||qqWt t |j ||n_t|tr |dkrv|t|7}n|dkrtdn|||krdSt|tr|j||nt||tr|j |||nt t |j ||nt|ttfrt|dkrCtdqt|dkrf|||ds< 4w(  $ G