
    Ngj                    v    d dl mZ d dlZd dlZd dlmZ d dlmZ  ej	        e
          Z G d de          ZdS )    )annotationsN)IterableDataset)InputExamplec                  2     e Zd ZdZdd fd
Zd Zd Z xZS )SentenceLabelDatasetaU  
    This dataset can be used for some specific Triplet Losses like BATCH_HARD_TRIPLET_LOSS which requires
    multiple examples with the same label in a batch.

    It draws n consecutive, random and unique samples from one label at a time. This is repeated for each label.

    Labels with fewer than n unique samples are ignored.
    This also applied to drawing without replacement, once less than n samples remain for a label, it is skipped.

    This *DOES NOT* check if there are more labels than the batch is large or if the batch size is divisible
    by the samples drawn per label.
       Fexampleslist[InputExample]samples_per_labelintwith_replacementboolc                   t                                                       || _        i }|D ]5}|j        |vr
g ||j        <   ||j                                     |           6g | _        g | _        d}|                                D ]h\  }}t          |          | j        k    rK| j        	                    |           | j                            t          | j                             |dz  }it          j        |          | _        || _        t          j                            | j                   t                               dt          |           dt          | j                   d| j         d| d	           dS )	a  
        Creates a LabelSampler for a SentenceLabelDataset.

        Args:
            examples (List[InputExample]): A list of InputExamples.
            samples_per_label (int, optional): The number of consecutive, random, and unique samples drawn per label.
                The batch size should be a multiple of samples_per_label. Defaults to 2.
            with_replacement (bool, optional): If True, each sample is drawn at most once (depending on the total number
                of samples per label). If False, one sample can be drawn in multiple draws, but not multiple times in
                the same drawing. Defaults to False.
        r      zSentenceLabelDataset: z examples, from which z8 examples could be used (those labels appeared at least z	 times). z different labels found.N)super__init__r   labelappendgrouped_inputsgroups_right_borderitemslenextendnparangelabel_ranger   randomshuffleloggerinfo)
selfr	   r   r   label2exexample
num_labelsr   label_examples	__class__s
            o/var/www/html/ai-engine/env/lib/python3.11/site-packages/sentence_transformers/datasets/SentenceLabelDataset.pyr   zSentenceLabelDataset.__init__   s    	!2  	4 	4G}H,,*,'W]#**73333 !#% 
%-^^%5%5 	  	 !E>>""d&<<<#**>:::(//+,,   a
9Z00 0
	$*+++ bS]]  b  b#dNaJbJb  b  b  ]a  ]s  b  b  ~H  b  b  b	
 	
 	
 	
 	
    c              #    K   d}d}i |t          | j                  k     rW| j        |         vrt                      <   dk    rdn| j        dz
           }| j                 }| j        rt          j        ||          }n"fdt          j        ||          D             }t          |          | j        k    rYt          j	        
                    || j        d          D ]1}|dz  }                             |           | j        |         V  2|dz  }|t          | j                  k    r(d}i t          j	                            | j                   |t          | j                  k     Ud S d S )Nr   r   c                (    g | ]}|         v|S  r+   ).0ialready_seenr   s     r'   
<listcomp>z1SentenceLabelDataset.__iter__.<locals>.<listcomp>V   s+    mmm1PQYefkYlPlPlQPlPlPlr(   F)replace)r   r   r   setr   r   r   r   r   r   choiceaddr   )	r!   	label_idxcountleft_borderright_border	selectionelement_idxr.   r   s	          @@r'   __iter__zSentenceLabelDataset.__iter__G   s     	c$-....$Y/EL((&)eeU#$zz!!t/GPQ	/RK3E:L$ nIk<@@		mmmmm	+|(L(Lmmm	9~~!777#%9#3#3It?U_d#3#e#e ; ;KQJE '++K888-k:::::NIC 01111	!	!!$"2333/ c$-........r(   c                *    t          | j                  S )N)r   r   )r!   s    r'   __len__zSentenceLabelDataset.__len__d   s    4&'''r(   )r   F)r	   r
   r   r   r   r   )__name__
__module____qualname____doc__r   r:   r<   __classcell__)r&   s   @r'   r   r      sk         *
 *
 *
 *
 *
 *
 *
X4 4 4:( ( ( ( ( ( (r(   r   )
__future__r   loggingnumpyr   torch.utils.datar   sentence_transformers.readersr   	getLoggerr=   r   r   r+   r(   r'   <module>rH      s    " " " " " "      , , , , , , 6 6 6 6 6 6		8	$	$X( X( X( X( X(? X( X( X( X( X(r(   