
    /h                         S SK r S SK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 rS rS	 rSS
 jrS rS rS r\S:X  a  \" 5         gg! \	 a    Sr N8f = f)    N)reduce)fabs)shuffle)betai)LazyConcatenationLazyMapc                     [        U 5      [        U5      :w  a  [        S5      e[        S [        X5       5       5      [        U5      -  $ )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   .   #    U  H  u  pX:H  v   M     g 7fN ).0xys      K/var/www/auris/envauris/lib/python3.13/site-packages/nltk/metrics/scores.py	<genexpr>accuracy.<locals>.<genexpr>'   s     7"6$!qv"6s   len
ValueErrorsumzip	referencetests     r   accuracyr      s?     9~T";<<7#i"677#d)CC    c                     [        U S5      (       a  [        US5      (       d  [        S5      e[        U5      S:X  a  g[        U R                  U5      5      [        U5      -  $ )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$   *   sV     9n--WT>5R5R;<<
4yA~9))$/03t9<<r   c                     [        U S5      (       a  [        US5      (       d  [        S5      e[        U 5      S:X  a  g[        U R                  U5      5      [        U 5      -  $ )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&   @   sW     9n--WT>5R5R;<<
9~9))$/03y>AAr   c                 v    [        X5      n[        X5      nUb  Uc  gUS:X  d  US:X  a  gSX#-  SU-
  U-  -   -  $ )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   sN    0 	)"AyAyAIAva%)q5yAo-..r   c                     [        U 5      [        U5      :w  a  [        S5      e[        S [        X5       5       5      nU[        U 5      -  $ )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   H   #    U  H  u  pUR                  U5      v   M     g 7fr   )logprob)r   valdists      r   r   !log_likelihood.<locals>.<genexpr>   s      U@T#4<<,,@Ts    "r   )r   r   total_likelihoods      r   log_likelihoodr4   w   sG     9~T";<< UI@TUUc)n,,r   c                 h  ^ UR                  SS5      n[        U[        [        R                  [        S[        U 5      [        U5      -   S-   5      5      5      nUR                  SS 5      nUR                  SS5      nU(       a  [        SU-  5        [        U" U 5      U" U5      -
  5      nU(       a  [        S	U-  5        [        S
5        Sn[        X/5      m[        [        [        U 5      [        U5      -   5      5      n[        U5       H  n	U(       a  U	S-  S:X  a  [        SU	-  5        [        U5        U" [        U4S jUS[        U 5       5      5      n
U" [        U4S jU[        U 5      S 5      5      n[        X-
  5      nX:  a  US-  nU(       d  M  U	S-  S:X  d  M  [        SU-  5        [        SUS-   U	S-   -  -  5        [        S
5        M     US-   US-   -  nU(       a?  [        SU-  5        [        (       a&  S H   n[        SUS S[        XsU5      S 35        M"     XU4$ )a*  
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    [        U 5      [        U 5      -  $ r   )r   r   )lsts    r   <lambda>approxrand.<locals>.<lambda>   s    s3x#c(/Br   verboseFzshuffles: %dzactual statistic: %fz<------------------------------------------------------------g0.++
   r   zshuffle: %dc                    > TU    $ r   r   ir9   s    r   r:   r;      	    s1vr   Nc                    > TU    $ r   r   r?   s    r   r:   r;      rA   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kwargsr6   statr<   actual_statcindicesr@   pseudo_stat_apseudo_stat_bpseudo_statsignificancephir9   s                  @r   
approxrandrX      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
QF
#C5Q#a&)*G8_q2v{-!#$W%5wxQ7HIJW%5ws1vx7HIJ=89%FA7q2v{(;67$Q1q5(9:;(O! $ Ehl+L </05;
3q'U1-DQ,GHI < X&&r   c                     [        S5        SR                  5       n SR                  5       n[        SU 5        [        SU5        [        S[        X5      5        [        S5        [        U 5      n[        U5      n[        SU5        [        SU5        [        S[	        X#5      5        [        S	[        X#5      5        [        S
[        X#5      5        [        S5        g )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:)rJ   splitr   setr$   r&   r,   )r   r   reference_settest_sets       r   demor^      s    	(O288:I-335D	-#	+t	+x	01	(O	NM4yH	-'	+x 	,	-:;	,}78	,	-:;	(Or   __main__)rC   )rG   	functoolsr   mathr   randomr   scipy.stats.statsr   ImportError	nltk.utilr   r   r   r$   r&   r,   r4   rX   r^   __name__r   r   r   <module>rg      ss       ' 1D(=,B,/B-(B'J& zF g  Es   A AA