
    /h                     T    S r SSKrSSKJrJr  SSKJr   " S S\5      rS r	S r
S/rg)	zh
Class for representing hierarchical language structures, such as
syntax trees and morphological trees.
    N)Nonterminal
Production)
deprecatedc                      \ rS rSrSrS7S jrS rS rS rS r	S	 r
S
 rS rS rS rS rS rS rS r\" S5      S 5       r\" S5      S 5       r\" \\5      rS rS rS rS rS rS8S jrS7S jrS rS r S r!S  r"     S9S! jr# S:S" jr$S;S# jr%\&S$ 5       r'S% r(S& r)S<S' jr*S( r+S7S) jr,\&      S=S* j5       r-\&S+ 5       r.\&S, 5       r/S- r0S>S. jr1S/ r2S0 r3S1 r4S2 r5S?S3 jr6S4 r7S5 r8S6r9g)@Tree   a  
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``.
Nc                     Uc!  [        S[        U 5      R                  -  5      e[        U[        5      (       a!  [        S[        U 5      R                  -  5      e[
        R                  X5        Xl        g )Nz)%s: Expected a node value and child list z.%s() argument 2 should be a list, not a string)	TypeErrortype__name__
isinstancestrlist__init___label)selfnodechildrens      F/var/www/auris/envauris/lib/python3.13/site-packages/nltk/tree/tree.pyr   Tree.__init__\   sm    ;d4j>Q>QQ  #&&:../ 
 MM$)K    c                     U R                   UR                   L =(       a/    U R                  [        U 5      4UR                  [        U5      4:H  $ N)	__class__r   r   r   others     r   __eq__Tree.__eq__n   sD    ~~0 
dkk4:5NLLKS
 6
 	
r   c                 p   [        U[        5      (       d-  U R                  R                  UR                  R                  :  $ U R                  UR                  L a/  U R                  [        U 5      4UR                  [        U5      4:  $ U R                  R                  UR                  R                  :  $ r   )r   r   r   r   r   r   r   s     r   __lt__Tree.__lt__t   s    %&& >>**U__-E-EEE^^u.KKd,d5k/JJJ>>**U__-E-EEEr   c                     X:X  + $ r    r   s     r   <lambda>Tree.<lambda>   s    T!2r   c                 *    X:  =(       d    X:H  (       + $ r   r#   r   s     r   r$   r%      s    dl&Cdm!Dr   c                      X:  =(       d    X:H  $ r   r#   r   s     r   r$   r%      s    !>!>r   c                     X:  + $ r   r#   r   s     r   r$   r%      s    T!1r   c                     [        S5      eNz$Tree does not support multiplicationr
   r   vs     r   __mul__Tree.__mul__       >??r   c                     [        S5      er*   r+   r,   s     r   __rmul__Tree.__rmul__   r0   r   c                     [        S5      eNzTree does not support additionr+   r,   s     r   __add__Tree.__add__       899r   c                     [        S5      er5   r+   r,   s     r   __radd__Tree.__radd__   r8   r   c                 p   [        U[        [        45      (       a  [        R	                  X5      $ [        U[        [
        45      (       a4  [        U5      S:X  a  U $ [        U5      S:X  a  XS      $ XS      USS     $ [        [        U 5      R                  < S[        U5      R                  < 35      e)Nr       indices must be integers, not )
r   intslicer   __getitem__tuplelenr
   r   r   r   indexs     r   rA   Tree.__getitem__   s    ec5\**##D00e}--5zQUq!H~%!H~eABi00:&&U(<(<> r   c                    [        U[        [        45      (       a  [        R	                  XU5      $ [        U[        [
        45      (       a?  [        U5      S:X  a  [        S5      e[        U5      S:X  a  X US   '   g X US      USS  '   g [        [        U 5      R                  < S[        U5      R                  < 35      e)Nr   z,The tree position () may not be assigned to.r=   r>   )r   r?   r@   r   __setitem__rB   rC   
IndexErrorr
   r   r   )r   rE   values      r   rH   Tree.__setitem__   s    ec5\**##D77e}--5zQ !RSSUq!&U1X,1U1XuQRy):&&U(<(<> r   c                 ~   [        U[        [        45      (       a  [        R	                  X5      $ [        U[        [
        45      (       a;  [        U5      S:X  a  [        S5      e[        U5      S:X  a  XS   	 g XS      USS  	 g [        [        U 5      R                  < S[        U5      R                  < 35      e)Nr   z(The tree position () may not be deleted.r=   r>   )r   r?   r@   r   __delitem__rB   rC   rI   r
   r   r   rD   s     r   rM   Tree.__delitem__   s    ec5\**##D00e}--5zQ !KLLUqqNqN59-:&&U(<(<> r   zUse label() insteadc                     g)zIOutdated method to access the node value; use the label() method instead.Nr#   r   s    r   	_get_nodeTree._get_node       r   zUse set_label() insteadc                     g)zJOutdated method to set the node value; use the set_label() method instead.Nr#   )r   rJ   s     r   	_set_nodeTree._set_node   rS   r   c                     U R                   $ )z
Return the node label of the tree.

    >>> 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   rP   s    r   label
Tree.label   s     {{r   c                     Xl         g)a/  
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
NrX   )r   rY   s     r   	set_labelTree.set_label   s	     r   c                     / nU  HJ  n[        U[        5      (       a!  UR                  UR                  5       5        M9  UR	                  U5        ML     U$ )aR  
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
)r   r   extendleavesappend)r   r`   childs      r   r`   Tree.leaves   sE     E%&&elln-e$	 
 r   c                 R    [        U R                  5       U R                  5       5      $ )au  
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   rY   r`   rP   s    r   flattenTree.flatten  s     DJJL$++-00r   c                     SnU  H?  n[        U[        5      (       a  [        XR                  5       5      nM3  [        US5      nMA     SU-   $ )a  
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
r   r=   )r   r   maxheight)r   max_child_heightrb   s      r   ri   Tree.height  sO    & E%&&#&'7#H #&'7#; 	 
 ###r   c                 <  ^ / nUS;   a  UR                  S5        [        U 5       H[  u  mn[        U[        5      (       a.  UR	                  U5      nUR                  U4S jU 5       5        MI  UR                  T45        M]     US;   a  UR                  S5        U$ )a  
    >>> 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``.
)preorder	bothorderr#   c              3   0   >#    U  H  nT4U-   v   M     g 7fr   r#   ).0pis     r   	<genexpr>%Tree.treepositions.<locals>.<genexpr>B  s      <8a!8s   )	postorderrn   )ra   	enumerater   r   treepositionsr_   )r   order	positionsrb   childposrr   s        @r   rw   Tree.treepositions/  s     	--R !$HAu%&& ..u5   <8 <<  !& ( ..R r   c              #      #    U(       a  U" U 5      (       a  U v   U  H3  n[        U[        5      (       d  M  UR                  U5       Sh  vN   M5     g N	7f)a  
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)r   r   subtrees)r   filterrb   s      r   r}   Tree.subtreesI  sC     " JE%&& >>&111 1s   1AAA
Ac                    [        U R                  [        5      (       d  [        S5      e[	        [        U R                  5      [        U 5      5      /nU  H,  n[        U[        5      (       d  M  XR                  5       -  nM.     U$ )a  
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() # doctest: +NORMALIZE_WHITESPACE
    [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)
zPProductions can only be generated from trees having node labels that are strings)	r   r   r   r
   r   r   _child_namesr   productions)r   prodsrb   s      r   r   Tree.productions`  ss     $++s++b  K4l46HIJE%&&**,,  r   c                     / nU  HU  n[        U[        5      (       a!  UR                  UR                  5       5        M9  UR	                  X R
                  45        MW     U$ )a  
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)
)r   r   r_   posra   r   )r   r   rb   s      r   r   Tree.posy  sL     E%&&

599;'

E;;/0	 
 
r   c                 >   US:  a  [        S5      eU S4/nU(       av  UR                  5       u  p4[        U[        5      (       d  US:X  a  U$ US-  nO8[	        [        U5      S-
  SS5       H  nUR                  X5   XE4-   45        M     U(       a  Mv  [        S5      e)z
: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``.
r   zindex must be non-negativer#   r=   z-index must be less than or equal to len(self))rI   popr   r   rangerC   ra   )r   rE   stackrJ   treeposrr   s         r   leaf_treepositionTree.leaf_treeposition  s     199::"YY[NEeT**A:"NQJEs5zA~r26ALL%(GdN!;< 7 e HIIr   c                     X!::  a  [        S5      eU R                  U5      nU R                  US-
  5      n[        [        U5      5       H#  nU[        U5      :X  d  X5   XE   :w  d  M  USU s  $    U$ )z
:return: The tree position of the lowest descendant of this
    tree that dominates ``self.leaves()[start:end]``.
:raise ValueError: if ``end <= start``
zend must be greater than startr=   N)
ValueErrorr   r   rC   )r   startendstart_treeposend_treeposrr   s         r   treeposition_spanning_leaves!Tree.treeposition_spanning_leaves  s}     <=>> ..u5,,S1W5s=)*AC$$(8KN(J$Ra(( + r   c                 $    SSK Jn  U" XX#XE5        g)aY  
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
r   )chomsky_normal_formN)nltk.tree.transformsr   )r   factor
horzMarkov
vertMarkov	childChar
parentCharr   s          r   r   Tree.chomsky_normal_form  s    > 	=D*)Xr   c                 $    SSK Jn  U" XX#U5        g)a  
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 string 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
r   )un_chomsky_normal_formN)r   r   )r   expandUnaryr   r   	unaryCharr   s         r   r   Tree.un_chomsky_normal_form  s    * 	@t)Sr   c                 "    SSK Jn  U" XX#5        g)a  
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
r   )collapse_unaryN)r   r   )r   collapsePOScollapseRootjoinCharr   s        r   r   Tree.collapse_unary  s    & 	8t,Ar   c                     [        U[        5      (       a3  U Vs/ s H  o R                  U5      PM     nnU " UR                  U5      $ U$ s  snf )z
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.
)r   r   convertr   )clstreerb   r   s       r   r   Tree.convert  sG     dD!!8<=uE*H=t{{H--K >s   Ac                 "    U R                  5       $ r   copyrP   s    r   __copy__Tree.__copy__$  s    yy{r   c                      U R                  SS9$ )NTdeepr   )r   memos     r   __deepcopy__Tree.__deepcopy__'  s    yydy##r   c                 |    U(       d  [        U 5      " U R                  U 5      $ [        U 5      R                  U 5      $ r   )r   r   r   )r   r   s     r   r   	Tree.copy*  s/    :dkk400:%%d++r   c                     SSK Jn  U$ )Nr   )ImmutableTree)nltk.tree.immutabler   )r   r   s     r   _frozen_classTree._frozen_class0  s    5r   c                     U R                  5       nUc  UR                  U 5      nODU R                  SS9nUR                  S5       H  nU" X4   5      X4'   M     UR                  U5      n[	        U5        U$ )NTr   r`   )r   r   r   rw   hash)r   leaf_freezerfrozen_classnewcopyr   s        r   freezeTree.freeze5  su    ))+"**40GiiTi*G,,X6+GL9 7"**73GWr   c           
         [        U[        5      (       a  [        U5      S:w  a  [        S5      e[        R
                  " SU5      (       a  [        S5      eUu  p[        R                  " U5      [        R                  " U	5      pUc  SU
 U S3nUc  SU
 U S3n[        R                  " U
< SU< S	U< S
U< S35      nS/ 4/nUR                  U5       GHK  nUR                  5       nUS   U:X  ai  [        U5      S:X  a'  [        US   S   5      S:  a  U R                  XS5        USS R                  5       nUb  U" U5      nUR                  U/ 45        M  X:X  a}  [        U5      S:X  a:  [        US   S   5      S:X  a  U R                  XU5        OU R                  XS5        UR                  5       u  nnUS   S   R                  U " UU5      5        GM  [        U5      S:X  a  U R                  XU5        Ub  U" U5      nUS   S   R                  U5        GMN     [        U5      S:  a  U R                  USU	5        OK[        US   S   5      S:X  a  U R                  USU5        O"US   S   b   e[        US   S   5      S:X  d   eUS   S   S   nU(       a$  UR                  S:X  a  [        U5      S:X  a  US   nU$ )a  
Read a bracketed tree string and return the resulting tree.
Trees are represented as nested brackettings, such as::

  (S (NP (NNP John)) (VP (V runs)))

:type s: str
:param s: The string to read

:type brackets: str (length=2)
:param brackets: The bracket characters used to mark the
    beginning and end of trees and subtrees.

:type read_node: function
:type read_leaf: function
:param read_node, read_leaf: If specified, these functions
    are applied to the substrings of ``s`` corresponding to
    nodes and leaves (respectively) to obtain the values for
    those nodes and leaves.  They should have the following
    signature:

       read_node(str) -> 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
   z"brackets must be a length-2 stringz\szwhitespace brackets not allowedNz[^\sz]+z\s*(z)?|z|()r   r=   end-of-stringr    )r   r   rC   r
   researchescapecompilefinditergroup_parse_errorlstripra   r   r   )r   sbrackets	read_node	read_leafnode_patternleaf_patternremove_empty_top_bracketingopen_bclose_bopen_patternclose_patterntoken_rer   matchtokenrY   r   r   s                      r   
fromstringTree.fromstringE  s   x (C((CMQ,>@AA99UH%%=>>"')yy'8"))G:Lm"<.rBL"<.rBL::\=,H

 &&q)EKKMEQx6!u:?s58A;'7!';$$Q?ab	((*(%e,EeR[)!u:?58A;'1,((6:((?C"'))+xb	!##Cx$89 u:?$$Qv6(%e,Eb	!##E*3 *8 u:>Q9q!"Q88A;&&&uQx{#q(((Qx{1~ '4;;"+<Ta7Dr   c                    US:X  a  [        U5      SpTOUR                  5       UR                  5       pTSU R                  UUSU4-  nUR	                  SS5      R	                  SS5      nUn[        U5      US-   :  a  USUS-    S	-   nUS:  a  S	XS-
  S -   nS
nUSR                  SUSSU-   -  5      -  n[        U5      e)z
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.
r   z0%s.read(): expected %r but got %r
%sat index %d.z            
 	
   Nz...   z
{}"{}"
{}^z                   )rC   r   r   r   replaceformatr   )r   r   r   	expectingr   r   msgoffsets           r   r   Tree._parse_error  s     O#QALLE
 
 IIdC ((s3q6C"H*C"H%A8(*%AF%%h3"v+3FGGor   c           	          [        U5      [        :X  a\  [        U5      S:  aL  [        US   5      n[        U5      S:  a-  [	        X!SS  Vs/ s H  o0R                  U5      PM     sn5      $ U$ ggs  snf )z
:type l: list
:param l: a tree represented as nested lists

:return: A tree corresponding to the list representation ``l``.
:rtype: Tree

Convert nested lists to a NLTK Tree
r   r=   N)r   r   rC   reprr   fromlist)r   lrY   rb   s       r   r   Tree.fromlist  sj     7d?s1vz1JE1vzEqrU#KUELL$7U#KLL  *? $Ls   A1
c                      SSK Jn  U" U 5        g)z@
Open a new window containing a graphical diagram of this tree.
r   )
draw_treesN)nltk.draw.treer   )r   r   s     r   draw	Tree.draw  s     	.4r   c                 R    SSK Jn  [        U" XU5      R                  " S0 UD6US9  g)z
Pretty-print this tree as ASCII or Unicode art.
For explanation of the arguments, see the documentation for
`nltk.tree.prettyprinter.TreePrettyPrinter`.
r   )TreePrettyPrinterfileNr#   )nltk.tree.prettyprinterr   printtext)r   sentence	highlightstreamkwargsr   s         r   pretty_printTree.pretty_print  s(     	>	:??I&IPVWr   c                     SR                  S U  5       5      nSR                  [        U 5      R                  [	        U R
                  5      U5      $ )Nz, c              3   8   #    U  H  n[        U5      v   M     g 7fr   )r   )rp   cs     r   rs    Tree.__repr__.<locals>.<genexpr>  s     3dT!WWds   z{}({}, [{}]))joinr   r   r   r   r   )r   childstrs     r   __repr__Tree.__repr__  sE    993d33$$J
 	
r   c                 :    SSK Jn  U" U 5      R                  5       $ )Nr   )	draw_tree)svglingr  
_repr_svg_)r   r  s     r   r  Tree._repr_svg_  s    %))++r   c                 "    U R                  5       $ r   )pformatrP   s    r   __str__Tree.__str__  s    ||~r   c                 Z    SU;   a	  US   nUS	 OSn[        U R                  " S0 UD6US9  g)z8
Print a string representation of this Tree to 'stream'
r  Nr   r#   )r  r  )r   r  r  s      r   pprintTree.pprint  s9    
 vH%Fx Fdll$V$62r   c           
         U R                  X4U5      n[        U5      U-   U:  a  U$ [        U R                  [        5      (       a  US    U R                   U 3nOUS    [        U R                  5       U 3nU  H  n[        U[        5      (       a'  USSUS-   -  -   UR                  XS-   X4U5      -   -  nM?  [        U[        5      (       a"  USSUS-   -  -   SR                  U5      -   -  nMv  [        U[        5      (       a  U(       d  USSUS-   -  -   SU-  -   -  nM  USSUS-   -  -   [        U5      -   -  nM     XdS   -   $ )a  
: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)))``.
r   r   r   r   /%sr=   )
_pformat_flatrC   r   r   r   r   r   r  rB   r  )r   marginindentnodesepparensquotesr   rb   s           r   r  Tree.pformat!  sZ     w7q6F?V#H dkk3''!9+dkk]7)4A!9+d4;;/0	:AE%&&VaZ()mmFQJPQ
 E5))TC6A:..%@@E3''TC6A:..==TC6A:..e<<  !9}r   c                     [         R                  " S5      nU R                  SSSS9nS[         R                  " USU5      -   $ )aH  
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 https://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
z([#\$%&~_\{\}])   r   )z[.z ])r$  r%  r&  z\Tree z\\\1)r   r   r  sub)r   reserved_charsr  s      r   pformat_latex_qtreeTree.pformat_latex_qtreeI  s?    $ $67,,aL,I266.'7CCCr   c                    / nU  H  n[        U[        5      (       a#  UR                  UR                  XU5      5        M;  [        U[        5      (       a"  UR                  SR                  U5      5        Mr  [        U[        5      (       a  U(       d  UR                  SU-  5        M  UR                  [        U5      5        M     [        U R                  [        5      (       a4  SR                  US   U R                  USR                  U5      US   5      $ SR                  US   [        U R                  5      USR                  U5      US   5      $ )Nr   r!  z{}{}{} {}{}r   r   r=   )
r   r   ra   r"  rB   r  r   r   r   r   )r   r%  r&  r'  	childstrsrb   s         r   r"  Tree._pformat_flat`  s   	E%&&  !4!4Wf!MNE5))  %1E3''  .  e-  dkk3'' ''q	#q	  !''q	T[[!#q	 r   rX   r   )rm   )rightNr   |^)Tr3  r4  +)FFr5  )F)()NNNNF)Nr#   N)F   r   r   r6  F):r   
__module____qualname____firstlineno____doc__r   r   r    __ne____gt____le____ge__r.   r2   r6   r:   rA   rH   rM   r   rQ   rU   propertyr   rY   r\   r`   re   ri   rw   r}   r   r   r   r   r   r   r   classmethodr   r   r   r   r   r   r   r   r   r   r	  r  r  r  r  r  r-  r"  __static_attributes__r#   r   r   r   r      s   =~$
	F 3FDF>F1F@@::  & %&X 'X )*Y +Y Iy)D*1$642.2(J40 !YH JMT2B6  $,

   $)v vp  <  *X
,

3&PD.r   r   c                     / nU  HO  n[        U[        5      (       a&  UR                  [        UR                  5      5        M>  UR                  U5        MQ     U$ r   )r   r   ra   r   r   )r   namesrb   s      r   r   r   }  sF    EeT""LLU\\23LL	 
 Lr   c                     SSK Jn Jn  SnUR                  U5      n[	        S5        [	        U5        [	        UR                  5       5        [	        S5        [	        UR                  5       5        [	        US   5        [	        US   5        [	        UR                  5       5        [	        UR                  5       5        [	        US   5        [	        US   5        [	        US   5        US   nUR                  SUR                  S	5      5        [	        S
5        [	        U5        UR                  S5      US'   [	        U5        [	        5         [	        S5        UR                  5         [	        U5        [	        S5        UR                  5         [	        U5        [	        5         U " SSS/SS9n[	        S5        [	        U5        [	        5         UR                  UR                  5       5      n[	        S5        [	        U5        [	        5         [	        S5        [	        UR                  5       5        [	        5         [	        S5        [	        UR                  5       5        [	        5         UR                  S5        [	        U5        g)z
A demonstration showing how Trees and Trees can be
used.  This demonstration creates a Tree, and loads a
Tree from the Treebank corpus,
and shows the results of calling several of their methods.
r   )ProbabilisticTreer   zA(S (NP (DT the) (NN cat)) (VP (VBD ate) (NP (DT a) (NN cookie))))z#Convert bracketed string into tree:zDisplay tree properties:r=   )r=   r=   )r=   r=   r   z(JJ big)zTree modification:z	(NN cake))r=   r=   r=   zCollapse unary:zChomsky normal form:xyzg      ?)probzProbabilistic Tree:z0Convert tree to bracketed string and back again:zLaTeX output:zProduction output:)test   N)nltkrF  r   r   r  r  rY   ri   r`   insertr   r   r  r-  r   r\   )rF  r   r   tthe_catpts         r   demorR    s    - 	LAA	
/0	!H	!**,	
$%	!'')	!A$K	!A$K	!((*	!((*	!A$K	!D'N	!G* dGNN1dooj12	
	!H-AgJ	!H	G 

	!H	
 !	!H	G 
3c
	5B	
 	"I	G 			$A	
<=	!H	G 
/	!


!"	G 

	!--/	G KK	!Hr   )r;  r   nltk.grammarr   r   nltk.internalsr   r   r   r   rR  __all__r#   r   r   <module>rV     s<   
 
 0 %^4 ^BER r   