
    9iG                         S SK r S SKJrJrJr  S SKJr  S SKJr  S SK	J
r
  S SKJrJr  S SKJr  S SKJr  S S	KJr  \" 5       r\R*                  " \\R,                  S
9 " S S\
5      5       rg)    N)DictListUnion)	DataFrame)Metrics)Metric)METRICS
MetricKeys)InputFormat)
get_logger)default_group)	group_keymodule_namec            	           \ rS rSrSrSS\4S jjrSS jrS\\	\
\   4   S	\\	\
\\\4      4   SS4S
 jrS\\	\\	\4   4   4S jrSS jrS\S\4S jrSrg)TranslationEvaluationMetric   z2The metric class for translation evaluation.

    gap_thresholdc                     Xl         [        5       U l        [        5       U l        [        5       U l        [        5       U l        [        5       U l        g)a  Build a translation evaluation metric, following the designed
Kendall's tau correlation from WMT Metrics Shared Task competitions.

Args:
    gap_threshold: The score gap denoting the available hypothesis pair.

Returns:
    A metric for translation evaluation.
N)r   listlp
segment_id	raw_scorescoreinput_format)selfr   s     p/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/metrics/translation_evaluation_metric.py__init__$TranslationEvaluationMetric.__init__   s8     +&&V
 F    returnNc                    U R                   R                  5         U R                  R                  5         U R                  R                  5         U R                  R                  5         U R
                  R                  5         g)z(Clear all the stored variables.
        N)r   clearr   r   r   r   )r   s    r   r"   !TranslationEvaluationMetric.clear+   sV     	!

r   outputsinputsc                     U =R                   US   -  sl         U =R                  US   -  sl        U =R                  US   -  sl        U =R                  US   -  sl        U =R                  US   -  sl        g)zCollect the related results for processing.

Args:
    outputs: Dict containing 'scores'
    inputs: Dict containing 'labels' and 'segment_ids'

r   r   r   r   r   Nr   r   r   r   r   )r   r$   r%   s      r   addTranslationEvaluationMetric.add7   sh     	6$<6,//&--VN33

gg&&
r   c           	      z   U R                   U R                  U R                  U R                  U R                  S.n[        US9n[        5       nUR                  R                  5        GHN  n[        R                  SUR                  -  5        XR                  U:H     n[        5       n[        UR                   R                  5       5       Hf  nXDR                   U:H     nU R                  U5      XSR                  S-   U-   '   [        R                  SUXSR                  S-   U-      S-  4-  5        Mh     [        UR                  5       5      [        U5      -  nXUR                  S-   '   [        R                  SUR                  U4-  5        [        R                  S	5        UR!                  U5        GMQ     U$ )
zaCompute the Kendall's tau correlation.

Returns:
    A dict denoting Kendall's tau correlation.

r'   )dataz&Evaluation results for %s input format_z	%s: %fd   _avgz1Average evaluation result for %s input format: %f )r   r   r   r   r   r   dictuniqueloggerinfovaluesortedcompute_kendall_tausumvalueslenupdate)	r   r+   correlationr   input_format_datatemp_correlationr   sub_dataavg_correlations	            r   evaluate$TranslationEvaluationMetric.evaluateJ   s    ''// --ZZ
 d#f --446LKK@&,,- . $%6%6,%F G#v.1188:;,-A-AR-GH)-)A)A()K !!3!3c!9#%"& '%&8&83&>&CDsJLLM	 < " '')+-01A-BCO7F**V34KKK%++_=> ?KKO/0- 70 r   c                 :   U =R                   UR                   -  sl         U =R                  UR                  -  sl        U =R                  UR                  -  sl        U =R                  UR                  -  sl        U =R
                  UR
                  -  sl        g)zMerge the predictions from other TranslationEvaluationMetric objects.

Args:
    other: Another TranslationEvaluationMetric object.

N)r   r   segment_idsr   r   r   )r   others     r   merge!TranslationEvaluationMetric.mergev   sh     	5885,,,%//)U///

ekk!
r   csv_datac                 d   S=p#[        UR                  R                  5       5       GH_  nXR                  U:H     nUR                  S5      n[	        S[        U5      5       GH  n[	        US-   [        U5      5       H  nU R                  U   U R                  U   -
  U R                  :  aP  U R                  U   U R                  U   :  a  US-  nMW  U R                  U   U R                  U   :  a  US-  nM~  M  U R                  U   U R                  U   -
  U R                  * ::  d  M  U R                  U   U R                  U   :  a  US-  nM  U R                  U   U R                  U   :  d  M  US-  nGM     GM"     GMb     X#-   S:X  a  [        R                  S5        gX#-
  X#-   -  $ )zCompute kendall's tau correlation.

Args:
    csv_data: The pandas dataframe.

Returns:
    float: THe kendall's Tau correlation.

r   records   znWe don't have available pairs when evaluation. Marking the kendall tau correlation as the lowest value (-1.0).g      )r5   r   r1   to_dictranger9   r   r   r   r2   warning)	r   rG   concordiscorr   group_csv_dataexamplesijs	            r   r6   /TranslationEvaluationMetric.compute_kendall_tau   s     !4!4!;!;!=>J%&9&9Z&GHN%--i8H1c(m,q1uc(m4A~~a(4>>, "&"4"45::a=4::a=8"aKF!ZZ]TZZ]:"aKF ;*T^^. #'#5#5"56::a=4::a=8"aKF!ZZ]TZZ]:"aKF 5 - ?( ?aNNR O88r   )r   r   r   r   r   r   )g      9@)r    N)rD   r   r    N)__name__
__module____qualname____firstlineno____doc__floatr   r"   r   strr   r   intr(   r@   rE   r   r6   __static_attributes__ r   r   r   r      s    #e #$
4T%[ 01 d5#4556;?&*$sDe$445 *X"'9I '9% '9r   r   )	importlibtypingr   r   r   pandasr   modelscope.metainfor   modelscope.metrics.baser   modelscope.metrics.builderr	   r
   )modelscope.models.nlp.unite.configurationr   modelscope.utils.loggerr   modelscope.utils.registryr   r2   register_moduletranslation_evaluation_metricr   r^   r   r   <module>rj      sb     $ $  ' * : A . 3	 	)N)NPZ9& Z9PZ9r   