Skip to main content

logodds

Log-odds ratio with a Dirichlet prior, for finding the words that distinguish two corpora. Implementation of Monroe et al.'s "Fightin' Words" method, adapted from Jack Hessel's Python implementation.

This is a dependency-free implementation that works on plain Python dictionaries mapping words to counts. It does not use scikit-learn, and there are no vectorizers or arrays anywhere in the interface.

Install

pip install logodds

Usage

from logodds import compute_log_odds, top_words

corpus1 = {"cat": 30, "purr": 12, "dog": 5, "the": 200}
corpus2 = {"cat": 4, "dog": 28, "leash": 9, "the": 210}

scores = compute_log_odds(corpus1, corpus2)

top_words(scores, n=20)             # words characteristic of corpus1
top_words(scores, n=20, corpus=2)   # words characteristic of corpus2

compute_log_odds returns a dict mapping each word to a z-score. Positive means the word is characteristic of corpus1, negative means corpus2, and near zero means it does not distinguish them.

Informative prior

Pass counts from a larger background corpus:

scores = compute_log_odds(corpus1, corpus2, prior_counts=background)

Each word's prior is its background count plus alpha, so α₀ is simply the sum of those priors. Words that are common in the background get a large prior and are shrunk hard toward zero.

Optional arguments:

Argument Default
prior_counts None Background corpus counts. Switches on the informative prior.
alpha 0.01 Without prior_counts, the pseudo-count added to every word. With it, a floor so no word gets a zero prior.
min_count 0 Drop words whose combined count is below this.
vocabulary None Restrict to a given word list.

Corpus sizes are measured over the retained vocabulary, so filtering with min_count or vocabulary renormalizes the frequencies.

Intermediate values

log_odds_details takes the same arguments and returns count1, count2, freq1, freq2, prior, delta, variance and z for each word.

Method

For word w in corpus i, with count y_i^w, corpus size n_i, prior α_w and α_0 = Σ_w α_w:

δ_i^w = log( (y_i^w + α_w) / (n_i + α_0 - y_i^w - α_w) )

δ^w = δ_1^w - δ_2^w

σ²(δ^w) ≈ 1/(y_1^w + α_w) + 1/(y_2^w + α_w)

z = δ^w / sqrt(σ²(δ^w))

Monroe, B. L., Colaresi, M. P., & Quinn, K. M. (2008). Fightin' Words: Lexical Feature Selection and Evaluation for Identifying the Content of Political Conflict. Political Analysis, 16(4), 372–403. https://doi.org/10.1093/pan/mpn018

Tests

pip install -e . pytest
python -m pytest -q

License

MIT

Metadata

Release files for logodds 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for logodds 0.1.0
File Size Uploaded Provenance
logodds-0.1.0.tar.gz 7.2 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for logodds 0.1.0
File Interpreter ABI Platform
logodds-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.8 kB

Release files / logodds-0.1.0.tar.gz

Download URL logodds-0.1.0.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
7ee753b389589742064106398a8cdb782871f88ffc0fa79393a604d2901e5378
BLAKE2b-256 checksum
How to use checksums
b9b0940f17c923e1c1a05d7c278e0ce2a7a086835881f1a3fc74606e9d4b39a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

Release files / logodds-0.1.0-py3-none-any.whl

Download URL logodds-0.1.0-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ddd9ec10e3f1605b5858aead5875adab025f8798790950c8cd6399974d2817f3
BLAKE2b-256 checksum
How to use checksums
d7e6e4b80364a8cb9b5a608665e0bf3bbeb9737de052eb1733d52c433da1f668
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

Security for logodds 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for logodds-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page