ณ๒
4าวIc           @   s1   d  Z  d   Z d   Z d   Z d d  Z d S(   s#   
Tools for comparing ranked lists.
c         c   sf   t  |   }  t  |  } xG |  i   D]9 \ } } y | | | | f VWq% t j
 o q% Xq% Wd S(   sธ   Finds the difference between the values in ranks1 and ranks2 for keys
    present in both dicts. If the arguments are not dicts, they are converted
    from (key, rank) sequences.
    N(   t   dictt	   iteritemst   KeyError(   t   ranks1t   ranks2t   kt   v1(    (    s+   /p/zhu/06/nlp/nltk/nltk/metrics/spearman.pyt   _rank_dists   s     c      
   C   s   d } d } x5 t  |  |  D]$ \ } } | | | 7} | d 7} q Wy& d d t |  | | | d SWn t j
 o d Sn Xd S(   sU  Returns the Spearman correlation coefficient for two rankings, which
    should be dicts or sequences of (key, rank). The coefficient ranges from
    -1.0 (ranks are opposite) to 1.0 (ranks are identical), and is only
    calculated for keys in both rankings (for meaningful results, remove keys
    present in only one list before ranking).i    i   i   g        N(   R   t   floatt   ZeroDivisionError(   R   R   t   nt   resR   t   d(    (    s+   /p/zhu/06/nlp/nltk/nltk/metrics/spearman.pyt   spearman_correlation   s     &c         C   s   d   t  |   D S(   s   Given a sequence, yields each element with an increasing rank, suitable
    for use as an argument to L{spearman_correlation}.
    c         s   s%   x |  ] \ } } | | f Vq Wd  S(   N(    (   t   .0t   iR   (    (    s+   /p/zhu/06/nlp/nltk/nltk/metrics/spearman.pys	   <genexpr>0   s    (   t	   enumerate(   t   seq(    (    s+   /p/zhu/06/nlp/nltk/nltk/metrics/spearman.pyt   ranks_from_sequence,   s    gV็ฏา<c         c   s   d } d } xl t |   D]^ \ } \ } } y% t | |  | j o
 | } n Wn t j
 o n X| | f V| } q Wd S(   s๛   Given a sequence of (key, score) tuples, yields each key with an
    increasing rank, tying with previous key's rank if the difference between
    their scores is less than rank_gap. Suitable for use as an argument to
    L{spearman_correlation}.
    i    N(   t   NoneR   t   abst	   TypeError(   t   scorest   rank_gapt
   prev_scoret   rankR   t   keyt   score(    (    s+   /p/zhu/06/nlp/nltk/nltk/metrics/spearman.pyt   ranks_from_scores3   s     N(   t   __doc__R   R   R   R   (    (    (    s+   /p/zhu/06/nlp/nltk/nltk/metrics/spearman.pys   <module>
   s   			