§
    'ê[fÔ~  ã                   óä   — d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ  G d„ d	¦  «        Zdd„Z G d„ de¦  «        Zd„ Zdd„Zd„ Zd„ Zd„ ZdZdZdZedk    r e¦   «          dS dS )z™
Tools for reading and writing dependency trees.
The input is assumed to be in Malt-TAB format
(https://stp.lingfil.uu.se/~nivre/research/MaltXML.html).
é    N)Údefaultdict)Úchain)Úpformat)Úfind_binary)ÚTreec                   óÜ   — e Zd ZdZ	 	 	 	 	 d d„Zd„ Zd„ Zd„ Zd	„ Zd
„ Z	d„ Z
d„ Zd„ Zd„ Zd„ Ze	 d!d„¦   «         Zd„ Zd„ Zd„ Z	 	 	 	 d"d„Zd#d„Zd„ Zd„ Zd$d„Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ ZdS )%ÚDependencyGraphzQ
    A container for the nodes and labelled edges of a dependency structure.
    NFÚROOTc                 óÂ   — t          d„ ¦  «        | _        | j        d                              ddddœ¦  «         d| _        |r|                      |||||¬¦  «         dS dS )a¬  Dependency graph.

        We place a dummy `TOP` node with the index 0, since the root node is
        often assigned 0 as its head. This also means that the indexing of the
        nodes corresponds directly to the Malt-TAB format, which starts at 1.

        If zero-based is True, then Malt-TAB-like input with node numbers
        starting at 0 and the root node assigned -1 (as produced by, e.g.,
        zpar).

        :param str cell_separator: the cell separator. If not provided, cells
            are split by whitespace.

        :param str top_relation_label: the label by which the top relation is
            identified, for examlple, `ROOT`, `null` or `TOP`.
        c            
      ó>   — d d d d d d d t          t          ¦  «        d dœ	S )N)	ÚaddressÚwordÚlemmaÚctagÚtagÚfeatsÚheadÚdepsÚrel)r   Úlist© ó    úN/var/www/piapp/venv/lib/python3.11/site-packages/nltk/parse/dependencygraph.pyú<lambda>z*DependencyGraph.__init__.<locals>.<lambda>=   s1   € ØØØØØØØÝ#¥DÑ)Ô)Øð
ð 
€ r   r   ÚTOP)r   r   r   N)Úcell_extractorÚ
zero_basedÚcell_separatorÚtop_relation_label)r   ÚnodesÚupdateÚrootÚ_parse)ÚselfÚtree_strr   r   r   r   s         r   Ú__init__zDependencyGraph.__init__$   s’   € õ0 !ð
ð 
ñ
ô 
ˆŒ
ð 	Œ
�1Œ×Ò e°EÀaÐHÐHÑIÔIÐIàˆŒ	àð 	Ø�KŠKØØ-Ø%Ø-Ø#5ð ñ ô ð ð ð ð	ð 	r   c                 ó   — | j         |= dS )zw
        Removes the node with the given address.  References
        to this node in others will still exist.
        N©r    )r$   r   s     r   Úremove_by_addressz!DependencyGraph.remove_by_addressW   s   € ð
 ŒJ�wÐÐÐr   c                 óÀ   — | j                              ¦   «         D ]C}g }|d         D ]1}||v r|                     |¦  «         Œ|                     |¦  «         Œ2||d<   ŒDdS )zp
        Redirects arcs to any of the nodes in the originals list
        to the redirect node address.
        r   N)r    ÚvaluesÚappend)r$   Ú	originalsÚredirectÚnodeÚnew_depsÚdeps         r   Úredirect_arcszDependencyGraph.redirect_arcs^   sƒ   € ð
 ”J×%Ò%Ñ'Ô'ð 	$ð 	$ˆDØˆHØ˜F”|ð )ð )�Ø˜)Ð#Ð#Ø—O’O HÑ-Ô-Ð-Ð-à—O’O CÑ(Ô(Ð(Ð(Ø#ˆD�‰LˆLð	$ð 	$r   c                 óÒ   — | j         |         d         }| j         |         d                              |g ¦  «         | j         |         d         |                              |¦  «         dS )zw
        Adds an arc from the node specified by head_address to the
        node specified by the mod address.
        r   r   N)r    Ú
setdefaultr,   )r$   Úhead_addressÚmod_addressÚrelations       r   Úadd_arczDependencyGraph.add_arcl   sb   € ð
 ”:˜kÔ*¨5Ô1ˆØŒ
�<Ô  Ô(×3Ò3°H¸bÑAÔAÐAØŒ
�<Ô  Ô(¨Ô2×9Ò9¸+ÑFÔFÐFÐFÐFr   c                 óH  — | j                              ¦   «         D ]‡}| j                              ¦   «         D ]k}|d         |d         k    rW|d         dk    rK|d         }|d                              |g ¦  «         |d         |                              |d         ¦  «         ŒlŒˆdS )zr
        Fully connects all non-root nodes.  All nodes are set to be dependents
        of the root node.
        r   r   r   r   N)r    r+   r4   r,   )r$   Únode1Únode2r7   s       r   Úconnect_graphzDependencyGraph.connect_graphv   s½   € ð
 ”Z×&Ò&Ñ(Ô(ð 	Eð 	EˆEØœ×*Ò*Ñ,Ô,ð Eð E�Ø˜Ô# u¨YÔ'7Ò7Ð7¸EÀ%¼LÈEÒ<QÐ<QØ$ Uœ|�HØ˜&”M×,Ò,¨X°rÑ:Ô:Ð:Ø˜&”M (Ô+×2Ò2°5¸Ô3CÑDÔDÐDøð	Eð	Eð 	Er   c                 ó   — | j         |         S )z'Return the node with the given address.r(   ©r$   Únode_addresss     r   Úget_by_addresszDependencyGraph.get_by_addressƒ   s   € àŒz˜,Ô'Ð'r   c                 ó   — || j         v S )zq
        Returns true if the graph contains a node with the given node
        address, false otherwise.
        r(   r>   s     r   Úcontains_addressz DependencyGraph.contains_address‡   s   € ð
 ˜tœzÐ)Ð)r   c           	      ó¦  — d}|dz  }|dz  }t          | j                             ¦   «         d„ ¬¦  «        D ]•}|d                     |d         |d         |d         ¦  «        z  }|d	                              ¦   «         D ]L\  }}|D ]D}|�!|d                     |d         ||¦  «        z  }Œ%|d                     |d         |¦  «        z  }ŒEŒMŒ–|dz  }|S )a  Return a dot representation suitable for using with Graphviz.

        >>> dg = DependencyGraph(
        ...     'John N 2\n'
        ...     'loves V 0\n'
        ...     'Mary N 2'
        ... )
        >>> print(dg.to_dot())
        digraph G{
        edge [dir=forward]
        node [shape=plaintext]
        <BLANKLINE>
        0 [label="0 (None)"]
        0 -> 2 [label="ROOT"]
        1 [label="1 (John)"]
        2 [label="2 (loves)"]
        2 -> 1 [label=""]
        2 -> 3 [label=""]
        3 [label="3 (Mary)"]
        }

        zdigraph G{
zedge [dir=forward]
znode [shape=plaintext]
c                 ó   — | d         S ©Nr   r   )Úvs    r   r   z(DependencyGraph.to_dot.<locals>.<lambda>«   s
   € ¸aÀ	¼l€ r   )Úkeyz
{} [label="{} ({})"]r   r   r   Nz
{} -> {} [label="{}"]z

{} -> {} z
})Úsortedr    r+   ÚformatÚitems)r$   Úsr/   r   r   r1   s         r   Úto_dotzDependencyGraph.to_dotŽ   s   € ð0 ˆØ	Ð#Ñ#ˆØ	Ð'Ñ'ˆõ ˜4œ:×,Ò,Ñ.Ô.Ð4JÐ4JÐKÑKÔKð 	Hð 	HˆDØÐ)×0Ò0Ø�Y”Ø�Y”Ø�V”ñô ñ ˆAð
 " &œ\×/Ò/Ñ1Ô1ð Hð H‘	��TØð Hð H�CØ�ØÐ6×=Ò=¸dÀ9¼oÈsÐTWÑXÔXÑX˜˜à˜]×1Ò1°$°y´/À3ÑGÔGÑG˜˜ð	HðHð 	
ˆU‰
ˆàˆr   c                 óH   — |                       ¦   «         }t          |¦  «        S )a�  Show SVG representation of the transducer (IPython magic).
        >>> from nltk.test.setup_fixt import check_binary
        >>> check_binary('dot')
        >>> dg = DependencyGraph(
        ...     'John N 2\n'
        ...     'loves V 0\n'
        ...     'Mary N 2'
        ... )
        >>> dg._repr_svg_().split('\n')[0]
        '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'

        )rL   Údot2img)r$   Ú
dot_strings     r   Ú
_repr_svg_zDependencyGraph._repr_svg_»   s   € ð —[’[‘]”]ˆ
Ý�zÑ"Ô"Ð"r   c                 ó*   — t          | j        ¦  «        S ©N)r   r    ©r$   s    r   Ú__str__zDependencyGraph.__str__Ë   s   € Ý�t”zÑ"Ô"Ð"r   c                 ó2   — dt          | j        ¦  «        › d�S )Nz<DependencyGraph with z nodes>)Úlenr    rS   s    r   Ú__repr__zDependencyGraph.__repr__Î   s   € Ø@­¨D¬J©¬Ð@Ð@Ð@Ð@r   c                 óÄ   ‡‡‡— t          | ¦  «        5 }ˆˆˆfd„|                     ¦   «                              d¦  «        D ¦   «         cddd¦  «         S # 1 swxY w Y   dS )aû  
        :param filename: a name of a file in Malt-TAB format
        :param zero_based: nodes in the input file are numbered starting from 0
            rather than 1 (as produced by, e.g., zpar)
        :param str cell_separator: the cell separator. If not provided, cells
            are split by whitespace.
        :param str top_relation_label: the label by which the top relation is
            identified, for examlple, `ROOT`, `null` or `TOP`.

        :return: a list of DependencyGraphs

        c                 ó6   •— g | ]}t          |‰‰‰¬ ¦  «        ‘ŒS ))r   r   r   ©r	   )Ú.0r%   r   r   r   s     €€€r   ú
<listcomp>z(DependencyGraph.load.<locals>.<listcomp>â   sF   ø€ ð ð ð ð õ  ØØ)Ø#1Ø'9ð	ñ ô ðð ð r   ú

N)ÚopenÚreadÚsplit)Úfilenamer   r   r   Úinfiles    ``` r   ÚloadzDependencyGraph.loadÑ   s¼   øøø€ õ  �(‰^Œ^ð 		˜vðð ð ð ð ð ð !'§¢¡¤× 3Ò 3°FÑ ;Ô ;ðñ ô ð		ð 		ð 		ð 		ñ 		ô 		ð 		ð 		ð 		ð 		ð 		ð 		øøøð 		ð 		ð 		ð 		ð 		ð 		s   “5AÁAÁAc                 óÎ   ‡— t          j        | j        |         d                              ¦   «         ¦  «        }| j        |         d         Št	          ˆfd„|D ¦   «         ¦  «        S )zl
        Returns the number of left children under the node specified
        by the given address.
        r   r   c              3   ó(   •K  — | ]}|‰k     ¯d V — ŒdS ©é   Nr   ©r[   ÚcÚindexs     €r   ú	<genexpr>z0DependencyGraph.left_children.<locals>.<genexpr>ó   ó'   øè è € Ð4Ð4˜¨!¨eª)¨)�1¨)¨)¨)¨)Ð4Ð4r   ©r   Úfrom_iterabler    r+   Úsum©r$   Ú
node_indexÚchildrenrj   s      @r   Úleft_childrenzDependencyGraph.left_childrenì   óa   ø€ õ
 Ô& t¤z°*Ô'=¸fÔ'E×'LÒ'LÑ'NÔ'NÑOÔOˆØ”
˜:Ô& yÔ1ˆÝÐ4Ð4Ð4Ð4˜hÐ4Ñ4Ô4Ñ4Ô4Ð4r   c                 óÎ   ‡— t          j        | j        |         d                              ¦   «         ¦  «        }| j        |         d         Št	          ˆfd„|D ¦   «         ¦  «        S )zm
        Returns the number of right children under the node specified
        by the given address.
        r   r   c              3   ó(   •K  — | ]}|‰k    ¯d V — ŒdS rf   r   rh   s     €r   rk   z1DependencyGraph.right_children.<locals>.<genexpr>ü   rl   r   rm   rp   s      @r   Úright_childrenzDependencyGraph.right_childrenõ   rt   r   c                 óŒ   — |                       |d         ¦  «        s(| j        |d                                       |¦  «         d S d S rE   )rB   r    r!   )r$   r/   s     r   Úadd_nodezDependencyGraph.add_nodeþ   sK   € Ø×$Ò$ T¨)¤_Ñ5Ô5ð 	5ØŒJ�t˜I”Ô'×.Ò.¨tÑ4Ô4Ð4Ð4Ð4ð	5ð 	5r   c                 óD  — d„ }d„ }d„ }d„ }	||||	dœ}
t          |t          ¦  «        rd„ |                     d¦  «        D ¦   «         }d„ |D ¦   «         }d	„ |D ¦   «         }d
}t          |d¬¦  «        D �]D\  }}|                     |¦  «        }|€t	          |¦  «        }n|t	          |¦  «        k    sJ ‚|€?	 |
|         }n5# t
          $ r(}t          d                     |¦  «        ¦  «        |‚d
}~ww xY w	  |||¦  «        \  }}}}}}}}n*# t          t          f$ r  ||¦  «        \  }}}}}}}Y nw xY w|dk    rŒËt          |¦  «        }|r|dz  }| j
        |                              ||||||||dœ¦  «         |dk    r|dk    r|}| j
        |         d         |                              |¦  «         �ŒF| j
        d         d         |         r:| j
        d         d         |         d         }| j
        |         | _        || _        d
S t          j        d¦  «         d
S )a½  Parse a sentence.

        :param extractor: a function that given a tuple of cells returns a
        7-tuple, where the values are ``word, lemma, ctag, tag, feats, head,
        rel``.

        :param str cell_separator: the cell separator. If not provided, cells
        are split by whitespace.

        :param str top_relation_label: the label by which the top relation is
        identified, for examlple, `ROOT`, `null` or `TOP`.

        c                 ó"   — | \  }}}|||||d|dfS ©NÚ r   )Úcellsrj   r   r   r   s        r   Úextract_3_cellsz/DependencyGraph._parse.<locals>.extract_3_cells  s$   € Ø#‰OˆD�#�tØ˜$  c¨3°°D¸"Ð<Ð<r   c                 ó$   — | \  }}}}|||||d||fS r|   r   )r~   rj   r   r   r   r   s         r   Úextract_4_cellsz/DependencyGraph._parse.<locals>.extract_4_cells  s'   € Ø#(Ñ ˆD�#�t˜SØ˜$  c¨3°°D¸#Ð=Ð=r   c                 ól   — | \  }}}}}}}	 t          |¦  «        }n# t          $ r Y nw xY w|||||d||fS r|   ©ÚintÚ
ValueError)	r~   rj   Ú
line_indexr   r   r   Ú_r   r   s	            r   Úextract_7_cellsz/DependencyGraph._parse.<locals>.extract_7_cells   sa   € Ø9>Ñ6ˆJ˜˜e S¨!¨T°3ðÝ˜J™œ��øÝð ð ð à�ðøøøð ˜$  s¨C°°T¸3Ð>Ð>s   Œ œ
)¨)c           
      ór   — | \
  }}}}}}}}	}
}
	 t          |¦  «        }n# t          $ r Y nw xY w||||||||	fS rR   rƒ   )r~   rj   r†   r   r   r   r   r   r   r   r‡   s              r   Úextract_10_cellsz0DependencyGraph._parse.<locals>.extract_10_cells)  sg   € ØINÑFˆJ˜˜e T¨3°°t¸SÀ!ÀQðÝ˜J™œ��øÝð ð ð à�ðøøøð ˜$  t¨S°%¸¸sÐBÐBs   � Ÿ
,«,)é   é   é   é
   c              3   ó   K  — | ]}|V — Œd S rR   r   )r[   Úlines     r   rk   z)DependencyGraph._parse.<locals>.<genexpr>:  s"   è è € Ð:Ð:˜t�dÐ:Ð:Ð:Ð:Ð:Ð:r   Ú
c              3   ó>   K  — | ]}|                      ¦   «         V — Œd S rR   )Úrstrip©r[   Úls     r   rk   z)DependencyGraph._parse.<locals>.<genexpr><  s*   è è € Ð,Ð, �—’‘”Ð,Ð,Ð,Ð,Ð,Ð,r   c              3   ó   K  — | ]}|¯|V — Œ	d S rR   r   r”   s     r   rk   z)DependencyGraph._parse.<locals>.<genexpr>=  s'   è è € Ð'Ð'�q QÐ'�Ð'Ð'Ð'Ð'Ð'Ð'r   Nrg   )ÚstartúTNumber of tab-delimited fields ({}) not supported by CoNLL(10) or Malt-Tab(4) formatr‡   )r   r   r   r   r   r   r   r   r‹   r   r   zBThe graph doesn't contain a node that depends on the root element.)Ú
isinstanceÚstrr`   Ú	enumeraterV   ÚKeyErrorr…   rI   Ú	TypeErrorr„   r    r!   r,   r"   r   ÚwarningsÚwarn)r$   Úinput_r   r   r   r   r   r�   rˆ   rŠ   Ú
extractorsÚlinesÚcell_numberrj   r�   r~   Úer   r   r   r   r   r   r   Úroot_addresss                            r   r#   zDependencyGraph._parse  s  € ð,	=ð 	=ð 	=ð	>ð 	>ð 	>ð	?ð 	?ð 	?ð	Cð 	Cð 	Cð ØØØ ð	
ð 
ˆ
õ �f�cÑ"Ô"ð 	;Ø:Ð: v§|¢|°DÑ'9Ô'9Ð:Ñ:Ô:ˆFà,Ð, VÐ,Ñ,Ô,ˆØ'Ð'˜EÐ'Ñ'Ô'ˆàˆÝ$ U°!Ð4Ñ4Ô4ð 1	8ñ 1	8‰KˆE�4Ø—J’J˜~Ñ.Ô.ˆEØÐ"Ý! %™jœj��à"¥c¨%¡j¤jÒ0Ð0Ð0Ð0àÐ%ðØ%/°Ô%<�N�NøÝð ð ð Ý$ð:ß:@º&ÀÑ:MÔ:Mñô ð ðøøøøðøøøðQØBPÀ.Ø˜5ñCô CÑ?��t˜U D¨#¨u°d¸C¸Cøõ �zÐ*ð Qð Qð Qð <J¸>È%Ñ;PÔ;PÑ8��e˜T 3¨¨t°S°S°Sð	Qøøøð �sŠ{ˆ{Øå�t‘9”9ˆDØð Ø˜‘	�àŒJ�uÔ×$Ò$à$Ø Ø"Ø ØØ"Ø Øð	ð 	ñô ð ð ˜qÒ Ð  t¨q¢y yØ(�ØŒJ�tÔ˜VÔ$ SÔ)×0Ò0°Ñ7Ô7Ð7Ñ7àŒ:�aŒ=˜Ô Ð!3Ô4ð 	Øœ: aœ=¨Ô0Ð1CÔDÀQÔGˆLØœ
 <Ô0ˆDŒIØ&8ˆDÔ#Ð#Ð#åŒMØWñô ð ð ð s*   Â8CÃ
C3Ã#C.Ã.C3Ã7DÄ$D4Ä3D4Tc                 ó*   — |d         }|r|dk    r|S |S )Nr   ú,r   )r$   r/   ÚfilterÚws       r   Ú_wordzDependencyGraph._word|  s&   € Ø�ŒLˆØð 	Ø�CŠxˆxØ�Øˆr   c                 óð   ‡ — ‰                       |¦  «        }|d         }t          t          j        |d                              ¦   «         ¦  «        ¦  «        }|rt          |ˆ fd„|D ¦   «         ¦  «        S |S )z¥Turn dependency graphs into NLTK trees.

        :param int i: index of a node
        :return: either a word (if the indexed node is a leaf) or a ``Tree``.
        r   r   c                 ó:   •— g | ]}‰                      |¦  «        ‘ŒS r   ©Ú_tree©r[   r1   r$   s     €r   r\   z)DependencyGraph._tree.<locals>.<listcomp>Ž  s#   ø€ Ð?Ð?Ð?°3˜tŸzšz¨#™œÐ?Ð?Ð?r   )r@   rH   r   rn   r+   r   )r$   Úir/   r   r   s   `    r   r®   zDependencyGraph._treeƒ  s{   ø€ ð ×"Ò" 1Ñ%Ô%ˆØ�FŒ|ˆÝ•eÔ)¨$¨v¬,×*=Ò*=Ñ*?Ô*?Ñ@Ô@ÑAÔAˆàð 	Ý˜Ð?Ð?Ð?Ð?¸$Ð?Ñ?Ô?Ñ@Ô@Ð@àˆKr   c                 óÌ   ‡ — ‰ j         }|d         }t          t          j        |d                              ¦   «         ¦  «        ¦  «        }t          |ˆ fd„|D ¦   «         ¦  «        S )z–
        Starting with the ``root`` node, build a dependency tree using the NLTK
        ``Tree`` constructor. Dependency labels are omitted.
        r   r   c                 ó:   •— g | ]}‰                      |¦  «        ‘ŒS r   r­   r¯   s     €r   r\   z(DependencyGraph.tree.<locals>.<listcomp>›  s#   ø€ Ð;Ð;Ð;¨s˜4Ÿ:š: c™?œ?Ð;Ð;Ð;r   )r"   rH   r   rn   r+   r   )r$   r/   r   r   s   `   r   ÚtreezDependencyGraph.tree’  sb   ø€ ð
 Œyˆà�FŒ|ˆÝ•eÔ)¨$¨v¬,×*=Ò*=Ñ*?Ô*?Ñ@Ô@ÑAÔAˆÝ�DÐ;Ð;Ð;Ð;°dÐ;Ñ;Ô;Ñ<Ô<Ð<r   c              #   óL  K  — |s| j         }|d         |d         f}t          t          j        |d                              ¦   «         ¦  «        ¦  «        D ]N}|                      |¦  «        }||d         |d         |d         ffV — |                      |¬¦  «        E d{V —† ŒOdS )zs
        Extract dependency triples of the form:
        ((head word, head tag), rel, (dep word, dep tag))
        r   r   r   r   )r/   N)r"   rH   r   rn   r+   r@   Útriples)r$   r/   r   r°   r1   s        r   rµ   zDependencyGraph.triples�  sÅ   è è € ð ð 	Ø”9ˆDà�V”˜d 6œlÐ+ˆÝ�Ô+¨D°¬L×,?Ò,?Ñ,AÔ,AÑBÔBÑCÔCð 	.ð 	.ˆAØ×%Ò% aÑ(Ô(ˆCØ˜˜Uœ c¨&¤k°3°v´;Ð%?Ð@Ð@Ð@Ð@Ø—|’|¨�|Ñ-Ô-Ð-Ð-Ð-Ð-Ð-Ð-Ð-Ð-ð	.ð 	.r   c                 óL   — 	 | j         |         d         S # t          $ r Y d S w xY w)Nr   ©r    Ú
IndexError©r$   r°   s     r   Ú_hdzDependencyGraph._hd¬  s:   € ð	Ø”:˜a”= Ô(Ð(øÝð 	ð 	ð 	Ø�4�4ð	øøøó   ‚ •
#¢#c                 óL   — 	 | j         |         d         S # t          $ r Y d S w xY w)Nr   r·   r¹   s     r   Ú_relzDependencyGraph._rel²  s:   € ð	Ø”:˜a”= Ô'Ð'øÝð 	ð 	ð 	Ø�4�4ð	øøør»   c                 óü  — i }| j                              ¦   «         D ])}|d         D ]}t          |d         |g¦  «        }d||<   ŒŒ*| j         D ]®}i }|D ]J}|D ]E}|d         |d         k    r1t          |d         |d         g¦  «        }||         ||         z   ||<   ŒFŒK|D ]Z}	||	         ||	<   |	d         |	d         k    r;|                      |                      |	d         ¦  «        |	d         ¦  «        }
|
c c S Œ[Œ¯dS )aE  Check whether there are cycles.

        >>> dg = DependencyGraph(treebank_data)
        >>> dg.contains_cycle()
        False

        >>> cyclic_dg = DependencyGraph()
        >>> top = {'word': None, 'deps': [1], 'rel': 'TOP', 'address': 0}
        >>> child1 = {'word': None, 'deps': [2], 'rel': 'NTOP', 'address': 1}
        >>> child2 = {'word': None, 'deps': [4], 'rel': 'NTOP', 'address': 2}
        >>> child3 = {'word': None, 'deps': [1], 'rel': 'NTOP', 'address': 3}
        >>> child4 = {'word': None, 'deps': [3], 'rel': 'NTOP', 'address': 4}
        >>> cyclic_dg.nodes = {
        ...     0: top,
        ...     1: child1,
        ...     2: child2,
        ...     3: child3,
        ...     4: child4,
        ... }
        >>> cyclic_dg.root = top

        >>> cyclic_dg.contains_cycle()
        [1, 2, 4, 3]

        r   r   rg   r   F)r    r+   ÚtupleÚget_cycle_pathr@   )r$   Ú	distancesr/   r1   rG   r‡   Únew_entriesÚpair1Úpair2ÚpairÚpaths              r   Úcontains_cyclezDependencyGraph.contains_cycle¹  sb  € ð4 ˆ	à”J×%Ò%Ñ'Ô'ð 	#ð 	#ˆDØ˜F”|ð #ð #�Ý˜T )œ_¨cÐ2Ñ3Ô3�Ø!"�	˜#‘�ð#ð ”ð 	 ð 	 ˆAØˆKà"ð Oð O�Ø&ð Oð O�EØ˜Q”x 5¨¤8Ò+Ð+Ý# U¨1¤X¨u°Q¬xÐ$8Ñ9Ô9˜Ø+4°UÔ+;¸iÈÔ>NÑ+N˜ CÑ(øðOð
 $ð  ð  �Ø"-¨dÔ"3�	˜$‘Ø˜”7˜d 1œgÒ%Ð%Ø×.Ò.¨t×/BÒ/BÀ4ÈÄ7Ñ/KÔ/KÈTÐRSÌWÑUÔU�DØ�K�K�K�K�Kð &ð ð ˆur   c                 ó  — |d         D ]}||k    r|d         gc S Œ|d         D ]^}|                       |                      |¦  «        |¦  «        }t          |¦  «        dk    r |                     d|d         ¦  «         |c S Œ_g S )Nr   r   r   )rÀ   r@   rV   Úinsert)r$   Ú	curr_nodeÚgoal_node_indexr1   rÆ   s        r   rÀ   zDependencyGraph.get_cycle_pathë  s®   € Ø˜VÔ$ð 	.ð 	.ˆCØ�oÒ%Ð%Ø! )Ô,Ð-Ð-Ð-Ð-ð &à˜VÔ$ð 	ð 	ˆCØ×&Ò& t×':Ò':¸3Ñ'?Ô'?ÀÑQÔQˆDÝ�4‰yŒy˜1Š}ˆ}Ø—’˜A˜y¨Ô3Ñ4Ô4Ð4Ø���ð ð ˆ	r   c                 ó  ‡— |dk    rdŠn4|dk    rdŠn+|dk    rdŠn"t          d                     |¦  «        ¦  «        ‚d                     ˆfd	„t          | j                             ¦   «         ¦  «        D ¦   «         ¦  «        S )
z®
        The dependency graph in CoNLL format.

        :param style: the style to use for the format (3, 4, 10 columns)
        :type style: int
        :rtype: str
        r‹   z{word}	{tag}	{head}
rŒ   z{word}	{tag}	{head}	{rel}
rŽ   z9{i}	{word}	{lemma}	{ctag}	{tag}	{feats}	{head}	{rel}	_	_
r˜   r}   c              3   óT   •K  — | ]"\  }}|d          dk    ¯ ‰j         dd|i|¤ŽV — Œ#dS )r   r   r°   Nr   )rI   )r[   r°   r/   Útemplates      €r   rk   z+DependencyGraph.to_conll.<locals>.<genexpr>  sY   øè è € ð 
ð 
á��4Ø�EŒ{˜eÒ#Ð#ð ˆHŒOÐ(Ð(˜aÐ( 4Ð(Ð(à#Ð#Ð#Ð#ð
ð 
r   )r…   rI   ÚjoinrH   r    rJ   )r$   ÚstylerÎ   s     @r   Úto_conllzDependencyGraph.to_conllö  s°   ø€ ð �AŠ:ˆ:Ø0ˆHˆHØ�aŠZˆZØ7ˆHˆHØ�bŠ[ˆ[àUð ˆHõ ð2ß28²&¸±-´-ñô ð ð
 �wŠwð 
ð 
ð 
ð 
å! $¤*×"2Ò"2Ñ"4Ô"4Ñ5Ô5ð
ñ 
ô 
ñ 
ô 
ð 	
r   c                 óT  ‡ — ddl }t          t          dt          ‰ j        ¦  «        ¦  «        ¦  «        }ˆ fd„|D ¦   «         }i ‰ _        |D ]}‰ j        |         d         ‰ j        |<   Œ|                     ¦   «         }|                     |¦  «         |                     |¦  «         |S )zJConvert the data in a ``nodelist`` into a networkx labeled directed graph.r   Nrg   c                 ó�   •— g | ]B}‰                      |¦  «        ¯|‰                      |¦  «        ‰                     |¦  «        f‘ŒCS r   )rº   r½   )r[   Únr$   s     €r   r\   z,DependencyGraph.nx_graph.<locals>.<listcomp>  sS   ø€ ð 
ð 
ð 
Ø/0À4Ç8Â8ÈAÁ;Ä;ð
Ø�—’˜‘”˜TŸYšY q™\œ\Ð*ð
ð 
ð 
r   r   )	Únetworkxr   ÚrangerV   r    Ú	nx_labelsÚMultiDiGraphÚadd_nodes_fromÚadd_edges_from)r$   rÕ   Únx_nodelistÚnx_edgelistrÔ   Úgs   `     r   Únx_graphzDependencyGraph.nx_graph  sÃ   ø€ àˆˆˆå�5 ¥C¨¬
¡O¤OÑ4Ô4Ñ5Ô5ˆð
ð 
ð 
ð 
Ø4?ð
ñ 
ô 
ˆð ˆŒØð 	6ð 	6ˆAØ $¤
¨1¤¨fÔ 5ˆDŒN˜1ÑÐà×!Ò!Ñ#Ô#ˆØ	×Ò˜Ñ%Ô%Ð%Ø	×Ò˜Ñ%Ô%Ð%àˆr   )NNFNr
   )FNr
   )NFNr
   )TrR   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r&   r)   r2   r8   r<   r@   rB   rL   rP   rT   rW   Ústaticmethodrc   rs   rw   ry   r#   rª   r®   r³   rµ   rº   r½   rÇ   rÀ   rÑ   rÞ   r   r   r   r	   r	      sü  € € € € € ðð ð ØØØØ!ð1ð 1ð 1ð 1ðf ð  ð  ð$ð $ð $ðGð Gð Gð
Eð 
Eð 
Eð(ð (ð (ð*ð *ð *ð+ð +ð +ðZ#ð #ð #ð #ð #ð #ðAð Að Að àLRðð ð ñ „\ðð45ð 5ð 5ð5ð 5ð 5ð5ð 5ð 5ð ØØØ!ðxð xð xð xðtð ð ð ðð ð ð	=ð 	=ð 	=ð.ð .ð .ð .ðð ð ðð ð ð0ð 0ð 0ðd	ð 	ð 	ð
ð 
ð 
ð:ð ð ð ð r   r	   Úsvgc                 ó\  — 	 t          d¦  «         	 |dv rt          j        dd|z  gd| d¬¦  «        }n*t          j        dd|z  gt          | d¬¦  «        ¬¦  «        }|j        S #  t          d	                     | ¦  «        ¦  «        ‚xY w# t          $ r}t          d
¦  «        |‚d}~ww xY w)a­  
    Create image representation fom dot_string, using the 'dot' program
    from the Graphviz package.

    Use the 't' argument to specify the image file format, for ex. 'jpeg', 'eps',
    'json', 'png' or 'webp' (Running 'dot -T:' lists all available formats).

    Note that the "capture_output" option of subprocess.run() is only available
    with text formats (like svg), but not with binary image formats (like png).
    Údot)ræ   Údot_jsonÚjsonrä   z-T%sT)Úcapture_outputÚinputÚtextÚutf8)Úencoding)rê   zACannot create image representation by running dot from string: {}z0Cannot find the dot binary from Graphviz packageN)r   Ú
subprocessÚrunÚbytesÚstdoutÚ	ExceptionrI   ÚOSError)rO   ÚtÚprocr¤   s       r   rN   rN   &  sö   € ðSÝ�EÑÔÐð	ØÐ6Ð6Ð6Ý!”~Ø˜F Q™JÐ'Ø#'Ø$Øð	ñ ô ��õ "”~Ø˜F Q™JÐ'Ý 
°VÐ<Ñ<Ô<ðñ ô �ð ”;Ðøð	Ýðß’6˜*Ñ%Ô%ñô ð øøøøõ ð Sð Sð SÝÐJÑKÔKÐQRÐRøøøøðSøøøs)   ‚B ’AA% Á%$B	Â	B Â
B+ÂB&Â&B+c                   ó   — e Zd ZdZdS )ÚDependencyGraphErrorzDependency graph exception.N)rß   rà   rá   râ   r   r   r   r÷   r÷   K  s   € € € € € Ø%Ð%Ð%Ð%r   r÷   c                  óv   — t          ¦   «          t          ¦   «          t          ¦   «          t          ¦   «          d S rR   )Ú	malt_demoÚ
conll_demoÚconll_file_demoÚcycle_finding_demor   r   r   Údemorý   O  s2   € Ý�K„K€KÝ�L„L€LÝÑÔÐÝÑÔÐÐÐr   Fc                 ó  — t          d¦  «        }|                     ¦   «         }|                     ¦   «          | rÒddl}ddlm} |                     ¦   «         }|                     ¦   «          |                     |d¬¦  «        }| 	                    ||d¬¦  «         | 
                    |||j        ¦  «         |                     g ¦  «         |                     g ¦  «         |                     d	¦  «         |                     ¦   «          dS dS )
zw
    A demonstration of the result of reading a dependency
    version of the first sentence of the Penn Treebank.
    á  Pierre  NNP     2       NMOD
Vinken  NNP     8       SUB
,       ,       2       P
61      CD      5       NMOD
years   NNS     6       AMOD
old     JJ      2       NMOD
,       ,       2       P
will    MD      0       ROOT
join    VB      8       VC
the     DT      11      NMOD
board   NN      9       OBJ
as      IN      9       VMOD
a       DT      15      NMOD
nonexecutive    JJ      15      NMOD
director        NN      12      PMOD
Nov.    NNP     9       VMOD
29      CD      16      NMOD
.       .       9       VMOD
r   N)Úpylabrg   )Údimé2   )Ú	node_sizeztree.png)r	   r³   ÚpprintrÕ   Ú
matplotlibr   rÞ   ÚinfoÚspring_layoutÚdraw_networkx_nodesÚdraw_networkx_labelsr×   ÚxticksÚyticksÚsavefigÚshow)ÚnxÚdgr³   rÕ   r   rÝ   Úposs          r   rù   rù   V  s  € õ
 
ð	ñ
ô 
€Bð* �7Š7‰9Œ9€DØ‡K‚K�M„M€MØ	ð àˆˆˆØ$Ð$Ð$Ð$Ð$Ð$à�KŠK‰MŒMˆØ	�Š‰ŒˆØ×$Ò$ Q¨AÐ$Ñ.Ô.ˆØ×$Ò$ Q¨°rÐ$Ñ:Ô:Ð:à×%Ò% a¨¨b¬lÑ;Ô;Ð;Ø�Š�RÑÔÐØ�Š�RÑÔÐØ�Š�jÑ!Ô!Ð!Ø�
Š
‰Œˆˆˆðð r   c                  óà   — t          t          ¦  «        } |                      ¦   «         }|                     ¦   «          t	          | ¦  «         t	          |                      d¦  «        ¦  «         dS )zg
    A demonstration of how to read a string representation of
    a CoNLL format dependency tree.
    rŒ   N)r	   Úconll_data1r³   r  ÚprintrÑ   )r  r³   s     r   rú   rú   ƒ  sT   € õ
 
�Ñ	%Ô	%€BØ�7Š7‰9Œ9€DØ‡K‚K�M„M€MÝ	ˆ"�I„I€IÝ	ˆ"�+Š+�a‰.Œ.ÑÔÐÐÐr   c                  óä   — t          d¦  «         d„ t                               d¦  «        D ¦   «         } | D ]9}|                     ¦   «         }t          d¦  «         |                     ¦   «          Œ:d S )NzMass conll_read demo...c                 ó0   — g | ]}|¯t          |¦  «        ‘ŒS r   rZ   )r[   Úentrys     r   r\   z#conll_file_demo.<locals>.<listcomp>‘  s%   € ÐUÐUÐU¨ÈuÐU�o˜eÑ$Ô$ÐUÐUÐUr   r]   r‘   )r  Úconll_data2r`   r³   r  )ÚgraphsÚgraphr³   s      r   rû   rû   �  sr   € Ý	Ð
#Ñ$Ô$Ð$ØUÐUµ+×2CÒ2CÀFÑ2KÔ2KÐUÑUÔU€FØð ð ˆØ�zŠz‰|Œ|ˆÝˆd‰ŒˆØ�Š‰Œˆˆðð r   c                  óÜ  — t          t          ¦  «        } t          |                      ¦   «         ¦  «         t          ¦   «         }|                     d dgdddœ¦  «         |                     d dgdddœ¦  «         |                     d dgdddœ¦  «         |                     d dgdddœ¦  «         |                     d dgdddœ¦  «         t          |                     ¦   «         ¦  «         d S )	Nrg   r   r   )r   r   r   r   é   ÚNTOPrŒ   r‹   )r	   Útreebank_datar  rÇ   ry   )r  Ú	cyclic_dgs     r   rü   rü   ˜  sþ   € Ý	�Ñ	'Ô	'€BÝ	ˆ"×
Ò
Ñ
Ô
ÑÔÐÝÑ!Ô!€IØ×Ò ¨q¨c¸%ÈAÐNÐNÑOÔOÐOØ×Ò ¨q¨c¸&ÈQÐOÐOÑPÔPÐPØ×Ò ¨q¨c¸&ÈQÐOÐOÑPÔPÐPØ×Ò ¨q¨c¸&ÈQÐOÐOÑPÔPÐPØ×Ò ¨q¨c¸&ÈQÐOÐOÑPÔPÐPÝ	ˆ)×
"Ò
"Ñ
$Ô
$Ñ%Ô%Ð%Ð%Ð%r   rÿ   a/  
1   Ze                ze                Pron  Pron  per|3|evofmv|nom                 2   su      _  _
2   had               heb               V     V     trans|ovt|1of2of3|ev             0   ROOT    _  _
3   met               met               Prep  Prep  voor                             8   mod     _  _
4   haar              haar              Pron  Pron  bez|3|ev|neut|attr               5   det     _  _
5   moeder            moeder            N     N     soort|ev|neut                    3   obj1    _  _
6   kunnen            kan               V     V     hulp|ott|1of2of3|mv              2   vc      _  _
7   gaan              ga                V     V     hulp|inf                         6   vc      _  _
8   winkelen          winkel            V     V     intrans|inf                      11  cnj     _  _
9   ,                 ,                 Punc  Punc  komma                            8   punct   _  _
10  zwemmen           zwem              V     V     intrans|inf                      11  cnj     _  _
11  of                of                Conj  Conj  neven                            7   vc      _  _
12  terrassen         terras            N     N     soort|mv|neut                    11  cnj     _  _
13  .                 .                 Punc  Punc  punt                             12  punct   _  _
a  1   Cathy             Cathy             N     N     eigen|ev|neut                    2   su      _  _
2   zag               zie               V     V     trans|ovt|1of2of3|ev             0   ROOT    _  _
3   hen               hen               Pron  Pron  per|3|mv|datofacc                2   obj1    _  _
4   wild              wild              Adj   Adj   attr|stell|onverv                5   mod     _  _
5   zwaaien           zwaai             N     N     soort|mv|neut                    2   vc      _  _
6   .                 .                 Punc  Punc  punt                             5   punct   _  _

1   Ze                ze                Pron  Pron  per|3|evofmv|nom                 2   su      _  _
2   had               heb               V     V     trans|ovt|1of2of3|ev             0   ROOT    _  _
3   met               met               Prep  Prep  voor                             8   mod     _  _
4   haar              haar              Pron  Pron  bez|3|ev|neut|attr               5   det     _  _
5   moeder            moeder            N     N     soort|ev|neut                    3   obj1    _  _
6   kunnen            kan               V     V     hulp|ott|1of2of3|mv              2   vc      _  _
7   gaan              ga                V     V     hulp|inf                         6   vc      _  _
8   winkelen          winkel            V     V     intrans|inf                      11  cnj     _  _
9   ,                 ,                 Punc  Punc  komma                            8   punct   _  _
10  zwemmen           zwem              V     V     intrans|inf                      11  cnj     _  _
11  of                of                Conj  Conj  neven                            7   vc      _  _
12  terrassen         terras            N     N     soort|mv|neut                    11  cnj     _  _
13  .                 .                 Punc  Punc  punt                             12  punct   _  _

1   Dat               dat               Pron  Pron  aanw|neut|attr                   2   det     _  _
2   werkwoord         werkwoord         N     N     soort|ev|neut                    6   obj1    _  _
3   had               heb               V     V     hulp|ovt|1of2of3|ev              0   ROOT    _  _
4   ze                ze                Pron  Pron  per|3|evofmv|nom                 6   su      _  _
5   zelf              zelf              Pron  Pron  aanw|neut|attr|wzelf             3   predm   _  _
6   uitgevonden       vind              V     V     trans|verldw|onverv              3   vc      _  _
7   .                 .                 Punc  Punc  punt                             6   punct   _  _

1   Het               het               Pron  Pron  onbep|neut|zelfst                2   su      _  _
2   hoorde            hoor              V     V     trans|ovt|1of2of3|ev             0   ROOT    _  _
3   bij               bij               Prep  Prep  voor                             2   ld      _  _
4   de                de                Art   Art   bep|zijdofmv|neut                6   det     _  _
5   warme             warm              Adj   Adj   attr|stell|vervneut              6   mod     _  _
6   zomerdag          zomerdag          N     N     soort|ev|neut                    3   obj1    _  _
7   die               die               Pron  Pron  betr|neut|zelfst                 6   mod     _  _
8   ze                ze                Pron  Pron  per|3|evofmv|nom                 12  su      _  _
9   ginds             ginds             Adv   Adv   gew|aanw                         12  mod     _  _
10  achter            achter            Adv   Adv   gew|geenfunc|stell|onverv        12  svp     _  _
11  had               heb               V     V     hulp|ovt|1of2of3|ev              7   body    _  _
12  gelaten           laat              V     V     trans|verldw|onverv              11  vc      _  _
13  .                 .                 Punc  Punc  punt                             12  punct   _  _

1   Ze                ze                Pron  Pron  per|3|evofmv|nom                 2   su      _  _
2   hadden            heb               V     V     trans|ovt|1of2of3|mv             0   ROOT    _  _
3   languit           languit           Adv   Adv   gew|geenfunc|stell|onverv        11  mod     _  _
4   naast             naast             Prep  Prep  voor                             11  mod     _  _
5   elkaar            elkaar            Pron  Pron  rec|neut                         4   obj1    _  _
6   op                op                Prep  Prep  voor                             11  ld      _  _
7   de                de                Art   Art   bep|zijdofmv|neut                8   det     _  _
8   strandstoelen     strandstoel       N     N     soort|mv|neut                    6   obj1    _  _
9   kunnen            kan               V     V     hulp|inf                         2   vc      _  _
10  gaan              ga                V     V     hulp|inf                         9   vc      _  _
11  liggen            lig               V     V     intrans|inf                      10  vc      _  _
12  .                 .                 Punc  Punc  punt                             11  punct   _  _

1   Zij               zij               Pron  Pron  per|3|evofmv|nom                 2   su      _  _
2   zou               zal               V     V     hulp|ovt|1of2of3|ev              7   cnj     _  _
3   mams              mams              N     N     soort|ev|neut                    4   det     _  _
4   rug               rug               N     N     soort|ev|neut                    5   obj1    _  _
5   ingewreven        wrijf             V     V     trans|verldw|onverv              6   vc      _  _
6   hebben            heb               V     V     hulp|inf                         2   vc      _  _
7   en                en                Conj  Conj  neven                            0   ROOT    _  _
8   mam               mam               V     V     trans|ovt|1of2of3|ev             7   cnj     _  _
9   de                de                Art   Art   bep|zijdofmv|neut                10  det     _  _
10  hare              hare              Pron  Pron  bez|3|ev|neut|attr               8   obj1    _  _
11  .                 .                 Punc  Punc  punt                             10  punct   _  _

1   Of                of                Conj  Conj  onder|metfin                     0   ROOT    _  _
2   ze                ze                Pron  Pron  per|3|evofmv|nom                 3   su      _  _
3   had               heb               V     V     hulp|ovt|1of2of3|ev              0   ROOT    _  _
4   gewoon            gewoon            Adj   Adj   adv|stell|onverv                 10  mod     _  _
5   met               met               Prep  Prep  voor                             10  mod     _  _
6   haar              haar              Pron  Pron  bez|3|ev|neut|attr               7   det     _  _
7   vriendinnen       vriendin          N     N     soort|mv|neut                    5   obj1    _  _
8   rond              rond              Adv   Adv   deelv                            10  svp     _  _
9   kunnen            kan               V     V     hulp|inf                         3   vc      _  _
10  slenteren         slenter           V     V     intrans|inf                      9   vc      _  _
11  in                in                Prep  Prep  voor                             10  mod     _  _
12  de                de                Art   Art   bep|zijdofmv|neut                13  det     _  _
13  buurt             buurt             N     N     soort|ev|neut                    11  obj1    _  _
14  van               van               Prep  Prep  voor                             13  mod     _  _
15  Trafalgar_Square  Trafalgar_Square  MWU   N_N   eigen|ev|neut_eigen|ev|neut      14  obj1    _  _
16  .                 .                 Punc  Punc  punt                             15  punct   _  _
Ú__main__)rä   )F)râ   rî   rž   Úcollectionsr   Ú	itertoolsr   r  r   Únltk.internalsr   Ú	nltk.treer   r	   rN   rò   r÷   rý   rù   rú   rû   rü   r  r  r  rß   r   r   r   ú<module>r$     s€  ððð ð Ð Ð Ð Ø €€€Ø #Ð #Ð #Ð #Ð #Ð #Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð à &Ð &Ð &Ð &Ð &Ð &Ø Ð Ð Ð Ð Ð ðDð Dð Dð Dð Dñ Dô Dð DðN"Sð "Sð "Sð "SðJ&ð &ð &ð &ð &˜9ñ &ô &ð &ðð ð ð*ð *ð *ð *ðZ	ð 	ð 	ðð ð ð	&ð 	&ð 	&ð€ð(€ð T€ðl ˆzÒÐØ€D�F„F€F€F€Fð Ðr   