§
    íóÉje‚  ã                   óð   — 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 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)Úopen)Ú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© ó    ú}/home/lighthouse/badge-speaker-benchmark/.benchmark-venv-community/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_addressX   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_arcm   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,   r5   r-   )r%   Únode1Únode2r8   s       r   Úconnect_graphzDependencyGraph.connect_graphw   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%   Úsr0   r   r   r2   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"?>'

        )rM   Ú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!   rT   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)Ú_secure_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Úchildrenrk   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 rg   r   ri   s     €r   rl   z1DependencyGraph.right_children.<locals>.<genexpr>   rm   r   rn   rq   s      @r   Úright_childrenzDependencyGraph.right_childrenù   ru   r   c                 óŒ   — |                       |d         ¦  «        s(| j        |d                                       |¦  «         d S d S rF   )rC   r!   r"   )r%   r0   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   )Úcellsrk   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   rk   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   rk   Ú
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 rS   r„   )r   rk   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 rS   r   )r\   Úlines     r   rl   z)DependencyGraph._parse.<locals>.<genexpr>>  s"   è è € Ð:Ð:˜t�dÐ:Ð:Ð:Ð:Ð:Ð:r   ú
c              3   ó>   K  — | ]}|                      ¦   «         V — Œd S rS   )Úrstrip©r\   Úls     r   rl   z)DependencyGraph._parse.<locals>.<genexpr>@  s*   è è € Ð,Ð, �—’‘”Ð,Ð,Ð,Ð,Ð,Ð,r   c              3   ó   K  — | ]}|¯|V — Œ	d S rS   r   r•   s     r   rl   z)DependencyGraph._parse.<locals>.<genexpr>A  s'   è è € Ð'Ð'�q QÐ'�Ð'Ð'Ð'Ð'Ð'Ð'r   Nrh   )Ú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Ústrra   Ú	enumeraterW   ÚKeyErrorr†   rJ   Ú	TypeErrorr…   r!   r"   r-   r#   r    ÚwarningsÚwarn)r%   Úinput_r   r   r   r    r€   r‚   r‰   r‹   Ú
extractorsÚlinesÚcell_numberrk   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%   r0   Ú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\   r2   r%   s     €r   r]   z)DependencyGraph._tree.<locals>.<listcomp>’  s#   ø€ Ð?Ð?Ð?°3˜tŸzšz¨#™œÐ?Ð?Ð?r   )rA   rI   r   ro   r,   r   )r%   Úir0   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#   rI   r   ro   r,   r   )r%   r0   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   )r0   N)r#   rI   r   ro   r,   rA   Útriples)r%   r0   r   r±   r2   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                 óp  — d\  }}}t          t          ¦  «        }t          | j        ¦  «        D �]}||         |k    rŒ|||<   |g}t	          | j        |         d         ¦  «        g}|rÊ|d         D ]”}|| j        vrŒ||         |k    r!||                     |¦  «        d…         c c S ||         |k    rO|||<   |                     |¦  «         |                     t	          | j        |         d         ¦  «        ¦  «          n,Œ•|||                     ¦   «         <   |                     ¦   «          |°Ê�Œ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   rh   é   r   éÿÿÿÿNF)r   r…   r   r!   Úiterrk   r-   Úpop)	r%   ÚWHITEÚGRAYÚBLACKÚcolorr˜   ÚpathÚstackr2   s	            r   Úcontains_cyclezDependencyGraph.contains_cycle½  sd  € ðP %Ñˆˆt�UÝ�CÑ Ô ˆå˜$œ*Ñ%Ô%ð 	 ñ 	 ˆEØ�UŒ|˜uÒ$Ð$ØØˆE�%‰LØ�7ˆDÝ˜$œ* UÔ+¨FÔ3Ñ4Ô4Ð5ˆEØð  Ø  œ9ð  ð  �CØ $¤*Ð,Ð,Ø Ø˜S”z TÒ)Ð)ð  $ D§J¢J¨s¡O¤OÐ$5Ð$5Ô6Ð6Ð6Ð6Ð6Ð6Ø˜S”z UÒ*Ð*Ø%)˜˜c™
ØŸš CÑ(Ô(Ð(ØŸš¥T¨$¬*°S¬/¸&Ô*AÑ%BÔ%BÑCÔCÐCØ˜ð	 +ð ).�E˜$Ÿ(š(™*œ*Ñ%Ø—I’I‘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   )Úget_cycle_pathrA   rW   Úinsert)r%   Ú	curr_nodeÚgoal_node_indexr2   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   )rJ   )r\   r±   r0   Útemplates      €r   rl   z+DependencyGraph.to_conll.<locals>.<genexpr>#  sY   øè è € ð 
ð 
á��4Ø�EŒ{˜eÒ#Ð#ð ˆHŒOÐ(Ð(˜aÐ( 4Ð(Ð(à#Ð#Ð#Ð#ð
ð 
r   )r†   rJ   ÚjoinrI   r!   rK   )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   Nrh   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   ÚrangerW   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   )TrS   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r'   r*   r3   r9   r=   rA   rC   rM   rQ   rU   rX   Ústaticmethodrd   rt   rx   rz   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ðð ð ñ „\ðð:5ð 5ð 5ð5ð 5ð 5ð5ð 5ð 5ð ØØØ!ðxð xð xð xðtð ð ð ðð ð ð	=ð 	=ð 	=ð.ð .ð .ð .ðð ð ðð ð ðBð Bð BðH	ð 	ð 	ð
ð 
ð 
ð:ð ð ð ð r   r
   Úsvgc                 óp  — 	 t          d¦  «        }n"# t          $ r}t          d¦  «        |‚d}~ww xY w	 |dv rt          j        |d|z  gd| d¬¦  «        }n*t          j        |d|z  gt          | d¬	¦  «        ¬
¦  «        }|j        S # t          $ r# t          d                     | ¦  «        ¦  «        ‚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).
    Údotz0Cannot find the dot binary from Graphviz packageN)rê   Údot_jsonÚjsonrè   z-T%sT)Úcapture_outputÚinputÚtextÚutf8)Úencoding)rî   zACannot create image representation by running dot from string: {})r   ÚLookupErrorÚ	ExceptionÚ
subprocessÚrunÚbytesÚstdoutrJ   )rP   ÚtÚ
dot_binaryr¥   Úprocs        r   rO   rO   <  s	  € ðSõ
 ! Ñ'Ô'ˆ
ˆ
øÝð Sð Sð SÝÐJÑKÔKÐQRÐRøøøøðSøøøð
ØÐ2Ð2Ð2Ý”>Ø˜V a™ZÐ(Ø#Ø Øð	ñ ô ˆDˆDõ ”>Ø˜V a™ZÐ(Ý˜J°Ð8Ñ8Ô8ðñ ô ˆDð Œ{ÐøÝð 
ð 
ð 
ÝðßŠv�jÑ!Ô!ñ
ô 
ð 	
ð
øøøs   ‚ ’
1œ,¬1µAB Â-B5c                   ó   — e Zd ZdZdS )ÚDependencyGraphErrorzDependency graph exception.N)rã   rä   rå   ræ   r   r   r   rü   rü   e  s   € € € € € Ø%Ð%Ð%Ð%r   rü   c                  óv   — t          ¦   «          t          ¦   «          t          ¦   «          t          ¦   «          d S rS   )Ú	malt_demoÚ
conll_demoÚconll_file_demoÚcycle_finding_demor   r   r   Údemor  i  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)Úpylabrh   )Ú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þ   p  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   r[   )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_data2ra   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 )	Nrh   r   r   )r   r   r   r   rÀ   ÚNTOPr�   rŒ   )r
   Útreebank_datar  rÊ   rz   )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.pathsecr   r_   Ú	nltk.treer   r
   rO   ró   rü   r  rþ   rÿ   r   r  r!  r  r  rã   r   r   r   ú<module>r)     sŽ  ððð ð Ð Ð Ð Ø €€€Ø #Ð #Ð #Ð #Ð #Ð #Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð à &Ð &Ð &Ð &Ð &Ð &Ø -Ð -Ð -Ð -Ð -Ð -Ø Ð Ð Ð Ð Ð ðYð Yð Yð Yð Yñ Yô Yð Yðx&
ð &
ð &
ð &
ðR&ð &ð &ð &ð &˜9ñ &ô &ð &ðð ð ð*ð *ð *ð *ðZ	ð 	ð 	ðð ð ð	&ð 	&ð 	&ð€ð(€ð T€ðl ˆzÒÐØ€D�F„F€F€F€Fð Ðr   