o
    
ig                     @   s   d dl mZmZmZmZmZ d dlmZmZ d dl	m
Z
 d dlmZ d dlmZ ee ddddd	ee d
edee
egef dee dedee dee deeef fddZd	ee deeef fddZdd Zd	ee deeef fddZdd ZdS )    )AnyCallableDictIterableOptional)PRFScoreROCAUCScore)Doc)Example)SimpleFrozenListTN)getterlabelsmulti_labelpositive_label	thresholdexamplesattrr   r   r   r   r   returnc                K   sd  |du r
|rdnd}dd |D }dd |D }	t |}|r5| D ]}
||
jj  ||
jj  q | D ]}||j|}||j|}|D ]}||d}||}|sZ|sZd}|durf|	| || qG|r|D ]F}||d}||}|dur||kr|dkr||  jd7  _qk||kr|dkr||  j	d7  _	qk||k r|dkr||  j
d7  _
qkq7|r|rt| d	d
 d\}}t| dd
 d\}}||kr||kr||  jd7  _q7||  j
d7  _
||kr||  j	d7  _	q7|rt|dd
 d\}}|dkr||  j
d7  _
q7|r1t| dd
 d\}}||kr1||  j	d7  _	q7t }| D ]}| j|j7  _| j
|j
7  _
| j	|j	7  _	q9t|d }tdd | D | }tdd | D | }tdd | D | }tdd |	 D | }| dd| dd| d|j| d|j| d|j| d|| d|| d|| d|| ddd | D | d d!d |	 D i}t|d"kr|s|r|| d | d# }||| d< d$| d%|| d< |S |s|| d || d< d&|| d< |S || d || d< d'|| d< |S )(a  Returns PRF and ROC AUC scores for a doc-level attribute with a
    dict with scores for each label like Doc.cats. The reported overall
    score depends on the scorer settings.

    examples (Iterable[Example]): Examples to score
    attr (str): The attribute to score.
    getter (Callable[[Doc, str], Any]): Defaults to getattr. If provided,
        getter(doc, attr) should return the values for the individual doc.
    labels (Iterable[str]): The set of possible labels. Defaults to [].
    multi_label (bool): Whether the attribute allows multiple labels.
        Defaults to True. When set to False (exclusive labels), missing
        gold labels are interpreted as 0.0.
    positive_label (str): The positive label for a binary task with
        exclusive classes. Defaults to None.
    threshold (float): Cutoff to consider a prediction "positive". Defaults
        to 0.5 for multi-label, and 0.0 (i.e. whatever's highest scoring)
        otherwise.
    RETURNS (Dict[str, Any]): A dictionary containing the scores, with
        inapplicable scores as None:
        for all:
            attr_score (one of attr_micro_f / attr_macro_f / attr_macro_auc),
            attr_score_desc (text description of the overall score),
            attr_micro_p,
            attr_micro_r,
            attr_micro_f,
            attr_macro_p,
            attr_macro_r,
            attr_macro_f,
            attr_macro_auc,
            attr_f_per_type,
            attr_auc_per_type
    Ng      ?        c                 S      i | ]}|t  qS  )r   .0labelr   r   H/home/ubuntu/.local/lib/python3.10/site-packages/spacy_legacy/scorers.py
<dictcomp>5       z!score_cats_v1.<locals>.<dictcomp>c                 S   r   r   )r   r   r   r   r   r   6   r   r      c                 S      | d S Nr   r   itr   r   r   <lambda>U       zscore_cats_v1.<locals>.<lambda>)keyc                 S   r   r   r   r    r   r   r   r"   V   r#   c                 S   r   r   r   r    r   r   r   r"   ^   r#   c                 S   r   r   r   r    r   r   r   r"   b   r#   g0.++c                 s       | ]}|j V  qd S N)	precisionr   prfr   r   r   	<genexpr>k       z score_cats_v1.<locals>.<genexpr>c                 s   r%   r&   )recallr(   r   r   r   r*   l   r+   c                 s   r%   r&   )fscorer(   r   r   r   r*   m   r+   c                 s   s"    | ]}|  r|jnd V  qdS )r   N	is_binaryscore)r   aucr   r   r   r*   q   s     _score_score_desc_micro_p_micro_r_micro_f_macro_p_macro_r_macro_f
_macro_auc_f_per_typec                 S   s   i | ]	\}}||  qS r   )to_dictr   kvr   r   r   r   ~   s    _auc_per_typec                 S   s$   i | ]\}}||  r|jnd qS r&   r.   r=   r   r   r   r      s       fzF ()zmacro Fz	macro AUC)setupdate	predictedcatskeys	referenceget	score_settpfpfnmaxitemsr   valueslensumr'   r,   r-   )r   r   r   r   r   r   r   cfg
f_per_typeauc_per_typeegexample	pred_cats	gold_catsr   
pred_score
gold_score
pred_label
gold_label	micro_prf	label_prfn_catsmacro_pmacro_rmacro_f	macro_aucresultspositive_label_fr   r   r   score_cats_v1   s   +










rh   c                 K      t | dfddi|S )NrG   r   Frh   r   kwargsr   r   r   textcat_score_v1   s   rm   c                   C      t S r&   )rm   r   r   r   r   make_textcat_scorer_v1      ro   c                 K   ri   )NrG   r   Trj   rk   r   r   r   textcat_multilabel_score_v1   s   rq   c                   C   rn   r&   )rq   r   r   r   r   !make_textcat_multilabel_scorer_v1   rp   rr   )typingr   r   r   r   r   spacy.scorerr   r   spacy.tokensr	   spacy.trainingr
   
spacy.utilr   getattrstrboolfloatrh   rm   ro   rq   rr   r   r   r   r   <module>   sH    


 		

