
    NgJX                         d dl Z d dlmZmZ d dlmZ d dlmZ  G d dee          Z G d d	e          Z	 G d
 de          Z
d	dgZdS )    N)ABCMetaabstractmethod)Tree)slice_boundsc                        e Zd ZdZd fd	Zedd            Zed             Z fdZ fdZ	 fd	Z
 fd
Z fdZd fd	Z fdZ eed          r	d Zd Zd Zd Z xZS )AbstractParentedTreea  
    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.
    Nc                 X   t                                          ||           |t          |           D ]2\  }}t          |t                    r|                     ||d           3t          |           D ]2\  }}t          |t                    r|                     ||           1d S d S )NTdry_run)super__init__	enumerate
isinstancer   
_setparentselfnodechildrenichild	__class__s        N/var/www/html/ai-engine/env/lib/python3.11/site-packages/nltk/tree/parented.pyr   zAbstractParentedTree.__init__.   s    x(((  &dOO < <5eT** <OOE1dO;;;%dOO . .5eT** .OOE1---  . .    Fc                     dS )a  
        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   r   indexr   s       r   r   zAbstractParentedTree._setparent@         r   c                     dS )a  
        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``.
        Nr   r   r   r   s      r   
_delparentzAbstractParentedTree._delparentV   r   r   c                    t          |t                    rt          | |d          \  }}}t          |||          D ]9}t          | |         t                    r|                     | |         |           :t                                          |           d S t          |t                    r|dk     r|t          |           z  }|dk     rt          d          t          | |         t                    r|                     | |         |           t                                          |           d S t          |t          t          f          rYt          |          dk    rt          d          t          |          dk    r| |d         = d S | |d                  |dd          = d S t          t          |           j        dt          |          j                  )NT
allow_stepr   index out of rangez(The tree position () may not be deleted.    indices must be integers, not )r   slicer   ranger   r!   r   __delitem__intlen
IndexErrorlisttuple	TypeErrortype__name__)r   r   startstopstepr   r   s         r   r*   z AbstractParentedTree.__delitem__j   s   eU## $	 ,T5T J J JE45$-- 0 0d1gt,, 0OODGQ///GG&&&&& s## 	qyyT"qyy !5666$u+t,, 4UU333GG&&&&&e}-- 	5zzQ !KLLLUqqNNN qN59--- ::&&&U(<(<>  r   c                    t          |t                    r>t          | |d          \  }}}t          |t          t          f          st          |          }t          |          D ]8\  }}t          |t                    r|                     ||||z  z   d           9t          |||          D ]9}t          | |         t                    r| 	                    | |         |           :t          |          D ]6\  }}t          |t                    r|                     ||||z  z              7t                                          ||           d S t          |t                    r|dk     r|t          |           z  }|dk     rt          d          || |         u rd S t          |t                    r|                     ||           t          | |         t                    r| 	                    | |         |           t                                          ||           d S t          |t          t          f          r]t          |          dk    rt          d          t          |          dk    r|| |d         <   d S || |d                  |dd          <   d S t          t!          |           j        dt!          |          j                  )	NTr#   r
   r   r%   z,The tree position () may not be assigned to.r&   r'   )r   r(   r   r.   r/   r   r   r   r)   r!   r   __setitem__r+   r,   r-   r0   r1   r2   )	r   r   valuer3   r4   r5   r   r   r   s	           r   r7   z AbstractParentedTree.__setitem__   s   eU## 8	 ,T5T J J JE4edE]33 $U &e,, K K5eT** KOOE51t8+;TOJJJ5$-- 0 0d1gt,, 0OODGQ/// &e,, = =5eT** =OOE51t8+;<<<GGu----- s##  	qyyT"qyy !5666U##%&& .u---$u+t,, 4UU333GGu-----e}-- 	5zzQ !OPPPUq!&U1X -2U1XuQRRy))) ::&&&U(<(<>  r   c                     t          |t                    r#|                     |t          |                      t	                                          |           d S Nr   r   r   r,   r   append)r   r   r   s     r   r<   zAbstractParentedTree.append   sK    eT"" 	.OOE3t99---ur   c                     |D ][}t          |t                    r#|                     |t          |                      t	                                          |           \d S r:   r;   )r   r   r   r   s      r   extendzAbstractParentedTree.extend   sa     	" 	"E%&& 2s4yy111GGNN5!!!!	" 	"r   c                     |dk     r|t          |           z  }|dk     rd}t          |t                    r|                     ||           t	                                          ||           d S Nr   )r,   r   r   r   r   insert)r   r   r   r   s      r   rA   zAbstractParentedTree.insert   sp     199SYYE199EeT"" 	*OOE5)))ue$$$$$r   c                    |dk     r|t          |           z  }|dk     rt          d          t          | |         t                    r|                     | |         |           t                                          |          S )Nr   r%   )r,   r-   r   r   r!   r   pop)r   r   r   s     r   rD   zAbstractParentedTree.pop   sx    199SYYE1991222d5k4(( 	0OODK///ww{{5!!!r   c                     |                      |          }t          | |         t                    r|                     | |         |           t	                                          |           d S r:   )r   r   r   r!   r   remove)r   r   r   r   s      r   rF   zAbstractParentedTree.remove   s^    

5!!d5k4(( 	0OODK///ur   __getslice__c           	          |                      t          t          d|          t          d|                              S r@   )__getitem__r(   maxr   r3   r4   s      r   rG   z!AbstractParentedTree.__getslice__   0    ##E#a--Q$F$FGGGr   c           	          |                      t          t          d|          t          d|                              S r@   )r*   r(   rJ   rK   s      r   __delslice__z!AbstractParentedTree.__delslice__  rL   r   c           	          |                      t          t          d|          t          d|                    |          S r@   )r7   r(   rJ   )r   r3   r4   r8   s       r   __setslice__z!AbstractParentedTree.__setslice__  s2    ##E#a--Q$F$FNNNr   c                 .    | j         t          |           fS )a  Method used by the pickle module when un-pickling.
        This method provides the arguments passed to ``__new__``
        upon un-pickling. Without this method, ParentedTree instances
        cannot be pickled and unpickled in Python 3.7+ onwards.

        :return: Tuple of arguments for ``__new__``, i.e. the label
            and the children of this node.
        :rtype: Tuple[Any, List[AbstractParentedTree]]
        )_labelr.   r   s    r   __getnewargs__z#AbstractParentedTree.__getnewargs__  s     T$ZZ((r   r:   F)rB   )r2   
__module____qualname____doc__r   r   r   r!   r*   r7   r<   r>   rA   rD   rF   hasattrr.   rG   rN   rP   rT   __classcell__r   s   @r   r   r      s        0. . . . . .$    ^*   ^&& & & & &P: : : : :x    
" " " " "% % % % %" " " " " "     wt^$$ 	O	H 	H 	H	H 	H 	H	O 	O 	O
) 
) 
) 
) 
) 
) 
)r   r   )	metaclassc                   h     e Zd ZdZd fd	Zd Zd fd	Zd Zd Zd	 Z	d
 Z
d Zd Zd ZddZ xZS )ParentedTreea  
    A ``Tree`` that automatically maintains parent pointers for
    single-parented trees.  The following are methods for querying
    the structure of a parented tree: ``parent``, ``parent_index``,
    ``left_sibling``, ``right_sibling``, ``root``, ``treeposition``.

    Each ``ParentedTree`` may have at most one parent.  In
    particular, subtrees may not be shared.  Any attempt to reuse a
    single ``ParentedTree`` as a child of more than one parent (or
    as multiple children of the same parent) will cause a
    ``ValueError`` exception to be raised.

    ``ParentedTrees`` should never be used in the same tree as ``Trees``
    or ``MultiParentedTrees``.  Mixing tree implementations may result
    in incorrect parent pointers and in ``TypeError`` exceptions.
    Nc                     d | _         	 t                                          ||           |Gt          |           D ]9\  }}t	          |t
                    rd |_         |                     ||           8d S d S r:   )_parentr   r   r   r   r   r   r   s        r   r   zParentedTree.__init__'  s    Cx(((
 &dOO . .5eT** .$(EMOOE1--- 
. .r   c                     ddl m} |S )Nr   )ImmutableParentedTree)nltk.tree.immutablerb   )r   rb   s     r   _frozen_classzParentedTree._frozen_class5  s    ======$$r   Fc                     |s!t          j        | j        j         d           t	                                          d          S )NzB objects do not support shallow copies. Defaulting to a deep copy.T)deep)warningswarnr   r2   r   copy)r   rf   r   s     r   ri   zParentedTree.copy:  sJ     	M>*nnn   ww|||&&&r   c                     | j         S )z5The parent of this tree, or None if it has no parent.)r`   rS   s    r   parentzParentedTree.parentE  s
    |r   c                 l    | j         dS t          | j                   D ]\  }}|| u r|c S J d            )aD  
        The index of this tree in its parent.  I.e.,
        ``ptree.parent()[ptree.parent_index()] is ptree``.  Note that
        ``ptree.parent_index()`` is not necessarily equal to
        ``ptree.parent.index(ptree)``, since the ``index()`` method
        returns the first child that is equal to its argument.
        NFz&expected to find self in self._parent!)r`   r   )r   r   r   s      r   parent_indexzParentedTree.parent_indexI  sT     <4!$,// 	 	HAu}} >>>>>r   c                 h    |                                  }| j        r|dk    r| j        |dz
           S dS )z6The left sibling of this tree, or None if it has none.r   r&   N)rm   r`   r   rm   s     r   left_siblingzParentedTree.left_siblingX  s>    ((**< 	2L1,,<q 011tr   c                     |                                  }| j        r+|t          | j                  dz
  k     r| j        |dz            S dS )z7The right sibling of this tree, or None if it has none.r&   N)rm   r`   r,   ro   s     r   right_siblingzParentedTree.right_sibling_  sM    ((**< 	2LC,=,=,ABB<q 011tr   c                     | }|                                 (|                                 }|                                 (|S )z
        The root of this tree.  I.e., the unique ancestor of this tree
        whose parent is None.  If ``ptree.parent()`` is None, then
        ``ptree`` is its own root.
        )rk   r   roots     r   ru   zParentedTree.rootf  s9     kkmm';;==D kkmm'r   c                     |                                  dS |                                                                  |                                 fz   S )z
        The tree position of this tree, relative to the root of the
        tree.  I.e., ``ptree.root[ptree.treeposition] is ptree``.
        Nr   )rk   treepositionrm   rS   s    r   rw   zParentedTree.treepositionq  sE    
 ;;== 2;;==--//43D3D3F3F2HHHr   c                 p    t          |t                    sJ | |         |u sJ |j        | u sJ d |_        d S r:   )r   r^   r`   r    s      r   r!   zParentedTree._delparent  sM    %.....E{e####}$$$$ r   c                     t          |t                    st          d          t          |d          r|j        t          d          |s	| |_        d S d S )Nz5Can not insert a non-ParentedTree into a ParentedTreer`   z3Can not insert a subtree that already has a parent.)r   r^   r0   rY   r`   
ValueErrorr   s       r   r   zParentedTree._setparent  sn    %.. 	USTTT 5)$$ 	T)BRSSS  	! EMMM	! 	!r   r:   rU   )r2   rV   rW   rX   r   rd   ri   rk   rm   rp   rr   ru   rw   r!   r   rZ   r[   s   @r   r^   r^     s         ". . . . . .% % %
' ' ' ' ' '  ? ? ?    	 	 	I I I  ! ! ! ! ! ! ! !r   r^   c                   h     e Zd ZdZd fd	Zd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd ZddZ xZS )MultiParentedTreea  
    A ``Tree`` that automatically maintains parent pointers for
    multi-parented trees.  The following are methods for querying the
    structure of a multi-parented tree: ``parents()``, ``parent_indices()``,
    ``left_siblings()``, ``right_siblings()``, ``roots``, ``treepositions``.

    Each ``MultiParentedTree`` may have zero or more parents.  In
    particular, subtrees may be shared.  If a single
    ``MultiParentedTree`` is used as multiple children of the same
    parent, then that parent will appear multiple times in its
    ``parents()`` method.

    ``MultiParentedTrees`` should never be used in the same tree as
    ``Trees`` or ``ParentedTrees``.  Mixing tree implementations may
    result in incorrect parent pointers and in ``TypeError`` exceptions.
    Nc                     g | _         	 t                                          ||           |Gt          |           D ]9\  }}t	          |t
                    rg |_         |                     ||           8d S d S r:   )_parentsr   r   r   r   r   r   r   s        r   r   zMultiParentedTree.__init__  s    	 	x(((
 &dOO . .5eT** .%'ENOOE1--- 
. .r   c                     ddl m} |S )Nr   )ImmutableMultiParentedTree)rc   r   )r   r   s     r   rd   zMultiParentedTree._frozen_class  s    BBBBBB))r   c                 *    t          | j                  S )a  
        The set of parents of this tree.  If this tree has no parents,
        then ``parents`` is the empty set.  To check if a tree is used
        as multiple children of the same parent, use the
        ``parent_indices()`` method.

        :type: list(MultiParentedTree)
        )r.   r~   rS   s    r   parentszMultiParentedTree.parents  s     DM"""r   c                 >    d |                                  D             S )a}  
        A list of all left siblings of this tree, in any of its parent
        trees.  A tree may be its own left sibling if it is used as
        multiple contiguous children of the same parent.  A tree may
        appear multiple times in this list if it is the left sibling
        of this tree with respect to multiple parents.

        :type: list(MultiParentedTree)
        c                 6    g | ]\  }}|d k    ||dz
           S )r   r&   r   .0rk   r   s      r   
<listcomp>z3MultiParentedTree.left_siblings.<locals>.<listcomp>  s6     
 
 
qyy 519yyr   _get_parent_indicesrS   s    r   left_siblingszMultiParentedTree.left_siblings  /    
 
#'#;#;#=#=
 
 
 	
r   c                 >    d |                                  D             S )a  
        A list of all right siblings of this tree, in any of its parent
        trees.  A tree may be its own right sibling if it is used as
        multiple contiguous children of the same parent.  A tree may
        appear multiple times in this list if it is the right sibling
        of this tree with respect to multiple parents.

        :type: list(MultiParentedTree)
        c                 V    g | ]&\  }}|t          |          d z
  k     ||d z            'S )r&   )r,   r   s      r   r   z4MultiParentedTree.right_siblings.<locals>.<listcomp>  sD     
 
 
Fa(( 519(((r   r   rS   s    r   right_siblingsz MultiParentedTree.right_siblings  r   r   c                 *      fd j         D             S )Nc                 J    g | ]}t          |          D ]\  }}|u 	||f S r   )r   )r   rk   r   r   r   s       r   r   z9MultiParentedTree._get_parent_indices.<locals>.<listcomp>  sP     
 
 
 )& 1 1
 
 u}} UO }}}r   r~   rS   s   `r   r   z%MultiParentedTree._get_parent_indices  s0    
 
 
 
-
 
 
 	
r   c                 j    t          |                     i                                                     S )z
        The set of all roots of this tree.  This set is formed by
        tracing all possible parent paths until trees with no parents
        are found.

        :type: list(MultiParentedTree)
        )r.   _get_roots_helpervaluesrS   s    r   rootszMultiParentedTree.roots  s,     D**2..5577888r   c                 x    | j         r | j         D ]}|                    |           n| |t          |           <   |S r:   )r~   r   id)r   resultrk   s      r   r   z#MultiParentedTree._get_roots_helper  sP    = 	$- 1 1((00001  $F2d88r   c                 P     | j         vrg S  fdt          |          D             S )aY  
        Return a list of the indices where this tree occurs as a child
        of ``parent``.  If this child does not occur as a child of
        ``parent``, then the empty list is returned.  The following is
        always true::

          for parent_index in ptree.parent_indices(parent):
              parent[parent_index] is ptree
        c                 "    g | ]\  }}|u 	|S r   r   )r   r   r   r   s      r   r   z4MultiParentedTree.parent_indices.<locals>.<listcomp>  s"    SSSnueUd]]E]]]r   )r~   r   )r   rk   s   ` r   parent_indicesz MultiParentedTree.parent_indices  s9     &&ISSSS	&0A0ASSSSr   c                 <      u rdgS  fd j         D             S )a  
        Return a list of all tree positions that can be used to reach
        this multi-parented tree starting from ``root``.  I.e., the
        following is always true::

          for treepos in ptree.treepositions(root):
              root[treepos] is ptree
        r   c                 ~    g | ]9}|                               D ]!}t          |          D ]\  }}|u 	||fz   ":S r   )treepositionsr   )r   rk   treeposr   r   ru   r   s        r   r   z3MultiParentedTree.treepositions.<locals>.<listcomp>!  su       %33D99  &/&7&7	  #UED==	 5(" !====r   r   rt   s   ``r   r   zMultiParentedTree.treepositions  sH     4<<4K    "m   r   c                     t          |t                    sJ  |         |u sJ t           fd|j        D                       dk    sJ t	                     D ]\  }}||u r	||k    r d S |j                                        d S )Nc                     g | ]}|u |	S r   r   )r   pr   s     r   r   z0MultiParentedTree._delparent.<locals>.<listcomp>1  s    ;;;!dAr   r&   )r   r|   r,   r~   r   rF   )r   r   r   r   cs   `    r   r!   zMultiParentedTree._delparent-  s    %!233333E{e####;;;;u~;;;<<AAAA dOO 	( 	(DAqEzza5jjN!!$'''''r   Fc                     t          |t                    st          d          |s-|j        D ]	}|| u r d S 
|j                            |            d S d S )Nz?Can not insert a non-MultiParentedTree into a MultiParentedTree)r   r|   r0   r~   r<   )r   r   r   r   rk   s        r   r   zMultiParentedTree._setparent;  s    %!233 	Q  
  	,. , ,T>>EE " %%d+++++	, 	,r   r:   rU   )r2   rV   rW   rX   r   rd   r   r   r   r   r   r   r   r   r!   r   rZ   r[   s   @r   r|   r|     s         ". . . . . . * * *	# 	# 	#
 
 
 
 
 
 
 
 
9 9 9  T T T  0( ( (, , , , , , , ,r   r|   )rg   abcr   r   nltk.tree.treer   	nltk.utilr   r   r^   r|   __all__r   r   r   <module>r      s    ' ' ' ' ' ' ' '       " " " " " "}) }) }) }) })47 }) }) }) })@~! ~! ~! ~! ~!' ~! ~! ~!Br, r, r, r, r,, r, r, r,l r   