3
­d•  ã               @   s*   d Z dd„ Zdd„ Zdd„ Zddd	„Zd
S )z#
Tools for comparing ranked lists.
c             c   sR   t | ƒ} t |ƒ}x<| D ]4}y|| | ||  fV  W q tk
rH   Y qX qW dS )z¸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)ÚdictÚKeyError)Úranks1Úranks2Úk© r   ú5/tmp/pip-build-v9q4h5k9/nltk/nltk/metrics/spearman.pyÚ_rank_dists   s    
r   c             C   sj   d}d}x*t | |ƒD ]\}}||| 7 }|d7 }qW ydd| ||| d    S  tk
rd   dS X dS )aU  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).é    é   é   g        N)r   ÚZeroDivisionError)r   r   ÚnÚresr   Údr   r   r   Úspearman_correlation   s    r   c             C   s   dd„ t | ƒD ƒS )z„Given a sequence, yields each element with an increasing rank, suitable
    for use as an argument to ``spearman_correlation``.
    c             s   s   | ]\}}||fV  qd S )Nr   )Ú.0Úir   r   r   r   ú	<genexpr>1   s    z&ranks_from_sequence.<locals>.<genexpr>)Ú	enumerate)Úseqr   r   r   Úranks_from_sequence-   s    r   çVçž¯Ò<c             c   sb   d}d}xTt | ƒD ]H\}\}}yt|| ƒ|kr4|}W n tk
rJ   Y nX ||fV  |}qW dS )zü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
    ``spearman_correlation``.
    Nr	   )r   ÚabsÚ	TypeError)ZscoresZrank_gapZ
prev_scoreZrankr   ÚkeyZscorer   r   r   Úranks_from_scores4   s    
r   N)r   )Ú__doc__r   r   r   r   r   r   r   r   Ú<module>
   s   