
    +#h-                     |    d dl Z d dlZd dlZd dlmZ d dlmZ d Zd Zd Z	ddZ
d Zd	 Zd
 Zd Zd Zg dZddZy)    N)Counter)print_messagec                 T     t        |      t        k(  sJ t         fd|D              S )Nc              3   6   K   | ]  }t        |        y wN)em_score.0ans
predictions     N/var/www/html/sandstorm/venv/lib/python3.12/site-packages/dsp/utils/metrics.py	<genexpr>zEM.<locals>.<genexpr>        ASx
C(A   typelistmaxr   answers_lists   ` r   EMr   	   '    %%%ALAAA    c                 T     t        |      t        k(  sJ t         fd|D              S )Nc              3   6   K   | ]  }t        |        y wr   )f1_scorer	   s     r   r   zF1.<locals>.<genexpr>   r   r   r   r   s   ` r   F1r      r   r   c                 T     t        |      t        k(  sJ t         fd|D              S )Nc              3   6   K   | ]  }t        |        y wr   )hotpot_f1_scorer	   s     r   r   zHotPotF1.<locals>.<genexpr>   s     HCz3/Hr   r   r   s   ` r   HotPotF1r!      s'    %%%H<HHHr   c                 \     t        |      t        k(  sJ t         fd|D              S )Nc              3   <   K   | ]  }t        |         yw))return_recallN)novel_f1_score)r
   r   historyr   r$   s     r   r   znF1.<locals>.<genexpr>   s      mY\~gz3mTTms   r   )r&   r   r   r$   s   `` `r   nF1r'      s(    %%%m`lmmmr   c           	      z    t        j                  d|       } d }d }d }d } | | | ||                         S )NNFDc                 0    t        j                  dd|       S )Nz\b(a|an|the)\b )resubtexts    r   remove_articlesz'normalize_text.<locals>.remove_articles$   s    vv'd33r   c                 @    dj                  | j                               S )Nr+   )joinsplitr.   s    r   white_space_fixz'normalize_text.<locals>.white_space_fix'   s    xx

%%r   c                 j    t        t        j                        dj                  fd| D              S )N c              3   ,   K   | ]  }|vs|  y wr    )r
   chexcludes     r   r   z6normalize_text.<locals>.remove_punc.<locals>.<genexpr>,   s     >bBg,=r>s   	)setstringpunctuationr2   )r/   r:   s    @r   remove_puncz#normalize_text.<locals>.remove_punc*   s(    f(()ww>D>>>r   c                 "    | j                         S r   )lowerr.   s    r   r@   znormalize_text.<locals>.lower.   s    zz|r   )unicodedata	normalize)sr0   r4   r>   r@   s        r   normalize_textrD   !   sC    eQ'A4&? ?;uQx+@ABBr   c                 0    t        |       t        |      k(  S r   )rD   )r   ground_truths     r   r   r   4   s    *%)EEEr   c                    t        |       j                         }t        |      j                         }t        |      t        |      z  }t        |j	                               }t        |      t        |      cxk(  rdk(  rn nt        d       |dk(  ryd|z  t        |      z  }d|z  t        |      z  }d|z  |z  ||z   z  }|S )Nr   Z
#> F1 Metric: Rare edge case of len(prediction_tokens) == len(ground_truth_tokens) == 0.
      ?   rD   r3   r   sumvalueslenr   )	r   rF   prediction_tokensground_truth_tokenscommonnum_same	precisionrecallf1s	            r   r   r   <   s    &z288:(6<<>&''2E*FFF6==?#H
%8!9>Q>j	l 1}h%6!77I8^c"566F
i-&
 Y%7	8BIr   c                 f   t        |       }t        |      }|dv r||k7  ry|dv r||k7  ry|j                         }|j                         }t        |      t        |      z  }t        |j	                               }|dk(  ryd|z  t        |      z  }d|z  t        |      z  }	d|z  |	z  ||	z   z  }
|
S )N)yesnonoanswerr   rI   rJ   )rD   r3   r   rL   rM   rN   )r   rF   normalized_predictionnormalized_ground_truthrO   rP   rQ   rR   rS   rT   rU   s              r   r    r    R   s    *:6,\: 99>SWn>n";;@UYp@p-3351779&''2E*FFF6==?#H1}h%6!77I8^c"566F
i-&
 Y%7	8BIr   c                 N   t        |       j                         }t        |      j                         }t        |      t        |      z  }t        |j	                               }t        |      t        |      cxk(  rdk(  rn nt        d       |dk(  ryd|z  t        |      z  }|S )Nr   rH   rI   rK   )r   rF   rO   rP   rQ   rR   rS   s          r   precision_scorer]   g   s    &z288:(6<<>&''2E*FFF6==?#H
%8!9>Q>j	l 1}h%6!77Ir   )imemymyselfweourours	ourselvesyouyouryoursyourself
yourselveshehimhishimselfsheherhersherselfititsitselftheythemtheirtheirs
themselveswhatwhichwhowhomthisthatthesethoseamisarewaswerebebeenbeinghavehashadhavingdodoesdiddoingaantheandbutiforbecauseasuntilwhileofatbyforwithaboutagainstbetweenintothroughduringbeforeafterabovebelowtofromupdowninoutonoffoverunderagainfurtherthenonceheretherewhenwherewhyhowallanybotheachfewmoremostothersomesuchrX   nornotonlyownsamesothantooveryrC   tcanwilljustdonshouldnowc                    t        |       j                         }t        |      j                         }t        |      j                         }t        |t        z         }|D cg c]	  }||vs| }}|D cg c]	  }||vs| }}t	        |      t	        |      z  }t        |j                               }	|	dk(  ryd|	z  t        |      z  }
d|	z  t        |      z  }d|
z  |z  |
|z   z  }|r|S |S c c}w c c}w )Nr   rI   rJ   )rD   r3   r;   	stopwordsr   rL   rM   rN   )r&   r   rF   r$   history_tokensrO   rP   r   rQ   rR   rS   rT   rU   s                r   r%   r%      s%   #G,224N&z288:(6<<>)34N %A(?A A 'C!>*AC C &''2E*FFF6==?#H1}h%6!77I8^c"566F
i-&
 Y%7	8BI#ACs   "	C2,C26	C7 C7)F)r,   r<   rA   collectionsr   dsp.utils.utilsr   r   r   r!   r'   rD   r   r   r    r]   r   r%   r8   r   r   <module>r      sT    	    )BBInC&F,**	U	r   