a
     ˜ëjJX  ã                   @   sp   d dl Z d dlmZmZ d dlmZ d dlmZ G dd„ deed�ZG dd	„ d	eƒZ	G d
d„ deƒZ
d	dgZdS )é    N)ÚABCMetaÚabstractmethod)ÚTree)Úslice_boundsc                       s¼   e Zd ZdZd"‡ fdd„	Zed#dd„ƒZedd	„ ƒZ‡ fd
d„Z‡ fdd„Z	‡ fdd„Z
‡ fdd„Z‡ fdd„Zd$‡ fdd„	Z‡ fdd„Zeedƒr¬dd„ Zdd„ Zdd„ Zd d!„ Z‡  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                    sn   t ƒ  ||¡ |d urjt| ƒD ]"\}}t|tƒr| j||dd� qt| ƒD ]\}}t|tƒrJ|  ||¡ qJd S )NT©Údry_run)ÚsuperÚ__init__Ú	enumerateÚ
isinstancer   Ú
_setparent©ÚselfÚnodeÚchildrenÚiÚchild©Ú	__class__© úQ/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/tree/parented.pyr
   .   s    

zAbstractParentedTree.__init__Fc                 C   s   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.
        Nr   ©r   r   Úindexr   r   r   r   r   @   s    zAbstractParentedTree._setparentc                 C   s   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   r   r   r   Ú
_delparentV   s    zAbstractParentedTree._delparentc                    s,  t |tƒr\t| |dd�\}}}t|||ƒD ]"}t | | tƒr*|  | | |¡ q*tƒ  |¡ nÌt |tƒr¶|dk rz|t	| ƒ7 }|dk rŠt
dƒ‚t | | tƒr¨|  | | |¡ tƒ  |¡ nrt |ttfƒ�rt	|ƒdkrÜt
dƒ‚n.t	|ƒdkrô| |d = n| |d  |dd … = ntdt| ƒjt|ƒjf ƒ‚d S )NT©Z
allow_stepr   úindex out of rangez(The tree position () may not be deleted.é   ú#%s indices must be integers, not %s)r   Úslicer   Úranger   r   r	   Ú__delitem__ÚintÚlenÚ
IndexErrorÚlistÚtupleÚ	TypeErrorÚtypeÚ__name__)r   r   ÚstartÚstopÚstepr   r   r   r   r"   j   s2    


ÿÿz AbstractParentedTree.__delitem__c                    sä  t |tƒrÚt| |dd�\}}}t |ttfƒs4t|ƒ}t|ƒD ]*\}}t |tƒr<| j||||  dd� q<t|||ƒD ]"}t | | tƒrt|  	| | |¡ qtt|ƒD ]&\}}t |tƒr |  ||||  ¡ q t
ƒ  ||¡ �nt |tƒ�rf|dk rú|t| ƒ7 }|dk �rtdƒ‚|| | u �rd S t |tƒ�r6|  ||¡ t | | tƒ�rV|  	| | |¡ t
ƒ  ||¡ nzt |ttfƒ�rÄt|ƒdk�rŽtdƒ‚n4t|ƒdk�rª|| |d < n|| |d  |dd … < ntdt| ƒjt|ƒjf ƒ‚d S )	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%   r(   r)   r*   )r   r   Úvaluer+   r,   r-   r   r   r   r   r   r.   ’   sJ    




ÿÿz AbstractParentedTree.__setitem__c                    s*   t |tƒr|  |t| ƒ¡ tƒ  |¡ d S ©N©r   r   r   r$   r	   Úappend)r   r   r   r   r   r2   Î   s    
zAbstractParentedTree.appendc                    s4   |D ]*}t |tƒr"|  |t| ƒ¡ tƒ  |¡ qd S r0   r1   )r   r   r   r   r   r   ÚextendÓ   s    
zAbstractParentedTree.extendc                    sH   |dk r|t | ƒ7 }|dk r d}t|tƒr6|  ||¡ tƒ  ||¡ d S ©Nr   )r$   r   r   r   r	   Úinsert)r   r   r   r   r   r   r5   Ù   s    
zAbstractParentedTree.insertéÿÿÿÿc                    sN   |dk r|t | ƒ7 }|dk r$tdƒ‚t| | tƒrB|  | | |¡ tƒ  |¡S )Nr   r   )r$   r%   r   r   r   r	   Úpop)r   r   r   r   r   r7   æ   s    zAbstractParentedTree.popc                    s8   |   |¡}t| | tƒr(|  | | |¡ tƒ  |¡ d S r0   )r   r   r   r   r	   Úremover   r   r   r   r8   ñ   s    
zAbstractParentedTree.removeÚ__getslice__c                 C   s   |   ttd|ƒtd|ƒƒ¡S r4   )Ú__getitem__r    Úmax©r   r+   r,   r   r   r   r9   ÿ   s    z!AbstractParentedTree.__getslice__c                 C   s   |   ttd|ƒtd|ƒƒ¡S r4   )r"   r    r;   r<   r   r   r   Ú__delslice__  s    z!AbstractParentedTree.__delslice__c                 C   s   |   ttd|ƒtd|ƒƒ|¡S r4   )r.   r    r;   )r   r+   r,   r/   r   r   r   Ú__setslice__  s    z!AbstractParentedTree.__setslice__c                 C   s   | 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]]
        )Z_labelr&   ©r   r   r   r   Ú__getnewargs__  s    
z#AbstractParentedTree.__getnewargs__)N)F)r6   )r*   Ú
__module__Ú__qualname__Ú__doc__r
   r   r   r   r"   r.   r2   r3   r5   r7   r8   Úhasattrr&   r9   r=   r>   r@   Ú__classcell__r   r   r   r   r      s$   
(<
r   )Ú	metaclassc                       sz   e Zd ZdZd‡ fdd„	Zdd„ Zd‡ fdd	„	Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zddd„Z‡  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                    sN   d | _ tƒ  ||¡ |d u rJt| ƒD ]$\}}t|tƒr$d |_ |  ||¡ q$d S r0   )Ú_parentr	   r
   r   r   r   r   r   r   r   r   r
   '  s    
zParentedTree.__init__c                 C   s   ddl m} |S )Nr   )ÚImmutableParentedTree)Únltk.tree.immutablerI   )r   rI   r   r   r   Ú_frozen_class5  s    zParentedTree._frozen_classFc                    s&   |st  | jj› d�¡ tƒ jdd�S )NzB objects do not support shallow copies. Defaulting to a deep copy.T)Údeep)ÚwarningsÚwarnr   r*   r	   Úcopy)r   rL   r   r   r   rO   :  s
    ÿzParentedTree.copyc                 C   s   | j S )z5The parent of this tree, or None if it has no parent.)rH   r?   r   r   r   ÚparentE  s    zParentedTree.parentc                 C   sB   | j du rdS t| j ƒD ]\}}|| u r|  S qds>J dƒ‚dS )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!)rH   r   )r   r   r   r   r   r   Úparent_indexI  s    

zParentedTree.parent_indexc                 C   s(   |   ¡ }| jr$|dkr$| j|d  S dS )z6The left sibling of this tree, or None if it has none.r   r   N)rQ   rH   ©r   rQ   r   r   r   Úleft_siblingX  s    zParentedTree.left_siblingc                 C   s2   |   ¡ }| jr.|t| jƒd k r.| j|d  S dS )z7The right sibling of this tree, or None if it has none.r   N)rQ   rH   r$   rR   r   r   r   Úright_sibling_  s    zParentedTree.right_siblingc                 C   s   | }|  ¡ dur|  ¡ }q|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.
        N)rP   ©r   Úrootr   r   r   rV   f  s    
zParentedTree.rootc                 C   s*   |   ¡ du rdS |   ¡  ¡ |  ¡ f S dS )z�
        The tree position of this tree, relative to the root of the
        tree.  I.e., ``ptree.root[ptree.treeposition] is ptree``.
        Nr   )rP   ÚtreepositionrQ   r?   r   r   r   rW   q  s    zParentedTree.treepositionc                 C   s6   t |tƒsJ ‚| | |u sJ ‚|j| u s,J ‚d |_d S r0   )r   rG   rH   r   r   r   r   r     s    zParentedTree._delparentc                 C   s<   t |tƒstdƒ‚t|dƒr.|jd ur.tdƒ‚|s8| |_d S )Nz5Can not insert a non-ParentedTree into a ParentedTreerH   z3Can not insert a subtree that already has a parent.)r   rG   r(   rD   rH   Ú
ValueErrorr   r   r   r   r   ˆ  s    
zParentedTree._setparent)N)F)F)r*   rA   rB   rC   r
   rK   rO   rP   rQ   rS   rT   rV   rW   r   r   rE   r   r   r   r   rG     s   	rG   c                       s|   e Zd ZdZd‡ fdd„	Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zddd„Z‡  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                    sN   g | _ tƒ  ||¡ |d u rJt| ƒD ]$\}}t|tƒr$g |_ |  ||¡ q$d S r0   )Ú_parentsr	   r
   r   r   r   r   r   r   r   r   r
   ¨  s    
zMultiParentedTree.__init__c                 C   s   ddl m} |S )Nr   )ÚImmutableMultiParentedTree)rJ   r[   )r   r[   r   r   r   rK   ¸  s    zMultiParentedTree._frozen_classc                 C   s
   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&   rZ   r?   r   r   r   ÚparentsÁ  s    	zMultiParentedTree.parentsc                 C   s   d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                 S   s$   g | ]\}}|d kr||d  ‘qS )r   r   r   ©Ú.0rP   r   r   r   r   Ú
<listcomp>Ö  s   þz3MultiParentedTree.left_siblings.<locals>.<listcomp>©Ú_get_parent_indicesr?   r   r   r   Úleft_siblingsÌ  s    
þzMultiParentedTree.left_siblingsc                 C   s   d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                 S   s,   g | ]$\}}|t |ƒd  k r||d   ‘qS )r   )r$   r]   r   r   r   r_   æ  s   þz4MultiParentedTree.right_siblings.<locals>.<listcomp>r`   r?   r   r   r   Úright_siblingsÜ  s    
þz MultiParentedTree.right_siblingsc                    s   ‡ fdd„ˆ j D ƒS )Nc                    s.   g | ]&}t |ƒD ]\}}|ˆ u r||f‘qqS r   )r   )r^   rP   r   r   r?   r   r   r_   í  s   ýz9MultiParentedTree._get_parent_indices.<locals>.<listcomp>©rZ   r?   r   r?   r   ra   ì  s    
þz%MultiParentedTree._get_parent_indicesc                 C   s   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Úvaluesr?   r   r   r   Úrootsô  s    zMultiParentedTree.rootsc                 C   s.   | j r| j D ]}| |¡ qn| |t| ƒ< |S r0   )rZ   re   Úid)r   ÚresultrP   r   r   r   re   þ  s
    
z#MultiParentedTree._get_roots_helperc                    s(   |ˆ j vrg S ‡ fdd„t|ƒD ƒS 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                    s   g | ]\}}|ˆ u r|‘qS r   r   )r^   r   r   r?   r   r   r_     ó    z4MultiParentedTree.parent_indices.<locals>.<listcomp>N)rZ   r   )r   rP   r   r?   r   Úparent_indices  s    

z MultiParentedTree.parent_indicesc                    s(   ˆˆ u rdgS ‡ ‡fdd„ˆj D ƒS 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                    s@   g | ]8}|  ˆ ¡D ](}t|ƒD ]\}}|ˆu r||f ‘qqqS r   )Útreepositionsr   )r^   rP   Ztreeposr   r   ©rV   r   r   r   r_   !  s
   üz3MultiParentedTree.treepositions.<locals>.<listcomp>Nrd   rU   r   rm   r   rl     s
    	þzMultiParentedTree.treepositionsc                    st   t |tƒsJ ‚ˆ | |u sJ ‚t‡ fdd„|jD ƒƒdks>J ‚tˆ ƒD ]\}}||u rF||krF qpqF|j ˆ ¡ d S )Nc                    s   g | ]}|ˆ u r|‘qS r   r   )r^   Úpr?   r   r   r_   1  rj   z0MultiParentedTree._delparent.<locals>.<listcomp>r   )r   rY   r$   rZ   r   r8   )r   r   r   r   Úcr   r?   r   r   -  s     zMultiParentedTree._delparentFc                 C   s>   t |tƒstdƒ‚|s:|jD ]}|| u r q:q|j | ¡ d S )Nz?Can not insert a non-MultiParentedTree into a MultiParentedTree)r   rY   r(   rZ   r2   )r   r   r   r   rP   r   r   r   r   ;  s    
ÿ
zMultiParentedTree._setparent)N)F)r*   rA   rB   rC   r
   rK   r\   rb   rc   ra   rg   re   rk   rl   r   r   rE   r   r   r   r   rY   –  s   	
rY   )rM   Úabcr   r   Znltk.tree.treer   Z	nltk.utilr   r   rG   rY   Ú__all__r   r   r   r   Ú<module>   s       7þ