o
    –hÝ  ã                   @   sd   d dl mZ d dlZd dlmZmZmZ eddƒZdd„ Z	dd	„ Z
d
d„ Zdd„ ZG dd„ dƒZdS )é    )Ú
namedtupleN)ÚShiftÚOpenConstituentÚCloseConstituentÚ
RepairEnumzname value is_correctc                 C   sº   g }|D ]E}|g}|d|… D ]}|D ]	}|   ||g¡}qqd}	|| D ]}|  |¡}
| j| }|	|
d|f  ¡  ¡ 7 }	|   ||g¡}q$| |	¡ qt |¡}dd„ || D ƒ}|||fS )zÞ
    score candidate fixed sequences by summing up the transition scores of the most important block

    the candidate with the best summed score is chosen, and the candidate sequence is reconstructed from the blocks
    é   g        r   c                 S   s   g | ]	}|D ]}|‘qqS © r   )Ú.0ÚblockÚxr   r   úd/var/www/html/env_mimamsha/lib/python3.10/site-packages/stanza/models/constituency/dynamic_oracle.pyÚ
<listcomp>   s    z$score_candidates.<locals>.<listcomp>)Ú
bulk_applyÚforwardÚtransition_mapÚcpuÚitemÚappendÚnpÚargmax)ÚmodelÚstateÚ
candidatesÚcandidate_idxÚscoresÚ	candidateÚcurrent_stater
   Ú
transitionÚscoreÚpredictionsÚt_idxÚbest_idxÚbest_candidater   r   r   Úscore_candidates	   s"   ÿ



r#   c                 C   sb   d}|t | ƒk r/t| | tƒr|d }nt| | tƒr%|d }|dkr%|S |d }|t | ƒk sdS )z°
    Advance cur_index through gold_sequence until we have seen 1 more Close than Open

    The index returned is the index of the Close which occurred after all the stuff
    r   r   éÿÿÿÿN)ÚlenÚ
isinstancer   r   ©Úgold_sequenceÚ	cur_indexÚcountr   r   r   Úadvance_past_constituents!   s   
úr+   c                 C   sb   d}|d }|dkr/t | | tƒr|d }|dkr|S nt | | tƒr'|d }|d }|dks
dS )z†
    Go backwards from cur_index to find the open which opens the previous block of stuff.

    Return None if it can't be found.
    r   r   N)r&   r   r   r'   r   r   r   Úfind_previous_open1   s   ÿùr,   c                 C   sŠ   d}d}|t | ƒk rCt| | tƒr|d }n#t| | tƒr(|d }|dkr'|S nt| | tƒr9|r7|dkr7|S d}|d }|t | ƒk s
dS )a  
    Advance cur_index through gold_sequence until the next block has ended

    This is different from advance_past_constituents in that it will
    also return when there is a Shift when count == 0.  That way, we
    return the first block of things we know attach to the left
    r   Fr   r$   TN)r%   r&   r   r   r   )r(   r)   r*   Ú	saw_shiftr   r   r   Úfind_in_order_constituent_endC   s   
õr.   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚDynamicOraclec                    s~   || _ |d ur	|nˆ jj| _ˆ | _tƒ | _|r&t‡ fdd„| d¡D ƒƒ| _tƒ | _|r=t‡ fdd„| d¡D ƒƒ| _d S d S )Nc                    ó   g | ]}ˆ |  ¡  ‘qS r   ©Úupper©r	   r   ©Úrepair_typesr   r   r   d   ó    z*DynamicOracle.__init__.<locals>.<listcomp>ú,c                    r0   r   r1   r3   r4   r   r   r   g   r6   )	Úroot_labelsÚUNKNOWNÚvalueÚoracle_levelr5   ÚsetÚadditional_levelsÚsplitÚdeactivated_levels)Úselfr8   r;   r5   r=   r?   r   r4   r   Ú__init__\   s   "ÿzDynamicOracle.__init__c              
   C   sÐ   |j |j }||kr| jjdfS | jD ]N}|jdu rq| jdur/|j| jkr/|| jvr/|js/q|| j	v r5q| |||j |j| j
||¡}|du rHqt|tƒrWt|ƒdkrW|  S |dura||f  S q| jjdfS )a  
        Return which error has been made, if any, along with an updated transition list

        We assume the transition sequence builds a correct tree, meaning
        that there will always be a CloseConstituent sometime after an
        OpenConstituent, for example
        Né   )r(   Únum_transitionsr5   ÚCORRECTÚfnr;   r:   r=   Údebugr?   r8   r&   Útupler%   r9   )r@   Úpred_transitionr   r   Úgold_transitionÚrepair_typeÚrepairr   r   r   Ú	fix_errori   s&   

&
ÿzDynamicOracle.fix_errorN)Ú__name__Ú
__module__Ú__qualname__rA   rL   r   r   r   r   r/   [   s    r/   )Úcollectionsr   Únumpyr   Ú,stanza.models.constituency.parse_transitionsr   r   r   r   r#   r+   r,   r.   r/   r   r   r   r   Ú<module>   s    
