³ò
4ÒÇIc           @   sè   d  d k  Z  d  d k Z d  d k Z y d  d k l Z Wn e j
 o d Z n Xd  d k l Z l	 Z	 d  d k
 l Z d  d k l Z d „  Z d „  Z d „  Z d	 d
 „ Z d „  Z d „  Z d „  Z e d j o e ƒ  n d S(   iÿÿÿÿN(   t   betai(   t   LazyConcatenationt   LazyMap(   t   izip(   t   FreqDistc         C   s~   t  |  ƒ t  | ƒ j o t d ƒ ‚ n d } x8 t |  | ƒ D]' \ } } | | j o | d 7} q? q? Wt | ƒ t  |  ƒ S(   s2  
    Given a list of reference values and a corresponding list of test
    values, return the percentage of corresponding values that are
    equal.  In particular, return the percentage of indices
    C{0<i<=len(test)} such that C{test[i] == reference[i]}.

    @type reference: C{list}
    @param reference: An ordered list of reference values.
    @type test: C{list}
    @param test: A list of values to compare against the corresponding
        reference values.
    @raise ValueError: If C{reference} and C{length} do not have the
        same length.
    s    Lists must have the same length.i    i   (   t   lent
   ValueErrorR   t   float(   t	   referencet   testt   num_correctt   xt   y(    (    s)   /p/zhu/06/nlp/nltk/nltk/metrics/scores.pyt   accuracy   s     c         C   st   t  |  d ƒ p t  | d ƒ o t d ƒ ‚ n t | ƒ d j o d Sn$ t t |  i | ƒ ƒ ƒ t | ƒ Sd S(   sÍ  
    Given a set of reference values and a set of test values, return
    the percentage of test values that appear in the reference set.
    In particular, return |C{reference}S{cap}C{test}|/|C{test}|.
    If C{test} is empty, then return C{None}.
    
    @type reference: C{Set}
    @param reference: A set of reference values.
    @type test: C{Set}
    @param test: A set of values to compare against the reference set.
    @rtype: C{float} or C{None}
    t   intersections!   reference and test should be setsi    N(   t   hasattrt	   TypeErrorR   t   NoneR   R   (   R   R	   (    (    s)   /p/zhu/06/nlp/nltk/nltk/metrics/scores.pyt	   precision.   s    c         C   st   t  |  d ƒ p t  | d ƒ o t d ƒ ‚ n t |  ƒ d j o d Sn$ t t |  i | ƒ ƒ ƒ t |  ƒ Sd S(   s×  
    Given a set of reference values and a set of test values, return
    the percentage of reference values that appear in the test set.
    In particular, return |C{reference}S{cap}C{test}|/|C{reference}|.
    If C{reference} is empty, then return C{None}.
    
    @type reference: C{Set}
    @param reference: A set of reference values.
    @type test: C{Set}
    @param test: A set of values to compare against the reference set.
    @rtype: C{float} or C{None}
    R   s!   reference and test should be setsi    N(   R   R   R   R   R   R   (   R   R	   (    (    s)   /p/zhu/06/nlp/nltk/nltk/metrics/scores.pyt   recallD   s    g      à?c         C   sz   t  |  | ƒ } t |  | ƒ } | t j p | t j o t Sn | d j p | d j o d Sn d | | d | | S(   s3  
    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
    L{precision} and L{recall}, weighted by C{alpha}.  In particular,
    given the precision M{p} and recall M{r} defined by:
        - M{p} = |C{reference}S{cap}C{test}|/|C{test}|
        - M{r} = |C{reference}S{cap}C{test}|/|C{reference}|
    The f-measure is:
        - 1/(C{alpha}/M{p} + (1-C{alpha})/M{r})
        
    If either C{reference} or C{test} is empty, then C{f_measure}
    returns C{None}.
    
    @type reference: C{Set}
    @param reference: A set of reference values.
    @type test: C{Set}
    @param test: A set of values to compare against the reference set.
    @rtype: C{float} or C{None}
    i    g      ð?i   (   R   R   R   (   R   R	   t   alphat   pt   r(    (    s)   /p/zhu/06/nlp/nltk/nltk/metrics/scores.pyt	   f_measureZ   s    c         C   sV   t  |  ƒ t  | ƒ j o t d ƒ ‚ n t d „  t |  | ƒ Dƒ ƒ } | t  |  ƒ S(   sÅ  
    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: C{list}
    @param test: A list of probability distributions over values to
        compare against the corresponding reference values.
    @type test: C{list} of L{ProbDistI}
    s    Lists must have the same length.c         s   s(   x! |  ] \ } } | i  | ƒ Vq Wd  S(   N(   t   logprob(   t   .0t   valt   dist(    (    s)   /p/zhu/06/nlp/nltk/nltk/metrics/scores.pys	   <genexpr>‡   s   	(   R   R   t   sumt   zip(   R   R	   t   total_likelihood(    (    s)   /p/zhu/06/nlp/nltk/nltk/metrics/scores.pyt   log_likelihoodw   s
    	c      
      s‡  | i  d d ƒ } t | t d „  t d t |  ƒ t | ƒ d ƒ ƒ ƒ } | i  d d „  ƒ } | i  d t ƒ } | o d | GHn t i | |  ƒ | | ƒ ƒ } | o d	 | GHd
 d GHn d } t |  | g ƒ ‰  t	 t |  ƒ t | ƒ ƒ } xt	 | ƒ D]ÿ }	 | o |	 d d j o d |	 GHn t
 i | ƒ | t ‡  f d †  | t |  ƒ  ƒ ƒ }
 | t ‡  f d †  | t |  ƒ ƒ ƒ } t i |
 | ƒ } | | j o | d 7} n | oB |	 d d j o1 d | GHd t | d ƒ |	 d GHd
 d GHqqWt | d ƒ | d } | oV d | GHt oB x? d d d d d d g D]! } d | t | | | ƒ f GHqMWqzn | | | f S(   s  
    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: C{tuple}
    @param a: a list of test values
    @type a: C{list}
    @param b: another list of independently generated test values
    @type b: C{list}
    t   shufflesiç  c         S   s   |  | S(    (    (   R   R   (    (    s)   /p/zhu/06/nlp/nltk/nltk/metrics/scores.pyt   <lambda>£   s    i   t	   statisticc         S   s   t  t |  ƒ ƒ t |  ƒ S(    (   R   R   R   (   t   lst(    (    s)   /p/zhu/06/nlp/nltk/nltk/metrics/scores.pyR!   ¤   s    t   verboses   shuffles: %ds   actual statistic: %ft   -i<   g0Žä.ÿ++i
   i    s   shuffle: %dc            s   ˆ  |  S(    (    (   t   i(   R#   (    s)   /p/zhu/06/nlp/nltk/nltk/metrics/scores.pyR!   º   s    c            s   ˆ  |  S(    (    (   R&   (   R#   (    s)   /p/zhu/06/nlp/nltk/nltk/metrics/scores.pyR!   »   s    s   pseudo-statistic: %fs   significance: %fg{®Gáz„?gš™™™™™©?gš™™™™™¹?g333333Ã?g      Ð?g      à?s   prob(phi<=%f): %f(   t   gett   mint   reducet   xrangeR   t   Falset   matht   fabsR   t   ranget   randomt   shuffleR   R   R    (   t   at   bt   kwargsR    t   statR$   t   actual_statt   ct   indicesR&   t   pseudo_stat_at   pseudo_stat_bt   pseudo_statt   significancet   phi(    (   R#   s)   /p/zhu/06/nlp/nltk/nltk/metrics/scores.pyt
   approxrand‹   sF    8	 ((		 'c          C   s»   d d GHd i  ƒ  }  d i  ƒ  } d G|  GHd G| GHd Gt |  | ƒ GHd d GHt |  ƒ } t | ƒ } d G| GHd G| GHd	 Gt | | ƒ GHd
 Gt | | ƒ GHd Gt | | ƒ GHd d GHd  S(   NR%   iK   s    DET NN VB DET JJ NN NN IN DET NNs    DET VB VB DET NN NN NN IN DET NNs   Reference =s	   Test    =s	   Accuracy:s	   Test =   s
   Precision:s
      Recall:s
   F-Measure:(   t   splitR   t   setR   R   R   (   R   R	   t   reference_sett   test_set(    (    s)   /p/zhu/06/nlp/nltk/nltk/metrics/scores.pyt   demoÑ   s    						t   __main__(   t   sysR,   R/   t   scipy.stats.statsR    t   ImportErrorR   t	   nltk.utilR   R   t	   itertoolsR   t   nltk.probabilityR   R   R   R   R   R   R=   RB   t   __name__(    (    (    s)   /p/zhu/06/nlp/nltk/nltk/metrics/scores.pys   <module>
   s$   					F	