o
    }oi                     @   sL   d dl Z d dlmZ d dlmZ ddedefdd	Zddedefd
dZdS )    N)AbstractCTCDecoding)
Hypothesis   {Gz?subsampling_factorwindow_stridec                    s   ddt dtfdd dd dt dtf fd	d
| du r | S t| tr,| gS t| trCt| d trCfdd| D S t| trct| d trct| d d trcfdd| D S tdt|  )aA  
    Processes AED timestamp outputs and extracts word-level timestamps.
    Args:
        outputs (Hypothesis, list of Hypotesis or list of list of Hypotesis): The hypothesis outputs to process. Can be a single Hypothesis object or a list of Hypothesis objects.
        subsampling_factor (int, optional): The subsampling factor used in the model. Default is 1.
        window_stride (float, optional): The window stride used in the model. Default is 0.01.
    Returns:
        list of list of Hypotesis: The processed hypothesis outputs with word-level timestamps added.
    r   r   r   r   c                 S   s   |   } td| sd | fS d}g }g }t|| D ];}t|d}|d}t|d}	|| | }
|	| | }|  rU|  ||	|
|d}|| ||   qd|}||fS )Nz<\|\d+\|>.*?<\|\d+\|>z<\|(\d+)\|>(.*?)<\|(\d+)\|>r         )wordstart_offset
end_offsetstartend )stripresearchfinditerintgroupappendjoin)textr   r   patternmatchestext_without_timestampsmatchr   contentr   
start_timeend_timesample r!   d/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/asr/parts/utils/timestamp_utils.pyextract_words_with_timestamps    s0   


zDprocess_aed_timestamp_outputs.<locals>.extract_words_with_timestampsc                 S   s6   | D ]}|d | | |d< |d | | |d< q| S )Nr   r   r   r   r!   )segmentsr   r   segmentr!   r!   r"   segments_offset_to_timeA   s   z>process_aed_timestamp_outputs.<locals>.segments_offset_to_timec                    s    | j ||\}}|| _ |dur8t| jdkri | _| j|g g d tj|g dd}|||| jd< | S g g g d| _| S )zM
        Processes a single Hypothesis object to extract timestamps.
        Nr   )r
   r%   char).?!)segment_delimiter_tokensr%   )r   len	timestampupdater   _get_segment_offsets)hypr   r   r-   r   r$   )r#   r&   r!   r"   process_hypothesisG   s&   z9process_aed_timestamp_outputs.<locals>.process_hypothesisNr   c                       g | ]} |qS r!   r!   .0r0   r1   r   r   r!   r"   
<listcomp>k       z1process_aed_timestamp_outputs.<locals>.<listcomp>c                    s"   g | ]} fd d|D qS )c                    r2   r!   r!   r3   r5   r!   r"   r6   o   r7   z<process_aed_timestamp_outputs.<locals>.<listcomp>.<listcomp>r!   )r4   	hyps_listr5   r!   r"   r6   n   s    zRExpected Hypothesis, list of Hypothesis or list of list of Hypothesis object, got r   r   )r   float
isinstancer   list
ValueErrortype)outputsr   r   r!   )r#   r1   r&   r   r   r"   process_aed_timestamp_outputs   s    !
*r@   c                 C   s   | du r| S t | tr| g} t | d ts tdt| d  dd }t| D ]F\}}t|ds8td| |j}d|v rL||d ||| | jd< d	|v r]||d	 ||| | jd	< d
|v rn||d
 ||| | jd
< q(| S )a  
    Process the timestamps from list of hypothesis to user friendly format.
    Converts the start and end duration from frames to seconds.
    Args:
        outputs: List of Hypothesis objects.
        subsampling_factor: int, Subsampling factor used in the model.
        window_stride: float, Window stride used in the model. (sometimes referred to as hop length/shift)
    Returns:
        List of Hypothesis objects with processed timestamps
    Nr   z Expected Hypothesis object, got c           	      S   sN   t | D ] \}}|d }|d }|| | }|| | }||d< ||d< q| S )zv
        Process the timestamp for a single hypothesis.
        return the start and end duration in seconds.
        r   r   r   r   )	enumerate)	r-   r   r   idxvalr   r   r   r   r!   r!   r"   process_timestamp   s   
z4process_timestamp_outputs.<locals>.process_timestampr-   zyExpected Hypothesis object to have 'timestamp' attribute, when compute_timestamps is                     enabled but got r
   r'   r%   )r;   r   r=   r>   rA   hasattrr-   )r?   r   r   rD   rB   r0   r-   r!   r!   r"   process_timestamp_outputsw   s2   


rF   r9   )	r   2nemo.collections.asr.parts.submodules.ctc_decodingr   +nemo.collections.asr.parts.utils.rnnt_utilsr   r   r:   r@   rF   r!   r!   r!   r"   <module>   s
   b