Keyness¶
The focus-versus-reference comparison: keywords, lockwords, and the reproducibility record.
keyflux.keyness.keyness.Keyness
¶
Compare a focus corpus against a reference corpus to derive keyness.
Scores the combined vocabulary of two frequency Counters once, eagerly. The log-likelihood drives significance; the log ratio is the effect size used to rank keywords. A type enters the scored table when it meets the minimum frequency in at least one corpus, which keeps focus-exclusive keywords while discarding under-evidenced absent words.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
focus
|
Counter[str]
|
Frequency Counter for the focus corpus C (corpus of interest). |
required |
reference
|
Counter[str]
|
Frequency Counter for the reference corpus R. |
required |
measure
|
MeasureName
|
Keyness measure used for each row's |
'log_likelihood'
|
min_focus_freq
|
int
|
Minimum focus-corpus frequency to enter the table. |
5
|
min_reference_freq
|
int
|
Minimum reference-corpus frequency to enter the table. |
5
|
reference_id
|
str
|
A label for the reference corpus, stored in the repro record. |
'reference'
|
floor
|
float
|
Zero-cell floor for log ratio / %DIFF. |
ZERO_CELL_FLOOR
|
smp_k
|
float
|
Simple Maths constant |
SMP_DEFAULT_K
|
Contract
- A type is scored iff
focus_count >= min_focus_freqORreference_count >= min_reference_freq(evidence in either corpus). - Significance always comes from the log-likelihood, even when
measureis something else;effect_sizeis always the log ratio. - Swapping
focusandreference(with equal cutoffs) flips each row's direction and negates its effect size, turning positive keywords into negative ones and leaving lockwords unchanged.
Examples:
>>> from collections import Counter
>>> focus = Counter({"climate": 300, "the": 800, "policy": 90})
>>> reference = Counter({"climate": 30, "the": 780, "policy": 88})
>>> k = Keyness(focus, reference, measure="log_likelihood")
>>> kw = k.keywords(top=10)
>>> [r.type for r in kw.positive()]
['climate']
Source code in keyflux/keyness/keyness.py
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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | |
keywords(top=None)
¶
Return significance-filtered keywords, sorted by absolute effect size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
top
|
int | None
|
Maximum number of keywords (positive and negative combined) to keep; None keeps all significant keywords. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
KeywordTable
|
class: |
Contract
- Only rows significant at p<0.05 (log-likelihood) and with a non-neutral direction are included.
- Rows are ordered by absolute effect size, descending.
Examples:
>>> from collections import Counter
>>> focus = Counter({"climate": 30, "the": 80})
>>> reference = Counter({"climate": 2, "the": 78})
>>> k = Keyness(focus, reference)
>>> [r.type for r in k.keywords(top=5)]
['climate']
Source code in keyflux/keyness/keyness.py
lockwords(*, max_ll=measures.CHI2_CRITICAL['p05'], max_abs_log_ratio=0.5, min_freq_both=5)
¶
Return lockwords: stable types with comparable frequency in both corpora.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_ll
|
float
|
Lockwords must have a log-likelihood below this (not significant at p<0.05 by default). |
CHI2_CRITICAL['p05']
|
max_abs_log_ratio
|
float
|
Lockwords must have an absolute log ratio at or below this (relative frequencies near parity). |
0.5
|
min_freq_both
|
int
|
Lockwords must occur at least this many times in BOTH corpora (they are stable, frequent words — not rare noise). |
5
|
Returns:
| Type | Description |
|---|---|
list[KeynessRow]
|
Lockword rows, most frequent first. |
Contract
- Lockwords are disjoint from keywords: the log-likelihood ceiling is the same threshold that defines significance.
- Requires evidence in both corpora, so exclusives are never lockwords.
Examples:
>>> from collections import Counter
>>> focus = Counter({"the": 800, "climate": 30})
>>> reference = Counter({"the": 790, "climate": 2})
>>> k = Keyness(focus, reference)
>>> [r.type for r in k.lockwords()]
['the']
Source code in keyflux/keyness/keyness.py
table()
¶
Return the full per-type table, sorted by absolute effect size.
Returns:
| Type | Description |
|---|---|
list[KeynessRow]
|
Every scored row (one per type clearing the minimum frequency). |
Examples:
>>> from collections import Counter
>>> k = Keyness(Counter({"a": 30}), Counter({"a": 5, "b": 20}),
... min_focus_freq=1, min_reference_freq=1)
>>> len(k.table())
2
Source code in keyflux/keyness/keyness.py
keyflux.keyness.keyness.KeywordTable
dataclass
¶
A significance-filtered, effect-size-sorted set of keywords.
Attributes:
| Name | Type | Description |
|---|---|---|
rows |
tuple[KeynessRow, ...]
|
The keyword rows, sorted by absolute effect size (descending). |
repro |
ReproRecord
|
The reproducibility record for the run that produced them. |
Source code in keyflux/keyness/keyness.py
negative(n=None)
¶
Negative keywords (over-represented in the reference corpus).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int | None
|
Maximum number to return; None returns all. |
None
|
Returns:
| Type | Description |
|---|---|
list[KeynessRow]
|
Negative-direction rows sorted by effect size, most negative first. |
Examples:
>>> from collections import Counter
>>> k = Keyness(Counter({"a": 30, "x": 5}), Counter({"a": 2, "x": 6}))
>>> [r.type for r in k.keywords().negative()]
['x']
Source code in keyflux/keyness/keyness.py
positive(n=None)
¶
Positive keywords (over-represented in the focus corpus).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int | None
|
Maximum number to return; None returns all. |
None
|
Returns:
| Type | Description |
|---|---|
list[KeynessRow]
|
Positive-direction rows sorted by effect size, largest first. |
Examples:
>>> from collections import Counter
>>> k = Keyness(Counter({"a": 30, "x": 5}), Counter({"a": 2, "x": 6}))
>>> [r.type for r in k.keywords().positive()]
['a']
Source code in keyflux/keyness/keyness.py
keyflux.keyness.keyness.KeynessRow
dataclass
¶
One type's keyness statistics.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
str
|
The word type. |
focus_count |
int
|
Raw frequency in the focus corpus C. |
reference_count |
int
|
Raw frequency in the reference corpus R. |
focus_rf |
float
|
Relative frequency in C, per million tokens. |
reference_rf |
float
|
Relative frequency in R, per million tokens. |
score |
float
|
Value of the chosen keyness |
effect_size |
float
|
Log ratio (log2), always computed regardless of |
significance |
Significance
|
Band from the log-likelihood: ns / p05 / p01 / p001 / p0001. |
statistic |
float
|
The log-likelihood magnitude behind |
direction |
Direction
|
positive / negative / neutral, from the relative frequencies. |
Source code in keyflux/keyness/keyness.py
keyflux.keyness.keyness.ReproRecord
dataclass
¶
Reproducibility record for one keyness run.
Captures the three parameters that govern any keyness result — reference corpus, minimum-frequency cutoffs, and the statistical measure — plus the corpus totals and the keyflux version, so an analysis can be reproduced.
Attributes:
| Name | Type | Description |
|---|---|---|
reference_id |
str
|
A label identifying the reference corpus. |
measure |
MeasureName
|
The keyness measure used for |
min_focus_freq |
int
|
Minimum focus-corpus frequency to enter the table. |
min_reference_freq |
int
|
Minimum reference-corpus frequency to enter the table. |
focus_total |
int
|
Token total of the focus corpus. |
reference_total |
int
|
Token total of the reference corpus. |
top_n |
int | None
|
The |
floor |
float
|
Zero-cell floor used by log ratio / %DIFF. |
smp_k |
float
|
Simple Maths constant |
keyflux_version |
str
|
Version of keyflux that produced the result. |
Source code in keyflux/keyness/keyness.py
to_dict()
¶
Return a JSON-serialisable dict of the record.
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
A plain dict with one key per field. |
Examples:
>>> rec = ReproRecord("BE06", "log_likelihood", 5, 5, 1000, 1000,
... None, 0.5, 100.0, "0.1.0")
>>> rec.to_dict()["measure"]
'log_likelihood'