
    g                         d dl Z d dlmZ d dlmZ d dlmZ 	 d dlmZ d dl
mZmZ d Zd Zd	 Zdd
Zd Zd Zd Zedk(  r e        yy# e	$ r dZY 5w xY w)    N)reduce)fabs)shuffle)betai)LazyConcatenationLazyMapc                     t        |       t        |      k7  rt        d      t        d t        | |      D              t        |      z  S )a,  
    Given a list of reference values and a corresponding list of test
    values, return the fraction of corresponding values that are
    equal.  In particular, return the fraction of indices
    ``0<i<=len(test)`` such that ``test[i] == reference[i]``.

    :type reference: list
    :param reference: An ordered list of reference values.
    :type test: list
    :param test: A list of values to compare against the corresponding
        reference values.
    :raise ValueError: If ``reference`` and ``length`` do not have the
        same length.
     Lists must have the same length.c              3   ,   K   | ]  \  }}||k(    y wN ).0xys      H/var/www/openai/venv/lib/python3.12/site-packages/nltk/metrics/scores.py	<genexpr>zaccuracy.<locals>.<genexpr>'   s     7"6$!QqAv"6s   len
ValueErrorsumzip	referencetests     r   accuracyr      sA     9~T";<<7#i"677#d)CC    c                     t        | d      rt        |d      st        d      t        |      dk(  ryt        | j                  |            t        |      z  S )a  
    Given a set of reference values and a set of test values, return
    the fraction of test values that appear in the reference set.
    In particular, return card(``reference`` intersection ``test``)/card(``test``).
    If ``test`` is empty, then return None.

    :type reference: set
    :param reference: A set of reference values.
    :type test: set
    :param test: A set of values to compare against the reference set.
    :rtype: float or None
    intersection!reference and test should be setsr   Nhasattr	TypeErrorr   r   r   s     r   	precisionr#   *   sP     9n-WT>5R;<<
4yA~9))$/03t9<<r   c                     t        | d      rt        |d      st        d      t        |       dk(  ryt        | j                  |            t        |       z  S )a  
    Given a set of reference values and a set of test values, return
    the fraction of reference values that appear in the test set.
    In particular, return card(``reference`` intersection ``test``)/card(``reference``).
    If ``reference`` is empty, then return None.

    :type reference: set
    :param reference: A set of reference values.
    :type test: set
    :param test: A set of values to compare against the reference set.
    :rtype: float or None
    r   r   r   Nr    r   s     r   recallr%   @   sQ     9n-WT>5R;<<
9~9))$/03y>AAr   c                 t    t        | |      }t        | |      }||y|dk(  s|dk(  ryd||z  d|z
  |z  z   z  S )a-  
    Given a set of reference values and a set of test values, return
    the f-measure of the test values, when compared against the
    reference values.  The f-measure is the harmonic mean of the
    ``precision`` and ``recall``, weighted by ``alpha``.  In particular,
    given the precision *p* and recall *r* defined by:

    - *p* = card(``reference`` intersection ``test``)/card(``test``)
    - *r* = card(``reference`` intersection ``test``)/card(``reference``)

    The f-measure is:

    - *1/(alpha/p + (1-alpha)/r)*

    If either ``reference`` or ``test`` is empty, then ``f_measure``
    returns None.

    :type reference: set
    :param reference: A set of reference values.
    :type test: set
    :param test: A set of values to compare against the reference set.
    :rtype: float or None
    Nr   g      ?   )r#   r%   )r   r   alphaprs        r   	f_measurer+   V   sT    0 	)T"Ay$AyAIAva%!)q5yAo-..r   c                     t        |       t        |      k7  rt        d      t        d t        | |      D              }|t        |       z  S )a  
    Given a list of reference values and a corresponding list of test
    probability distributions, return the average log likelihood of
    the reference values, given the probability distributions.

    :param reference: A list of reference values
    :type reference: list
    :param test: A list of probability distributions over values to
        compare against the corresponding reference values.
    :type test: list(ProbDistI)
    r
   c              3   D   K   | ]  \  }}|j                  |        y wr   )logprob)r   valdists      r   r   z!log_likelihood.<locals>.<genexpr>   s      U@T#t4<<,@Ts    r   )r   r   total_likelihoods      r   log_likelihoodr2   w   sI     9~T";<< UIt@TUUc)n,,r   c                     |j                  dd      }t        |t        t        j                  t        dt        |       t        |      z   dz                     }|j                  dd       }|j                  dd      }|rt        d|z         t         ||        ||      z
        }|rt        d	|z         t        d
       d}t        | |g      t        t        t        |       t        |      z               }t        |      D ]  }	|r|	dz  dk(  rt        d|	z         t        |        |t        fd|dt        |                    }
 |t        fd|t        |       d             }t        |
|z
        }||k\  r|dz  }|s|	dz  dk(  st        d|z         t        d|dz   |	dz   z  z         t        d
        |dz   |dz   z  }|r9t        d|z         t        r%dD ]   }t        d|ddt        |||      d       " |||fS )an  
    Returns an approximate significance level between two lists of
    independently generated test values.

    Approximate randomization calculates significance by randomly drawing
    from a sample of the possible permutations. At the limit of the number
    of possible permutations, the significance level is exact. The
    approximate significance level is the sample mean number of times the
    statistic of the permutated lists varies from the actual statistic of
    the unpermuted argument lists.

    :return: a tuple containing an approximate significance level, the count
             of the number of times the pseudo-statistic varied from the
             actual statistic, and the number of shuffles
    :rtype: tuple
    :param a: a list of test values
    :type a: list
    :param b: another list of independently generated test values
    :type b: list
    shufflesi  r'   	statisticc                 0    t        |       t        |       z  S r   )r   r   )lsts    r   <lambda>zapproxrand.<locals>.<lambda>   s    s3x#c(/Br   verboseFzshuffles: %dzactual statistic: %fz<------------------------------------------------------------g0.++
   r   zshuffle: %dc                     |    S r   r   ir7   s    r   r8   zapproxrand.<locals>.<lambda>   	    s1vr   Nc                     |    S r   r   r<   s    r   r8   zapproxrand.<locals>.<lambda>   r>   r   zpseudo-statistic: %fzsignificance: %f)g{Gz?g?g?g333333?g      ?      ?z
prob(phi<=fz): )getminr   operatormulranger   printr   r   listr   r   r   )abkwargsr4   statr9   actual_statcindicesr=   pseudo_stat_apseudo_stat_bpseudo_statsignificancephir7   s                  @r   
approxrandrU      s   * zz*c*H8VHLL%3q6CF?Q;N2OPQH::k#BCDjjE*Gnx'(tAwa()K${23hA
QF
#C5Q#a&)*G8_q2v{-!#$W%5wxQ7HIJW%5ws1vx7HIJ==89+%FAq2v{(;67$Q1q5(9:;(O! $ Ehl+L </0;
3q'U1h-DQ,GHI < !X&&r   c                     t        d       dj                         } dj                         }t        d|        t        d|       t        dt        | |             t        d       t        |       }t        |      }t        d|       t        d|       t        dt	        ||             t        d	t        ||             t        d
t        ||             t        d       y )NzK---------------------------------------------------------------------------z DET NN VB DET JJ NN NN IN DET NNz DET VB VB DET NN NN NN IN DET NNzReference =z	Test    =z	Accuracy:z	Test =   z
Precision:z
   Recall:z
F-Measure:)rG   splitr   setr#   r%   r+   )r   r   reference_settest_sets       r   demor[      s    	(O288:I-335D	-#	+t	+x	401	(O	NM4yH	-'	+x 	,	-:;	,}h78	,	-:;	(Or   __main__)r@   )rD   	functoolsr   mathr   randomr   scipy.stats.statsr   ImportError	nltk.utilr   r   r   r#   r%   r+   r2   rU   r[   __name__r   r   r   <module>rd      ss       ' 1D(=,B,/B-(B'J& zF g  Es   A
 
AA