ó <¿CVc @s½dZddlmZmZddlZddlmZmZddlm Z ddl m Z ddl m Z mZmZddlmZed efd „ƒYƒZd efd „ƒYZd efd„ƒYZdefd„ƒYZdefd„ƒYZdeefd„ƒYZdeefd„ƒYZedee fd„ƒYƒZedee fd„ƒYƒZd„Zd„Zd„Zd„Zdd d!d"d#d$d%d&d'd(d)g Z dS(*uh Class for representing hierarchical language structures, such as syntax trees and morphological trees. iÿÿÿÿ(tprint_functiontunicode_literalsN(t Productiont Nonterminal(tProbabilisticMixIn(t slice_bounds(t string_typestpython_2_unicode_compatiblet unicode_repr(traise_unorderable_typestTreecBs!eZdZd7d„Zd„Zd„Zd„Zd„Zd„Z d„Z d„Z d „Z d „Z d „Zd „Zd „Zd„Zd„Zd„ZeeeƒZd„Zd„Zd„Zd„Zd„Zdd„Zd7d„Zd„Zd„Zd„Zd„Z dd7ddd d!„Z!e"dd d"d#„Z#e$e$d"d$„Z%e&d%„ƒZ'e$d&„Z(d'„Z)d7d(„Z*e&d)d7d7d7d7e$d*„ƒZ+e&d+„ƒZ,d,„Z-d7d8d7d-„Z.d.„Z/d/„Z0d0„Z1d1„Z2d2dd3d)e$d4„Z3d5„Z4d6„Z5RS(9ui 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.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|ƒjƒ‚nDt|tƒrStdt|ƒjƒ‚ntj||ƒ||_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|tƒs%|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.pyR€scCs||kp||kS(N((RR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyR�scCs ||k S(N((RR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyR‚scCstdƒ‚dS(Nu$Tree does not support multiplication(R (Rtv((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt__mul__ˆscCstdƒ‚dS(Nu$Tree does not support multiplication(R (RR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt__rmul__ŠscCstdƒ‚dS(NuTree does not support addition(R (RR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt__add__ŒscCstdƒ‚dS(NuTree does not support addition(R (RR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt__radd__ŽscCs±t|ttfƒr%tj||ƒSt|ttfƒr…t|ƒdkrP|St|ƒdkrn||dS||d|dSn(tdt|ƒj t|ƒj fƒ‚dS(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 cCsÆt|ttfƒr(tj|||ƒSt|ttfƒršt|ƒdkr^tdƒ‚qÂt|ƒ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.pytlabelÌs 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_labelÙs cCsMg}x@|D]8}t|tƒr8|j|jƒƒq |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.pyR0çs  cCst|jƒ|jƒƒS(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.pytflattenüs cCsUd}xD|D]<}t|tƒr: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  upreordercs­g}|dkr"|jdƒnxht|ƒD]Z\‰}t|tƒry|j|ƒ}|j‡fd†|Dƒƒq/|jˆfƒq/W|dkr©|jdƒn|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|tƒr"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|jtƒs!tdƒ‚ntt|jƒt|ƒƒg}x0|D](}t|tƒrI||jƒ7}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|tƒr8|j|jƒƒq |j||jfƒq 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Ê|dkrtdƒ‚n|dfg}x�|r¹|jƒ\}}t|tƒsq|dkrd|S|d8}q-xBtt|ƒdddƒD]$}|j||||ffƒqŽWq-Wtdƒ‚dS(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-negativeiiÿÿÿÿu-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_treeposition€s    #&cCsŒ||krtdƒ‚n|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_leaves—s & 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.pyRQ­su+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.pyRXÈscCs'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|tƒrAg|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+|jdƒD]}|||ƒ|| 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-stringiÿÿÿÿuN(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}|jddƒjddƒ}|}t|ƒ|dkr¦||d d}n|dkrÍd||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`RuR�t expectingRER‚tmsgtoffset((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyRr‹s   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.pytdraw©scKs9ddlm}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.treeprettyprinterR‹tprintttext(Rtsentencet highlighttstreamtkwargsR‹((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt pretty_print°scCs<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 Cs®ddl}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 gddgdtƒgdj||ƒjƒƒt|dƒ�}|jƒ}WdQX|j|ƒ|j|ƒ|j|ƒjƒSWdQXdS(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. iÿÿÿÿN(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 subprocessttempfileR‰R™tnltk.draw.utilRštnltk.internalsR›tcanvast add_widgettbboxtNamedTemporaryFiletformattnamet print_to_filetdestroy_widgettcalltFalsetsplittopentreadtremovet b64encodetdecode(RRŸR R¡R¢R™RšR›t _canvas_frametwidgettxtytwthRŒtin_pathtout_pathtsrtres((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyt _repr_png_¾s2        -  cCs |jƒS(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' ustreamRŒN(R RŽRÀ(RR“R’((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pytpprintâs    iFuc Csl|j|||ƒ}t|ƒ||kr/|St|jtƒr^d|d|j|f}n d|dt|jƒ|f}xß|D]×}t|tƒrÐ|dd|d|j||d|||ƒ7}q…t|tƒr|dd|ddj |ƒ7}q…t|tƒr<| r<|dd|dd|7}q…|dd|dt|ƒ7}q…W||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 RÀR#R–(RtmargintindenttnodeseptparenstquotesRuR2((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyRÀîs   "&!$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([#\$%&~_\{\}])RÅiRÆuRÇu[.u ]u\Tree u\\\1(u[.u ](RlRoRÀtsub(Rtreserved_charsRÀ((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pytpformat_latex_qtreescCsg}x¡|D]™}t|tƒrA|j|j|||ƒƒq t|tƒri|jdj|ƒƒq t|tƒr“| r“|jd|ƒq |jt|ƒƒq Wt|jtƒrçd|d|j|dj|ƒ|dfSd|dt|jƒ|dj|ƒ|dfSdS(Nu/u%su %s%s%s %s%siu i( RR R1RÃR#R–RRR(RRÆRÇRÈt 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?RCRERJRPRQRfRXR®R[t classmethodR_RcReRkRƒRrRŠR”R˜R¿RÁRÂRÀRËRÃ(((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyR sd?                             k   !  % RdcBs•eZdd„Zd„Zd„Zd„Zd„Zd„Zd„Z d„Z d„Z dd „Z d „Z d „Zd „Zd „Zd„ZRS(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|ƒjƒ‚dS(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|ƒjƒ‚dS(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|ƒjƒ‚dS(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|ƒjƒ‚dS(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|ƒjƒ‚dS(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|ƒjƒ‚dS(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|ƒjƒ‚dS(Nu%s may not be modified(RKR R(RR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyR1TscCstdt|ƒjƒ‚dS(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|ƒjƒ‚dS(Nu%s may not be modified(RKR R(RR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyRFXscCstdt|ƒjƒ‚dS(Nu%s may not be modified(RKR R(RR((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyR²ZscCstdt|ƒjƒ‚dS(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|ƒjƒ‚dS(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|dƒr+tdt|ƒjƒ‚n||_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(RRÍR RR&RØR)RÙRÚRÛR1R/RFR²RÜRÝRÞR.(((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/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(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. 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àRR R:RR t _setparentRf(RRRR9R2((s[/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tree.pyRŠs cCs tƒ‚dS(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.pyRâ�scCs tƒ‚dS(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 _delparent³s 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_stepiuindex out of rangeu(The tree position () may not be deleted.iu#%s indices must be integers, not %s(RR!RRfRGR RãRÕRàR)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,  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|||ds< ÿÿÿ4åw¤(  $ G