Ë
    ›¾jH(  ã                   ó¼   — d dl Z d dlZ e j                  d«      Z e j                  d«      Z e j                  d«      Zd„ Z G d„ d«      Zdd„Z	d	„ Z
 G d
„ d«      Zy)é    Nz\n\s*\nz^[\d]+([,\.]+[\d]+)*[,\.]*$z\sc                 óŽ   — g }t        | «      D ]4  \  }\  }}|dkD  r|dk(  r| |dz
     d   dk(  rŒ"|j                  ||f«       Œ6 |S )Nr   ú é   )Ú	enumerateÚappend)ÚparaÚfilteredÚiÚcharÚlabels        ú^/home/spazio/kingdom_calls/work5/transenv/lib/python3.12/site-packages/minisbd/modules/data.pyÚfilter_consecutive_whitespacesr      s^   € Ø€HÜ% d›Oò 'Ñˆ‰=ˆD�%ØˆqŠ5Ø�sŠ{˜t A a¡C™y¨™|¨sÒ2Øà�‰˜˜u˜Õ&ð'ð €Oó    c                   ó8   ‡ — e Zd Zdˆ fd„	Zd„ Zd„ Zd„ Zd„ Zˆ xZS )ÚTokenizationDatasetc                 ó°  •— t        ‰| �  |i |¤Ž || _        || _        || _        t
        j                  |«      }|D �cg c]  }|j                  «       ‘Œ }}|D �cg c]  }|sŒ|‘Œ	 }}|D ��	cg c]  }|D �	cg c]  }	d‘Œ c}	‘Œ }
}}	| j                  j                  dd«      }t        ||
«      D ����cg c]A  \  }}t        ||«      D ��cg c]$  \  }}|r|dk(  st        j                  d|«      |f‘Œ& c}}‘ŒC c}}}}| _        | j                  D �cg c]  }t        |«      ‘Œ c}| _        y c c}w c c}w c c}	w c c}	}w c c}}w c c}}}}w c c}w )Nr   Úskip_newlineFú
r   )ÚsuperÚ__init__ÚargsÚ
dictionaryÚvocabÚNEWLINE_WHITESPACE_REÚsplitÚrstripÚgetÚzipÚWHITESPACE_REÚsubÚdatar   )ÚselfÚconfigÚtextr   r   r   ÚkwargsÚtext_chunksÚptÚ_Úlabelsr   Úpcr   r   ÚxÚ	__class__s                   €r   r   zTokenizationDataset.__init__   s=  ø€ Ü‰Ñ˜$Ð) &Ò)ØˆŒ	Ø$ˆŒØˆŒ
ä+×1Ñ1°$Ó7ˆØ-8Ö9 r�r—y‘y•{Ð9ˆÐ9Ø$/Ö6˜b²2’rÐ6ˆÐ6Ø,7×8 b˜b–/˜’1”/Ð8ˆÑ8à—y‘y—}‘} ^°UÓ;ˆô $' {°FÓ#;÷=ñ =á˜˜Rô *-¨R°«÷]Ù%˜$ ¹\ÈdÐVZÊlô %×(Ñ(¨¨dÓ3°UÒ;õ ]õ =ˆŒ	ð
 AEÇ	Á	ÖJ¸1Ô3°AÕ6ÒJˆ�	ùò :ùÚ6ùÚ!ùÓ8ùó]ùõ =ùò
 KsG   ¿D0ÁD5Á$D5Á/	D?Á8	D:ÂD?Â6E
Ã)EÃ6E
ÄEÄ:D?ÅE
c           	      ó†   — | j                   D �cg c]'  }t        j                  t        d„ |D «       «      «      ‘Œ) c}S c c}w )zœ
        Returns a list of the labels for all of the sentences in this DataLoader

        Used at eval time to compare to the results, for example
        c              3   ó&   K  — | ]	  }|d    –— Œ y­w©r   N© ©Ú.0r+   s     r   ú	<genexpr>z-TokenizationDataset.labels.<locals>.<genexpr>1   s   è ø€ Ò1 q˜a �dÑ1ùs   ‚)r!   ÚnpÚarrayÚlist)r"   Úsents     r   r)   zTokenizationDataset.labels+   s2   € ð @D¿y¹yÖI°t”—‘œÑ1¨DÔ1Ó1Õ2ÒIÐIùÒIs   �,>c                 ó¨  — t        |«      }t        | j                  d   «      D �cg c]  }d‘Œ }}t        | j                  d   «      D �cg c]  }d‘Œ }}||   d   }||   d   }d}	d}
t        d| j                  d   dz   «      D ]¼  }||z   |dz
  k  rN|	rL||||z      d   j                  «       z  }|| j                  d   v rdnd}|||dz
  <   || j                  d   vrd}	||z
  dk\  rN|
rL|||z
     d   j                  «       |z   }|| j                  d   v rdnd}|||dz
  <   || j                  d   vrd}
|	rŒµ|
rŒ¸ ||z   S  ||z   S c c}w c c}w )	zT
        This function is to extract dictionary features for each character
        Únum_dict_featr   Tr   ÚwordsÚprefixesFÚsuffixes)ÚlenÚranger   Úlowerr   )r"   r   ÚidxÚlengthr
   Údict_forward_featsÚdict_backward_featsÚforward_wordÚbackward_wordÚprefixÚsuffixÚwindowÚfeats                r   Úextract_dict_featz%TokenizationDataset.extract_dict_feat3   s   € ô �T“ˆä).¨t¯y©y¸Ñ/IÓ)JÖK AšaÐKÐÐKÜ*/°·	±	¸/Ñ0JÓ*KÖL QšqÐLÐÐLØ˜C‘y ‘|ˆØ˜S™	 !™ˆØˆØˆÜ˜A˜dŸi™i¨Ñ8¸Ñ:Ó;ò 	ˆFð �f‘ ¨¡Ò)©fØ  S¨¡ZÑ 0°Ñ 3× 9Ñ 9Ó ;Ñ;�à(¨D¯O©O¸GÑ,DÑD‘qÈ!�à/3Ð" 6¨!¡8Ñ,à t§¡°zÑ'BÑBØ"�Fà�f‘ Ò"¡vØ $ S¨¡ZÑ 0°Ñ 3× 9Ñ 9Ó ;¸mÑ K�Ø)¨T¯_©_¸WÑ-EÑE‘qÈ1�Ø04Ð# F¨1¡HÑ-Ø ¨¯©¸
Ñ(CÑCØ"�Fâ¢&Øà!Ð$7Ñ7Ð7ð/	ð. "Ð$7Ñ7Ð7ùò; LùÚLs   ¦	E
Á	Ec                 ó  ‡ ‡— g }g Š‰ j                   d   D ]S  }|dk(  s|dk(  rŒ|dk(  rd„ }n,|dk(  rd„ }n#|dk(  rd	„ }nt        d
j                  |«      «      ‚‰j                  |«       ŒU ˆfd„}ˆ fd„}d‰ j                   d   v }d‰ j                   d   v }‰ j                   d   }	g }
g }g }t	        |«      D ]   \  }\  }} ||«      }|r&|t        |«      dz
  k(  rdnd}|j                  |«       |r|dk(  rdnd}|j                  |«       |	r‰ j                  ||«      }||z   }|
j                  |«       |j                  |«       |j                  |«       Œ¢ t        |
«      dkD  r|j                   ||
||«      «       |S )z7 Convert a paragraph to a list of processed sentences. Ú
feat_funcsÚend_of_paraÚstart_of_paraÚspace_beforec                 ó,   — | j                  d«      rdS dS )Nr   r   r   )Ú
startswith©r+   s    r   ú<lambda>z7TokenizationDataset.para_to_sentences.<locals>.<lambda>a   s   €  a§l¡l°3Ô&7 € ¸Q€ r   Úcapitalizedc                 ó0   — | d   j                  «       rdS dS )Nr   r   )ÚisupperrR   s    r   rS   z7TokenizationDataset.para_to_sentences.<locals>.<lambda>c   s   €  a¨¡d§l¡l¤n € ¸!€ r   Únumericc                 ó4   — t         j                  | «      �dS dS )Nr   r   )Ú
NUMERIC_REÚmatchrR   s    r   rS   z7TokenizationDataset.para_to_sentences.<locals>.<lambda>e   s   € ¤z×'7Ñ'7¸Ó':Ð'F € ÈQ€ r   z#Feature function "{}" is undefined.c                 ó:   •— ‰D �cg c]
  } || «      ‘Œ c}S c c}w ©Nr0   )r+   ÚfÚfuncss     €r   rS   z7TokenizationDataset.para_to_sentences.<locals>.<lambda>l   s   ø€ °%Ö#8¨Q¡A a¥DÒ#8€ ùÒ#8s   †c                 óì   •— t        j                  | D �cg c]  }‰j                  j                  |«      ‘Œ c}«      t        j                  |«      t        j                  |«      t	        | «      fS c c}w r\   )r4   r5   r   Úunit2idr6   )Ú
sent_unitsÚsent_labelsÚ
sent_featsÚyr"   s       €r   Úprocess_sentencez?TokenizationDataset.para_to_sentences.<locals>.process_sentencen   sV   ø€ Ü—H‘H¸ZÖH¸˜dŸj™j×0Ñ0°Õ3ÒHÓIÜ—H‘H˜[Ó)Ü—H‘H˜ZÓ(Ü˜Ó$ð&ð &ùÒHs   •"A1Úuse_dictionaryr   r   )r   Ú
ValueErrorÚformatr   r   r=   rJ   )r"   r   ÚresÚ	feat_funcÚfuncÚcomposite_funcre   Úuse_end_of_paraÚuse_start_of_pararf   Úcurrent_unitsÚcurrent_labelsÚcurrent_featsr
   Úunitr   Úfeatsr]   Ú
dict_featsr^   s   `                  @r   Úpara_to_sentencesz%TokenizationDataset.para_to_sentencesX   s®  ù€ àˆØˆØŸ™ <Ñ0ò 	ˆIØ˜MÒ)¨Y¸/Ò-IàØ˜NÒ*Ù>‘Ø˜mÒ+Ù;‘Ø˜iÒ'ÙN‘ä Ð!F×!MÑ!MÈiÓ!XÓYÐYà�L‰L˜Õð	ó  9ˆô	&ð (¨4¯9©9°\Ñ+BÐBˆØ+¨t¯y©y¸Ñ/FÐFÐØŸ™Ð#3Ñ4ˆØˆØˆØˆÜ )¨$£ò 	(ÑˆA‰}��eÙ" 4Ó(ˆEáØœc $›i¨™kÒ)‘A¨q�Ø—‘˜Q”Ù Ø˜aš‘A Q�Ø—‘˜Q”ñ Ø!×3Ñ3°D¸!Ó<�
Ø 
Ñ*�à× Ñ  Ô&Ø×!Ñ! %Ô(Ø× Ñ  Õ'ð#	(ô& ˆ}Ó Ò!Ø�J‰JÑ'¨°~À}ÓUÔVàˆ
r   c                 óÂ  — | j                   j                  d«      }| j                   j                  d«      }|\  }}}}|j                  d   }	||k7  j                  d«      j	                  «       }
t        d„ t        ||
«      D «       «      }t        j                  t        |«      |f|t        j                  ¬«      }t        j                  t        |«      |fdt        j                  ¬«      }t        j                  t        |«      ||	ft        j                  ¬«      }g }t        t        |«      «      D ]¤  }t        ||   |
|   «      ||<   ||||   |
|   …f   ||d|
|   ||   z
  …f<   ||||   |
|   …f   ||d|
|   ||   z
  …f<   ||||   |
|   …f   ||d|
|   ||   z
  …f<   |j!                  ||   ||   |
|    dg||
|   z
  ||   z   z  z   «       Œ¦ ||||fS )a  
        Advance to a new position in a batch where we have partially processed the batch

        If we have previously built a batch of data and made predictions on them, then when we are trying to make
        prediction on later characters in those paragraphs, we can avoid rebuilding the converted data from scratch
        and just (essentially) advance the indices/offsets from where we read converted data in this old batch.
        In this case, eval_offsets index within the old_batch to advance the strings to process.
        z<UNK>ú<PAD>éÿÿÿÿr   c              3   ó,   K  — | ]  \  }}||z
  –— Œ y ­wr\   r0   )r2   r
   Úls      r   r3   z8TokenizationDataset.advance_old_batch.<locals>.<genexpr>¡   s   è ø€ Ò>™d˜a �a˜•cÑ>ùs   ‚©ÚdtypeN)r   r`   ÚshapeÚsumÚtolistÚmaxr   r4   Úfullr=   Úint64Úint32ÚzerosÚfloat32r>   Úminr   )r"   Úeval_offsetsÚ	old_batchÚunkidÚpadidÚounitsÚolabelsÚ	ofeaturesÚorawÚ	feat_sizeÚlensÚpad_lenÚunitsr)   ÚfeaturesÚ	raw_unitsr
   s                    r   Úadvance_old_batchz%TokenizationDataset.advance_old_batch’   s  € ð —
‘
×"Ñ" 7Ó+ˆØ—
‘
×"Ñ" 7Ó+ˆà+4Ñ(ˆ�˜ DØ—O‘O BÑ'ˆ	Ø˜%‘×$Ñ$ QÓ'×.Ñ.Ó0ˆÜÑ>¤c¨,¸Ó&=Ô>Ó>ˆä—‘œ˜V› gÐ.°¼R¿X¹XÔFˆÜ—‘œ#˜f›+ wÐ/°¼2¿8¹8ÔDˆÜ—8‘8œS ›[¨'°9Ð=ÄRÇZÁZÔPˆØˆ	ä”s˜6“{Ó#ò 	sˆAÜ! ,¨q¡/°4¸±7Ó;ˆL˜‰OØ5;¸A¸|ÈA¹ÈtÐTUÉwÐ?VÐ<VÑ5WˆE�!Ð1�t˜A‘w ¨a¡Ñ0Ð1Ð1Ñ2Ø6=¸aÀÈaÁÐQUÐVWÑQXÐAXÐ>XÑ6YˆF�1Ð2˜˜Q™ ,¨q¡/Ñ1Ð2Ð2Ñ3Ø8AÀ!À\ÐRSÁ_ÐUYÐZ[ÑU\ÐE\ÐB\Ñ8]ˆH�QÐ4˜$˜q™' L°¡OÑ3Ð4Ð4Ñ5Ø×Ñ˜T !™W \°!¡_°T¸!±WÐ=ÀÀ	ÈWÐW[Ð\]ÑW^ÑM^ÐamÐnoÑapÑMpÑ@qÑqÕrð	sð �f˜h¨	Ð1Ð1r   )NNN)	Ú__name__Ú
__module__Ú__qualname__r   r)   rJ   ru   r•   Ú__classcell__)r,   s   @r   r   r      s    ø„ õKò&Jò#8òJ8öt2r   r   c                 ó°   ‡— | sg g fS ‰rt        t        | «      ˆfd„|¬«      }nt        t        | «      d„ |¬«      }t        t        |Ž «      }|d   |d   fS )zå
    Sort data and return both the data and the original indices.

    One useful application is to sort by length, which can be done with key=len
    Returns the data as a sorted list, then the indices of the original list.
    c                 ó   •—  ‰| d   «      S ©Nr   r0   )r+   Úkeys    €r   rS   z#sort_with_indices.<locals>.<lambda>½   s   ø€ ¹¸A¸a¹D»	€ r   )r�   Úreversec                 ó   — | d   S rœ   r0   rR   s    r   rS   z#sort_with_indices.<locals>.<lambda>¿   s
   € ¸¸!¹€ r   r   r   )Úsortedr   Útupler   )r!   r�   rž   ÚorderedÚresults    `   r   Úsort_with_indicesr¤   ³   sZ   ø€ ñ Ø�2ˆvˆÙ
Üœ 4›Ó.AÈ7ÔS‰äœ 4›©nÀgÔNˆä”3˜�=Ó!€FØ�!‰9�f˜Q‘iÐÐr   c                 óÖ   — t        | «      t        |«      k(  sJ d«       ‚t        | «      dk(  rg S t        t        t        || «      «      Ž D �cg c]  }t        |«      ‘Œ c}\  }}|S c c}w )z:
    Unsort a sorted list, based on the original idx.
    z9Number of list elements must match with original indices.r   )r=   r   r    r6   )Úsorted_listÚoidxÚtr(   Úunsorteds        r   Úunsortrª   Ä   sg   € ô ˆ{Óœs 4›yÒ(ÐeÐ*eÓeÐ(Ü
ˆ;Ó˜1ÒØˆ	Ü$'¬´°D¸+Ó0FÓ)GÐ$HÖI˜q”4˜•7ÒI�K€A€xØ€Oùò Js   ÁA&c                   ó.   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zd„ Zy)ÚSortedDatasetal  
    Holds a TokenizationDataset for use in a torch DataLoader

    The torch DataLoader is different from the DataLoader defined here
    and allows for cpu & gpu parallelism.  Updating output_predictions
    to use this class as a wrapper to a TokenizationDataset means the
    calculation of features can happen in parallel, saving quite a
    bit of time.
    c                 óv   — || _         t        | j                   j                  t        ¬«      \  | _        | _        y )N)r�   )Údatasetr¤   r!   r=   Úindices)r"   r®   s     r   r   zSortedDataset.__init__Ù   s(   € ØˆŒÜ"3°D·L±L×4EÑ4EÌ3Ô"OÑˆŒ	�4•<r   c                 ó,   — t        | j                  «      S r\   )r=   r!   )r"   s    r   Ú__len__zSortedDataset.__len__Ý   s   € Ü�4—9‘9‹~Ðr   c                 óR   — | j                   j                  | j                  |   «      S r\   )r®   ru   r!   )r"   Úindexs     r   Ú__getitem__zSortedDataset.__getitem__à   s    € Ø�|‰|×-Ñ-¨d¯i©i¸Ñ.>Ó?Ð?r   c                 ó.   — t        || j                  «      S r\   )rª   r¯   )r"   Úarrs     r   rª   zSortedDataset.unsortã   s   € Ü�c˜4Ÿ<™<Ó(Ð(r   c           	      ó
  — t        d„ |D «       «      rt        d«      ‚|d   d   d   j                  d   }| j                  j                  j                  d«      }t        d„ |D «       «      dz   }t        j                  t        |«      |f|t        j                  ¬	«      }t        j                  t        |«      |fdt        j                  ¬	«      }t        j                  t        |«      ||ft        j                  ¬	«      }g }t        |«      D ]l  \  }	}
|
d   \  }}}}|||	d t        |«      …f<   |||	d t        |«      …f<   |||	d t        |«      …d d …f<   |j                  |dg|t        |«      z
  z  z   «       Œn ||||fS )
Nc              3   ó8   K  — | ]  }t        |«      d kD  –— Œ y­wr/   ©r=   r1   s     r   r3   z(SortedDataset.collate.<locals>.<genexpr>ç   s   è ø€ Ò+˜aŒs�1‹v˜�zÑ+ùs   ‚z:Expected all paragraphs to have no preset sentence splits!r   é   rx   rw   c              3   ó>   K  — | ]  }t        |d    d   «      –— Œ y­w)r   é   Nr¹   r1   s     r   r3   z(SortedDataset.collate.<locals>.<genexpr>í   s   è ø€ Ò4 q”c˜!˜A™$˜q™'—lÑ4ùs   ‚r   r{   )Úanyrg   r}   r®   r   r`   r€   r4   r�   r=   r‚   rƒ   r„   r…   r   r   )r"   Úsamplesr�   rŠ   r‘   r’   r)   r“   r”   r
   ÚsampleÚu_Úl_Úf_Úr_s                  r   ÚcollatezSortedDataset.collateæ   so  € ÜÑ+ 7Ô+Ô+ÜÐYÓZÐZØ˜A‘J˜q‘M !Ñ$×*Ñ*¨2Ñ.ˆ	Ø—‘×"Ñ"×*Ñ*¨7Ó3ˆô Ñ4¨GÔ4Ó4°qÑ8ˆä—‘œ˜W› wÐ/°¼b¿h¹hÔGˆÜ—‘œ#˜g›,¨Ð0°"¼B¿H¹HÔEˆÜ—8‘8œS ›\¨7°IÐ>ÄbÇjÁjÔQˆØˆ	Ü" 7Ó+ò 	C‰IˆAˆvØ# A™Y‰NˆB��B˜Ø!#ˆE�!�X”c˜"“g�X�+ÑØ"$ˆF�1�h”s˜2“w�h�;ÑØ')ˆH�Q˜œ˜R›˜¢!�^Ñ$Ø×Ñ˜R 7 )¨w¼¸R»Ñ/@Ñ"AÑAÕBð	Cð �f˜h¨	Ð1Ð1r   N)	r–   r—   r˜   Ú__doc__r   r±   r´   rª   rÄ   r0   r   r   r¬   r¬   Ï   s"   „ ñòPòò@ò)ó2r   r¬   )NF)ÚreÚnumpyr4   Úcompiler   rY   r   r   r   r¤   rª   r¬   r0   r   r   ú<module>rÉ      sg   ðÛ 	Û ð #˜Ÿ
™
 :Ó.Ð ð ˆR�Z‰ZÐ6Ó7€
Ø�—
‘
˜5Ó!€ò	÷X2ñ X2óx ò"÷+2ò +2r   