Metadata-Version: 2.4
Name: polars-corpus
Version: 0.2.1
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Rust
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Programming Language :: Python :: Free Threading :: 2 - Beta
Classifier: Topic :: Scientific/Engineering :: Information Analysis
Classifier: Topic :: Text Processing
Classifier: Topic :: Text Processing :: General
Classifier: Topic :: Text Processing :: Linguistic
Classifier: Typing :: Typed
Requires-Dist: polars>=1.36
Requires-Dist: lark>=1.2
Requires-Dist: nltk>=3.9
Requires-Dist: anywidget>=0.9
Requires-Dist: sentence-transformers ; extra == 'embeddings'
Requires-Dist: model2vec ; extra == 'embeddings'
Requires-Dist: umap-learn ; extra == 'embeddings'
Requires-Dist: hdbscan ; extra == 'embeddings'
Requires-Dist: great-tables ; extra == 'examples'
Requires-Dist: scipy ; extra == 'examples'
Requires-Dist: matplotlib ; extra == 'examples'
Requires-Dist: seaborn ; extra == 'examples'
Requires-Dist: lxml ; extra == 'examples'
Requires-Dist: pyarrow>=14 ; extra == 'examples'
Requires-Dist: statsmodels ; extra == 'examples'
Requires-Dist: adjusttext ; extra == 'examples'
Provides-Extra: embeddings
Provides-Extra: examples
License-File: LICENSE.txt
Summary: Polars extensions for corpus linguistics
Author-email: Rob Malouf <rmalouf@sdsu.edu>
License-Expression: MIT
Requires-Python: >=3.11
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Documentation, https://rmalouf.github.io/polars_corpus/
Project-URL: Homepage, https://github.com/rmalouf/polars_corpus
Project-URL: Issues, https://github.com/rmalouf/polars_corpus/issues
Project-URL: Repository, https://github.com/rmalouf/polars_corpus

# polars-corpus

Corpus linguistics functions for Polars: search, concordances, frequency
lists, collocations, keywords, dispersion, and lexical diversity. The query
matcher is written in Rust; the rest is Python over Polars expressions.

A corpus is a DataFrame (or LazyFrame) with one row per token. The default
column names are `token`, `pos`, `lemma` and `file_id`; every function that
reads one of these takes a `*_column` or `expr` argument to point it elsewhere.
A LazyFrame is searched a chunk of files at a time, so the corpus does not have
to fit in memory.

The package is at version 0.2.0 and the API may still change.

Documentation: <https://rmalouf.github.io/polars_corpus/>

## Installation

```bash
pip install polars-corpus
```

The plotting functions need matplotlib, which is in the `examples` extra:

```bash
pip install "polars-corpus[examples]"
```

Requires Python 3.11 or later and Polars 1.36 or later, including 2.0.

## Usage

```python
import nltk
import polars as pl
import polars_corpus as plc

nltk.download("brown")
corpus = plc.from_nltk(nltk.corpus.brown)  # token, pos, sentence_tag, file_id, category

# Simple (BNCweb-style) query: "small" followed by a noun
results = plc.search(corpus, "small _{SUBST}")
results.concordance("token", window=5)

# CQP query
plc.search_cqp(corpus, '[pos="JJ"] [token="(?i)house"]').concordance(window=2)

word = pl.col("token").str.to_lowercase()

plc.frequency_list(corpus, word)
plc.collocations(plc.search(corpus, "house"), word, "logdice")
plc.keywords(
    corpus.filter(pl.col("category") == "news"),
    corpus.filter(pl.col("category") != "news"),
    word,
    "ll",
)
plc.dispersion(corpus, word, ["range", "dp"])
```

The same functions are available through a `.corpus` namespace on DataFrames,
LazyFrames and expressions:

```python
corpus.corpus.search("america").concordance("token", window=2)
```

## Contents

- **Search**: `search` (Simple query language) and `search_cqp` (CQP), with
  variable bindings in both. Results give KWIC concordances, hit distributions
  across a metadata column, and an interactive concordance viewer for Jupyter.
- **Frequency**: `frequency_list`, `ngrams`, `crosstab`.
- **Association measures**: PMI, MI3, log-Dice, t-score, z-score,
  log-likelihood, chi-squared, minimum sensitivity, simple maths, BIC, log
  ratio, %DIFF, odds ratio, Welch's t-test. Used by `collocations` and
  `keywords`.
- **Dispersion**: range, SD, CV, Juilland's D, Gries's DP, Burch's DA.
- **Lexical diversity**: TTR, MSTTR, MTLD, Yule's K, vocabulary growth,
  frequency spectrum.
- **Input**: plain text and WLP files (gzip or zstd compressed), NLTK corpus
  readers, and the BNC XML edition.
- **Plots**: `barcode_plot`, `dispersion_plot`, `keyword_plot`.

Not yet implemented: proximity operators (`<<3>>`, `<<s>>`) in the Simple query
language.

## Query languages

- Simple: `small _{SUBST}`, `( small | little ) _N*`. See
  [docs/simple_query.md](docs/simple_query.md).
- CQP: `[pos="JJ"] [lemma="house"]`. See [docs/cqp_query.md](docs/cqp_query.md).

## Examples

Worked notebooks are in [docs/notebooks/](docs/notebooks/) and rendered on the
documentation site: frequencies, concordances, collocations, keywords,
dispersion, and vocabulary.

## Development

Building from source needs a Rust toolchain and maturin:

```bash
make develop   # build the extension into the active environment
pytest
```

## License

MIT

