o
    h                     @   sV   d dl mZmZ edg dZdd Zddd	Zd
d Zdd Zdd ZdddZ	dS )    )defaultdict
namedtupleF1Result)	precisionrecallf1c                 C   s   t dd }g }g }|D ]S}|ddkr|ddd }n|}||vr)|| |D ]3}|ddkr=|ddd }	n|}	|	|vrH||	 | |i |d}
|| |	 |
 || |	< q+q|||fS )Nc                   S      t tS Nr   int r   r   Q/var/www/html/env_mimamsha/lib/python3.10/site-packages/stanza/utils/confusion.py<lambda>       z%condense_ner_labels.<locals>.<lambda>-r      )r   findsplitappendget)	confusiongold_labelspred_labelsnew_confusionnew_gold_labelsnew_pred_labelsl1new_l1l2new_l2	old_valuer   r   r   condense_ner_labels   s&   



r!   NFc                    s  dd }|r(t dd } D ]}|  D ]}| | || |< qq||du ryt }	|r?tfdd|	D }	t }
 D ] |r[tfdd  D }n  }|
|}
qF|sp|	|
}	|	}
||	}	||
}
n|}	|}
td	d
 |
D dg  d  }d}t|	D ]"\}}t|
D ]\}}t|i |dtsd} nq|s nq|r fdd}n fdd}t|	D ] \}}t|
D ]\}}|i |d}t t	|| qqɈ t	|
 dkrt
|	|
\}	}
|rd}nd} d d d |  d d d  }t	|t	|k r*dt	|t	|  | }d| d }|
D ]}|d t|  }q2| g}t|	D ]=\}}d t| }t|
D ]#\}}|i |d}||}|rw|ru|n|}|| d }q[||  qJd|S )z
    pretty print for confusion matrixes
    adapted from https://gist.github.com/zachguo/10296432

    The matrix should look like this:
      confusion[gold][pred]
    c                 S   sb   t | } d| v rd}| d nd}tdd | D s t| } nt| dd d} |r/dg|  } | S )	zn
        Sorts the labels in the list, respecting BIES if all labels are BIES, putting O at the front
        OTFc                 s   s>    | ]}t |tot|d ko|d dv o|d dv V  qdS )   r   )BIESr   )r   _N)
isinstancestrlen.0xr   r   r   	<genexpr>1   s   < z8format_confusion.<locals>.sort_labels.<locals>.<genexpr>c                 S   s   | dd  | d fS )Nr#   r   r   r.   r   r   r   r   5   s    z7format_confusion.<locals>.sort_labels.<locals>.<lambda>)key)setremoveallsorted)labelshad_Or   r   r   sort_labels&   s   

z%format_confusion.<locals>.sort_labelsc                   S   r   r	   r
   r   r   r   r   r   ;   r   z"format_confusion.<locals>.<lambda>Nc                 3   s4    | ] t  fd d   D r V  qdS )c                 3   s     | ]}  | d kV  qdS r   Nr   )r-   r1   )r   r.   r   r   r/   D   s    z-format_confusion.<locals>.<genexpr>.<genexpr>N)anykeys)r-   )r   r0   r   r/   D   s   2 z#format_confusion.<locals>.<genexpr>c                 3   s$    | ]}  | d kr|V  qdS r9   r   r,   )r   r1   r   r   r/   I   s   " c                 S   s   g | ]}t t|qS r   )r+   r*   r,   r   r   r   
<listcomp>X   s    z$format_confusion.<locals>.<listcomp>    Tr   Fc                       d  |  S )Nz%{0}dformatconfusion_cellcolumnwidthr   r   r   f       c                    r?   )Nz%{0}.1fr@   rB   rD   r   r   r   h   rF      zp\tzt\p   r#   z    z%{0}s z
    %{0}s 
)r   r;   r2   unionmax	enumerater)   r   r   r+   r!   rA   r*   rstripr   join)r   r6   hide_zeroes
hide_blank	transposer8   r   label1label2r   r   r   
empty_cellall_intsijformat_cellcellcorner_labelfst_empty_cellheaderlabeltextrowrC   r   )rE   r   r1   r   format_confusion   s   "

$

r`   c                 C   s^   d}d}|   D ] }| |   D ]}||kr|| | |  }q|| | |  }qq||| fS )z=
    Given a confusion dictionary, return correct, total
    r   )r;   )confusion_matrixcorrecttotalr   r   r   r   r   confusion_to_accuracy   s   rd   c                 C   s(  i }t  }|  D ]}|| | | D ]}|| qq	d}|D ]m}d}d}d}|D ])}||kr?| |i |d}q.|| |i |d }|| |i |d }q.|| dkrad}	n|||  }	|| dkrpd}
n|||  }
|	|
 dkrd}n
d|	|
  |	|
  }t|	|
|||< q$|S )Nr           r#   )r2   r;   addr   r   )ra   resultsr;   kk2sum_f1tpfnfpr   r   r   r   r   r   confusion_to_f1   s8   
rn   c                 C   s4   d}t | }| D ]	}||| j }q
|t| S )z5
    Return the macro f1 for a confusion matrix.
    re   )rn   r;   r   r+   )ra   rj   rg   rh   r   r   r   confusion_to_macro_f1   s
   ro   c                 C   sf   t | }d}d}| D ]"}|d ur||v rqt| |i  }||7 }||| j| 7 }q|| S )Nre   r   )rn   r;   sumr   valuesr   )ra   excluderg   rj   total_itemsrh   k_itemsr   r   r   confusion_to_weighted_f1   s   ru   )NFFFr	   )
collectionsr   r   r   r!   r`   rd   rn   ro   ru   r   r   r   r   <module>   s   
q%