RankedList¶
Ranked lists with tied-average ranks and tied-last handling for exclusives.
keyflux.ranking.rankedlist.RankedList
dataclass
¶
A type-to-rank mapping derived from counts or scores.
Attributes:
| Name | Type | Description |
|---|---|---|
ranks |
Mapping[str, float]
|
Type-to-rank mapping within this list's own domain (average ties). |
counts |
Mapping[str, float]
|
The values the ranks were derived from — frequency counts
(:meth: |
total |
float
|
Sum of the values. |
label |
str
|
An optional label (e.g. a time period) for plots and results. |
Source code in keyflux/ranking/rankedlist.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
aligned(other)
¶
Align two lists over their combined domain with tied-last exclusives.
Both lists are re-ranked over the union of their types. A type absent from a list is given count 0 there, so all of that list's exclusives tie at the bottom and receive the average of the unused tail ranks. This is the single place vocabulary union and tied-last ranking happen, so the divergence metric and the allotaxonograph always agree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
RankedList
|
The list to align against. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
|
list[float]
|
list's combined-domain rank for every type, index-aligned. |
Contract
typesis the sorted union of both lists' types.- A type present in only one list gets a tied-last (averaged) rank in the other.
- Present types keep the rank they have within their own list.
Examples:
>>> r1 = RankedList.from_counts({"a": 10, "b": 5})
>>> r2 = RankedList.from_counts({"b": 8, "c": 2})
>>> types, s, o = r1.aligned(r2)
>>> types
['a', 'b', 'c']
>>> s[types.index("c")] == o[types.index("a")] # both tied-last (3.0)
True
Source code in keyflux/ranking/rankedlist.py
from_counts(counts, *, label='')
classmethod
¶
Build a ranked list by ranking types on descending frequency.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
counts
|
Mapping[str, int]
|
Mapping of type to (positive) count. |
required |
label
|
str
|
Optional label for the list. |
''
|
Returns:
| Type | Description |
|---|---|
RankedList
|
A |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> r = RankedList.from_counts({"the": 100, "cat": 100, "sat": 5})
>>> r.ranks["sat"]
3.0
>>> r.ranks["the"]
1.5
Source code in keyflux/ranking/rankedlist.py
from_keyness(keyness, *, side='focus', label='')
classmethod
¶
Build a ranked list from one side of a :class:Keyness object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keyness
|
Keyness
|
A Keyness comparison. |
required |
side
|
str
|
|
'focus'
|
label
|
str
|
Optional label; defaults to the side name. |
''
|
Returns:
| Type | Description |
|---|---|
RankedList
|
A |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> from collections import Counter
>>> from keyflux.keyness.keyness import Keyness
>>> k = Keyness(Counter({"a": 30, "b": 10}), Counter({"a": 5, "b": 9}),
... min_focus_freq=1, min_reference_freq=1)
>>> RankedList.from_keyness(k).ranks["a"]
1.0
Source code in keyflux/ranking/rankedlist.py
from_scores(scores, *, label='')
classmethod
¶
Build a ranked list by ranking types on descending score.
Ranks by any numeric metric — keyness score, log ratio, salience — so a keyword ranking (or any other) can be compared like a frequency ranking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scores
|
Mapping[str, float]
|
Mapping of type to score. The highest score gets rank 1. Scores are treated as prominence: when two lists are aligned, a type absent from one gets score 0 there (tied-last), so non-negative scores keep that behaviour intuitive. |
required |
label
|
str
|
Optional label for the list. |
''
|
Returns:
| Type | Description |
|---|---|
RankedList
|
A |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> r = RankedList.from_scores({"climate": 6.4, "carbon": 5.8, "the": 0.1})
>>> r.ranks["climate"], r.ranks["the"]
(1.0, 3.0)
Source code in keyflux/ranking/rankedlist.py
rank_of(type_, *, last_rank=None)
¶
Return the rank of a type, or a fallback for an absent type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_
|
str
|
The type to look up. |
required |
last_rank
|
float | None
|
Rank to return when |
None
|
Returns:
| Type | Description |
|---|---|
float
|
The type's rank, or the fallback for an absent type. |
Examples:
>>> r = RankedList.from_counts({"a": 2, "b": 1})
>>> r.rank_of("a")
1.0
>>> r.rank_of("z", last_rank=99.0)
99.0
Source code in keyflux/ranking/rankedlist.py
types()
¶
Return the set of types in this list.
Examples:
keyflux.ranking.rankedlist.average_ranks(counts)
¶
Rank types by descending count, averaging ties (fractional ranking).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
counts
|
Mapping[str, float]
|
Mapping of type to count. The highest count gets rank 1. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
A dict mapping each type to its 1-based rank; tied counts share the |
dict[str, float]
|
average of the ranks they would occupy. |
Contract
- The highest count maps to the smallest rank.
- A group of
gtied types occupying ranksi..i+g-1each receive their average,i + (g - 1) / 2. - The ranks sum to
n (n + 1) / 2forntypes (rank-sum identity).
Examples: