
    ig                        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Zd	ee   deeef   fdZd Zd	ee   deeef   fdZd Zy)    )AnyCallableDictIterableOptional)PRFScoreROCAUCScore)Doc)Example)SimpleFrozenListTN)getterlabelsmulti_labelpositive_label	thresholdexamplesattrr   r   r   r   r   returnc                f
   ||rdnd}|D ci c]  }|t                }	}|D ci c]  }|t                }
}t        |      }|rm| D ]h  }|j                  |j                  j
                  j                                |j                  |j                  j
                  j                                j | D ]!  } ||j                  |      } ||j                  |      }|D ]C  }|j                  |d      }|j                  |      }|s|sd}|/|
|   j                  ||       E |r|D ]  }|j                  |d      }|j                  |      }|)||k\  r|dkD  r|	|   xj                  dz  c_
        L||k\  r|dk(  r|	|   xj                  dz  c_        o||k  su|dkD  s{|	|   xj                  dz  c_         |r|rt        |j                         d       \  }}t        |j                         d       \  }}||k(  r||k\  r|	|   xj                  dz  c_
        s|	|   xj                  dz  c_        ||k\  s|	|   xj                  dz  c_        |r2t        |d	       \  }}|dkD  s|	|   xj                  dz  c_        |st        |j                         d
       \  }}||k\  s
|	|   xj                  dz  c_        $ t               }|	j                         D ]_  }|xj                  |j                  z  c_
        |xj                  |j                  z  c_        |xj                  |j                  z  c_        a t!        |	      dz   }t#        d |	j                         D              |z  }t#        d |	j                         D              |z  }t#        d |	j                         D              |z  }t#        d |
j                         D              |z  }| dd| dd| d|j$                  | d|j&                  | d|j(                  | d|| d|| d|| d|| d|	j                         D ci c]  \  }}||j+                          c}}| d|
j                         D ci c]$  \  }}||j-                         r|j.                  nd& c}}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 c c}w c c}w c c}}w c c}}w )!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      ?        r      c                     | d   S Nr    its    e/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/spacy_legacy/scorers.py<lambda>zscore_cats_v1.<locals>.<lambda>U   
    2a5     )keyc                     | d   S r   r   r   s    r   r   zscore_cats_v1.<locals>.<lambda>V   r   r    c                     | d   S r   r   r   s    r   r   zscore_cats_v1.<locals>.<lambda>^   s
    2a5 r    c                     | d   S r   r   r   s    r   r   zscore_cats_v1.<locals>.<lambda>b   r   r    g0.++c              3   4   K   | ]  }|j                     y wN)	precision.0prfs     r   	<genexpr>z score_cats_v1.<locals>.<genexpr>k   s     ?C#--?   c              3   4   K   | ]  }|j                     y wr&   )recallr(   s     r   r+   z score_cats_v1.<locals>.<genexpr>l        <#**<r,   c              3   4   K   | ]  }|j                     y wr&   )fscorer(   s     r   r+   z score_cats_v1.<locals>.<genexpr>m   r/   r,   c              3   X   K   | ]"  }|j                         r|j                  nd  $ yw)r   N)	is_binaryscore)r)   aucs     r   r+   z score_cats_v1.<locals>.<genexpr>q   s!     ScCIIc1Ss   (*_score_score_desc_micro_p_micro_r_micro_f_macro_p_macro_r_macro_f
_macro_auc_f_per_type_auc_per_type   fzF ()zmacro Fz	macro AUC)r   r	   setupdate	predictedcatskeys	referenceget	score_settpfpfnmaxitemsvalueslensumr'   r.   r1   to_dictr3   r4   )r   r   r   r   r   r   r   cfglabel
f_per_typeauc_per_typeegexample	pred_cats	gold_cats
pred_score
gold_score
pred_label
gold_label	micro_prf	label_prfn_catsmacro_pmacro_rmacro_f	macro_auckvresultspositive_label_fs                                 r   score_cats_v1rl      s   V &CC	178%#8J86<=UE;=(=L=[F 	4BMM",,++0023MM",,++0023	4  (/7,,d3	7,,d3	 	FE"uc2J"u-Jk 
%U#--j*E	F  	2&]]5#6
&]]51
)!Y.:>"5),,1,#y0Z1_"5),,1,#i/JN"5),,1,	2 9%():@P%Q"J
%():@P%Q"J
Z'J),C:&))Q.):&))Q.)*z*--2-%(8H%I"J
A~:&))Q.)%():@P%Q"J
Y&:&))Q.)Q(/R 
I&&( %		$	$	$% _v%F?:+<+<+>??&HG<
(9(9(;<<vEG<
(9(9(;<<vEG 	S\=P=P=RSS
	 
 &&d&9..&9++&9++&7&7&7&
Y&*:J:J:LM$!Qq!))+~M&>J>P>P>R!
6:aA!++-qwwT1!
G 6{a"dV;#78HM#34& *-n-=Q(?4&$% N #*dV8+<#=4& (14&$% N $+dV:+>#?4& (34&$%Nq 9=P N!
s   TT"T',)T-0c                      t        | dfddi|S )NrG   r   Frl   r   kwargss     r   textcat_score_v1rq      s)      	 r    c                      t         S r&   )rq   r   r    r   make_textcat_scorer_v1rs      s    r    c                      t        | dfddi|S )NrG   r   Trn   ro   s     r   textcat_multilabel_score_v1ru      s+       	 r    c                      t         S r&   )ru   r   r    r   !make_textcat_multilabel_scorer_v1rw      s    &&r    )typingr   r   r   r   r   spacy.scorerr   r	   spacy.tokensr
   spacy.trainingr   
spacy.utilr   getattrstrboolfloatrl   rq   rs   ru   rw   r   r    r   <module>r      s    : : .  " ' )0,.$(!%EwE
E c3Z_%	E
 SME E SME E 
#s(^EPx0 tCH~ w	#s(^'r    