o
    rZhQ                     @   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   sL    t | } t |}| D ]}z|| | ||  fV  W q ty#   Y qw dS )zFinds 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   D/var/www/auris/lib/python3.10/site-packages/nltk/metrics/spearman.py_rank_dists   s   r   c                 C   sd   d}d}t | |D ]\}}||| 7 }|d7 }q	zdd| ||| d    W S  ty1   Y dS w )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        )r   ZeroDivisionError)r   r   nresr   dr   r   r   spearman_correlation   s   
r   c                 C   s   dd t | D S )zGiven 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   s^    d}d}t | D ]#\}\}}zt|| |kr|}W n	 ty$   Y nw ||fV  |}q	dS )zGiven 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
   