Ë
    ]›¾ju$  ã                   ó¦   — 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mZm	Z	m
Z
mZmZmZmZmZmZmZmZmZ ddlmZmZ  G d„ de«      Z G d	„ d
«      Zy)zI
Utility functions for the loading and conversion of CoNLL-format files.
é    N)ÚZipFile)ÚDocument)ÚIDÚTEXTÚLEMMAÚUPOSÚXPOSÚFEATSÚHEADÚDEPRELÚDEPSÚMISCÚNERÚ
START_CHARÚEND_CHAR)ÚFIELD_TO_IDXÚ	FIELD_NUMc                   ó   — e Zd Zy)Ú
CoNLLErrorN)Ú__name__Ú
__module__Ú__qualname__© ó    ú\/home/spazio/kingdom_calls/work5/transenv/lib/python3.12/site-packages/stanza/utils/conll.pyr   r      s   „ Ør   r   c                   ó¦   — e Zd Zedd„«       Zed„ «       Zed„ «       Zed„ «       Zedd„«       Zedd„«       Z	edd„«       Z
ed	„ «       Zedd
„«       Zy)ÚCoNLLc                 óf  — g g }}g g }}t        | «      D ]è  \  }}|j                  «       j                  d«      }t        |«      dk(  r6t        |«      dkD  sŒB|j	                  |«       g }|j	                  |«       g }Œi|j                  d«      r|j	                  |«       ŒŒ|j                  d«      }|rd|d   v rŒ§t        |«      t        k7  r*t        d|dz   › dt        › d	t        |«      › d
|› d|› �
«      ‚||gz  }Œê t        |«      dkD  r"|j	                  |«       |j	                  |«       ||fS )a£   Load the file or string into the CoNLL-U format data.
        Input: file or string reader, where the data is in CoNLL-U format.
        Output: a tuple whose first element is a list of list of list for each token in each sentence in the data,
        where the innermost list represents all fields of a token; and whose second element is a list of lists for each
        comment in each sentence in the data.
        z 
	r   ú#ú	ú.zCannot parse CoNLL line é   z: expecting z	 fields, z found at line z
  )	Ú	enumerateÚlstripÚrstripÚlenÚappendÚ
startswithÚsplitr   r   )	ÚfÚignore_gappingÚdocÚsentÚdoc_commentsÚsent_commentsÚline_idxÚlineÚarrays	            r   Ú
load_conllzCoNLL.load_conll   sb  € ð ˜ˆTˆØ&(¨"�mˆÜ'¨›lò 	 ‰NˆH�dà—;‘;“=×'Ñ'¨	Ó2ˆDÜ�4‹y˜AŠ~Ü�t“9˜q“=Ø—J‘J˜tÔ$Ø�DØ ×'Ñ'¨Ô6Ø$&‘Mà—?‘? 3Ô'Ø!×(Ñ(¨Ô.ØØŸ
™
 4Ó(�Ù! c¨U°1©X¡oØÜ�u“:¤Ò*Ü$Ð'?ÀÈÁ
¸|È<ÔXaÐWbÐbkÔloÐpuÓlvÐkwð  xGð  HPð  GQð  QUð  V[ð  U\ð  &]ó  ^ð  ^Ø˜˜‘‘ð%	 ô& ˆt‹9�qŠ=Ø�J‰J�tÔØ×Ñ Ô.Ø�LÐ Ð r   c                 óN  — g }g }t        | «      D ]ä  \  }}g }g }t        |«      D ]«  \  }}	 t        j                  |«      }	d|	t           v rCt        d„ |	t           j                  dd¬«      D «       «      |	t        <   |j                  |	«       Œjt        d„ |	t           j                  dd¬«      D «       «      |	t        <   |j                  |	«       Œ­ |j                  |«       |j                  |«       Œæ ||fS # t        $ r }
t	        d||t        |
«      fz  «      |
‚d}
~
ww xY w)	ai   Convert the CoNLL-U format input data to a dictionary format output data.
        Input: list of token fields loaded from the CoNLL-U format data, where the outmost list represents a list of sentences, and the inside list represents all fields of a token.
        Output: a list of list of dictionaries for each token in each sentence in the document.
        z*Could not process sentence %d token %d: %sNr!   c              3   ó2   K  — | ]  }t        |«      –— Œ y ­w©N©Úint©Ú.0Úxs     r   ú	<genexpr>z&CoNLL.convert_conll.<locals>.<genexpr>E   ó   è ø€ Ò*a°a¬3¨q¯6Ñ*aùó   ‚r"   ©Úmaxsplitc              3   ó2   K  — | ]  }t        |«      –— Œ y ­wr6   r7   r9   s     r   r<   z&CoNLL.convert_conll.<locals>.<genexpr>H   r=   r>   ú-)
r#   r   Úconvert_conll_tokenÚ
ValueErrorr   Ústrr   Útupler)   r'   )Ú	doc_conllÚdoc_dictÚ	doc_emptyÚsent_idxÚ
sent_conllÚ	sent_dictÚ
sent_emptyÚ	token_idxÚtoken_conllÚ
token_dictÚes              r   Úconvert_conllzCoNLL.convert_conll4   s?  € ð ˆØˆ	Ü$-¨iÓ$8ò 	)Ñ ˆH�jØˆIØˆJÜ*3°JÓ*?ò 
1Ñ&�	˜;ðzÜ!&×!:Ñ!:¸;Ó!G�Jð ˜*¤R™.Ñ(Ü%*Ñ*a¸:Äb¹>×;OÑ;OÐPSÐ^_Ð;OÓ;`Ô*aÓ%a�Jœr‘NØ×%Ñ% jÕ1ä%*Ñ*a¸:Äb¹>×;OÑ;OÐPSÐ^_Ð;OÓ;`Ô*aÓ%a�Jœr‘NØ×$Ñ$ ZÕ0ð
1ð �O‰O˜IÔ&Ø×Ñ˜ZÕ(ð	)ð  ˜Ð"Ð"øô "ò zÜ$Ð%QÐU]Ð_hÔjmÐnoÓjpÐTqÑ%qÓrÐxyÐyûðzús   ¬C;Ã;	D$ÄDÄD$c           
      óð   — t        | «      }dj                  |«      }|j                  d«      }|D ��cg c]0  }|j                  d«      D �cg c]  }|j                  d«      ‘Œ c}‘Œ2 }}}|S c c}w c c}}w )a   Convert the dictionary format input data to the CoNLL-U format output data.

        This is the reverse function of `convert_conll`, but does not include sentence level annotations or comments.

        Can call this on a Document using `CoNLL.convert_dict(doc.to_dict())`

        Input: dictionary format data, which is a list of list of dictionaries for each token in each sentence in the data.
        Output: CoNLL-U format data as a list of list of list for each token in each sentence in the data.
        z{:c}z

ú
r    )r   Úformatr)   )rH   r,   ÚtextÚ	sentencesÚsentencer;   rG   s          r   Úconvert_dictzCoNLL.convert_dictN   sh   € ô �xÓ ˆØ�}‰}˜SÓ!ˆØ—J‘J˜vÓ&ˆ	ØS\×]Àx¨X¯^©^¸DÓ-AÖB¨�a—g‘g˜d•mÔBÐ]ˆ	Ñ]ØÐùò CùÓ]s   ³A2ÁA-Á#A2Á-A2c                 ó   — i }t         D ]r  }| t         |      }|dk7  r|t        k(  rt        |«      ||<   n|||<   | t         t              dk(  sŒE| t         t              |t        <   | t         t              |t        <   Œt |S )zÓ Convert the CoNLL-U format input token to the dictionary format output token.
        Input: a list of all CoNLL-U fields for the token.
        Output: a dictionary that maps from field name to value.
        Ú_)r   r   r8   r   r   )rO   rP   ÚfieldÚvalues       r   rC   zCoNLL.convert_conll_token_   s‘   € ð ˆ
Ü!ò 
	EˆEØ¤¨UÑ 3Ñ4ˆEØ˜Š|ØœD’=Ü(+¨E«
�J˜uÒ%à(-�J˜uÑ%àœ<¬Ñ-Ñ.°#Ó5Ø#.¬|¼DÑ/AÑ#B�
œ4Ñ Ø$/´¼UÑ0CÑ$D�
œ5Ò!ð
	Eð Ðr   Nc                 óZ  — t        | |g«      rt        | |g«      rJ d«       ‚|r	| sJ d«       ‚|r/t        j                  |«      }t        j                  ||«      \  }}nŽ|r]t        |«      5 }|j                  | «      5 }t        j                  t        j                  |d¬«      |«      \  }}ddd«       ddd«       n/t        | d¬«      5 }t        j                  ||«      \  }}ddd«       t        j                  «      \  }	}
|	|
fS # 1 sw Y   Œ^xY w# 1 sw Y   Œ2xY w# 1 sw Y   Œ>xY w)zV Load the CoNLL-U format data from file or string into lists of dictionaries.
        z%either use input file or input stringz*must provide input_file if zip_file is setúutf-8©ÚencodingN)
ÚanyÚallÚioÚStringIOr   r3   r   ÚopenÚTextIOWrapperrR   )Ú
input_fileÚ	input_strr+   Úzip_fileÚinfilerG   r.   ÚzinÚfinrH   rI   s              r   Ú
conll2dictzCoNLL.conll2dicts   s=  € ô �J 	Ð*Ô+´C¸ÀYÐ8OÔ4PÐyÐRyÓyÐPÙ™JÐTÐ(TÓT˜JáÜ—[‘[ Ó+ˆFÜ&+×&6Ñ&6°v¸~Ó&NÑ#ˆI‘|ÙÜ˜Ó"ð x cØ—X‘X˜jÓ)ð x¨SÜ.3×.>Ñ.>¼r×?OÑ?OÐPSÐ^eÔ?fÐhvÓ.wÑ+�I˜|÷x÷xð xô �j¨7Ô3ð P°sÜ*/×*:Ñ*:¸3ÀÓ*OÑ'�	˜<÷Pô $×1Ñ1°)Ó<Ñˆ�)Ø˜ yÐ0Ð0÷xð xú÷xð xú÷Pð Pús0   Á+DÁ=/D	Â,DÃ
D!Ä	D	ÄDÄDÄ!D*c                 óZ   — t         j                  | |||¬«      \  }}}t        |d ||¬«      S )N©rj   ©rV   ÚcommentsÚempty_sentences)r   rn   r   )rh   ri   r+   rj   rH   r.   rI   s          r   Ú	conll2doczCoNLL.conll2docˆ   s6   € ä,1×,<Ñ,<¸ZÈÐTbÐmuÐ,<Ó,vÑ)ˆ�, 	Ü˜ t°lÐT]Ô^Ð^r   c                 óú  — t         j                  | |||¬«      \  }}}g }g }g }	g }
d }t        |||«      D ]è  \  }}}|D ]ª  }|j                  d«      s|j                  d«      sŒ&|j	                  dd¬«      d   }t        |«      dk(  r|}nT||k7  rNt        |d |	|
¬«      }|d k7  r&|j                  D ]  }|j                  «       |_	        Œ |j                  |«       |}nŒ¡|g}|g}	|g}
 Œµ |j                  |«       |	j                  |«       |
j                  |«       Œê t        |«      dkD  rMt        |d |	|
¬«      }|d k7  r&|j                  D ]  }|j                  «       |_	        Œ |j                  |«       }|S )	Nrp   z
# doc_id =z# newdoc id =ú=r"   r?   r   rq   )r   rn   Úzipr(   r)   r&   r   rW   ÚstripÚdoc_idr'   )rh   ri   r+   rj   rH   r.   rI   ÚdocsÚcurrent_docÚcurrent_commentsÚcurrent_emptyÚcurrent_doc_idr,   rr   ÚemptyÚcommentry   Únew_docÚis                      r   Úconll2multi_docszCoNLL.conll2multi_docs�   s¶  € ä,1×,<Ñ,<¸ZÈÐTbÐmuÐ,<Ó,vÑ)ˆ�, 	àˆØˆØÐØˆØˆÜ$'¨°,À	Ó$Jò 	,Ñ ˆC�˜5Ø#ò ,�Ø×%Ñ% lÔ3°w×7IÑ7IÈ/Õ7ZØ$Ÿ]™]¨3¸˜]Ó;¸AÑ>�FÜ˜;Ó'¨1Ò,Ø)/™Ø >Ò1Ü"*¨;¸TÐL\Ðn{Ô"|˜Ø)¨TÒ1Ø%,×%6Ñ%6ò B Ø+9×+?Ñ+?Ó+A ¥ðBàŸ™ GÔ,Ø)/™à Ø#& %�KØ(0 zÐ$Ø%* G�MÙð#,ð& ×"Ñ" 3Ô'Ø ×'Ñ'¨Ô1Ø×$Ñ$ UÕ+ð-	,ô. ˆ{Ó˜aÒÜ˜{°Ð@PÐboÔpˆGØ Ò%Ø ×*Ñ*ò 6�AØ-×3Ñ3Ó5�A•Hð6à�K‰K˜Ô Ø#ˆNàˆr   c                 óF   — t        | «      }t        j                  ||«       y)zq
        Convert the dictionary format input data to the CoNLL-U format output data and write to a file.
        N)r   r   Úwrite_doc2conll)rH   Úfilenamer,   s      r   Ú
dict2conllzCoNLL.dict2conll·   s   € ô
 �xÓ ˆÜ×Ñ˜c 8Õ,r   c                 óä   — t        |d«      r!|j                  dj                  | «      «       yt        |||¬«      5 }|j                  dj                  | «      «       ddd«       y# 1 sw Y   yxY w)a   
        Writes the doc as a conll file to the given file.

        If passed a string, that filename will be opened.  Otherwise, filename.write() will be called.

        Note that the output needs an extra 

 at the end to be a legal output file
        Úwritez{:C}

r`   N)Úhasattrr‰   rU   rf   )r,   r†   Úmodera   Úoutfiles        r   r…   zCoNLL.write_doc2conllÀ   s_   € ô �8˜WÔ%Ø�N‰N˜:×,Ñ,¨SÓ1Õ2ä�h ¨xÔ8ð 6¸GØ—‘˜j×/Ñ/°Ó4Ô5÷6÷ 6ñ 6ús   ¼!A&Á&A/)T)NNTN)Úwr_   )r   r   r   Ústaticmethodr3   rR   rY   rC   rn   rt   rƒ   r‡   r…   r   r   r   r   r      s¸   „ àò !ó ð !ðD ñ#ó ð#ð2 ñó ðð  ñó ðð& ò1ó ð1ð( ò_ó ð_ð ò'ó ð'ðR ñ-ó ð-ð ò6ó ñ6r   r   )Ú__doc__Úosrd   Úzipfiler   Ústanza.models.common.docr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   rD   r   r   r   r   r   ú<module>r“      s?   ðñó 
Û 	Ý å -ß |× |× |Õ |ß <ô	�ô 	÷~6ò ~6r   