JL iQ"dZdZdZdZddZy)z# Tools for comparing ranked lists. c#Kt|}t|}|D]} |||||z fy#t$rY!wxYww)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)dictKeyError)ranks1ranks2ks [/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/nltk/metrics/spearman.py _rank_distsr sY &\F &\F  VAY** *   s A1A =A=Acd}d}t||D]\}}|||zz }|dz } dd|z|||zdz zz z S#t$rYywxYw)aUReturns 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)rrnresrds rspearman_correlationrsu A CFF+1 q1u  QAGqAEAI/00 s; AAc&dt|DS)zGiven a sequence, yields each element with an increasing rank, suitable for use as an argument to ``spearman_correlation``. c3*K|] \}}||f yw)N).0irs r z&ranks_from_sequence..1s .tq!QF .s) enumerate)seqs rranks_from_sequencer-s /y~ ..c#Kd}d}t|D]$\}\}} t||z |kDr|}||f|}&y#t$rYwxYww)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 )rabs TypeError)scoresrank_gap prev_scorerankrkeyscores rranks_from_scoresr&4sm J D$V,r(s $/r