Skip to main content

polars-text

Polars expression plugins for fast, practical text analysis. Use them as expressions or via the pl.col("text").text.* namespace, plus a few Series-based utilities for token frequency stats.

Quick start

import polars as pl
import polars_text

df = pl.DataFrame({
    "text": [
        "Alice said \"Hello world\".",
        "Hello again, world!",
    ]
})

out = df.with_columns([
    pl.col("text").text.clean_text().alias("clean"),
    pl.col("text").text.word_count().alias("word_count"),
    pl.col("text").text.char_count().alias("char_count"),
    pl.col("text").text.sentence_count().alias("sentence_count"),
    pl.col("text").text.tokenize(
        model="native:plain_words_en",
        lowercase=True,
        remove_punct=True,
    ).alias("tokens"),
])

Expressions and namespace

Tokenization is available through the text namespace on expressions.

Tokenization

  • pl.col("text").text.tokenize(model="native:plain_words_en", lowercase=True, remove_punct=True, cache=None)
  • pl.col("text").text.embedding(embedder_model=None, cache=None, batch_size=None)
  • clean_text(expr)
  • word_count(expr)
  • char_count(expr)
  • sentence_count(expr)
  • concordance(expr, search_word, num_left_tokens=5, num_right_tokens=5, regex=False, case_sensitive=False, remove_punct=False)

Namespace usage

df = pl.DataFrame({"text": ["Hello world, hello again."]})

out = df.select([
    pl.col("text").text.clean_text().alias("clean"),
    pl.col("text").text.word_count().alias("word_count"),
    pl.col("text").text.tokenize(model="native:plain_words_en").alias("tokens"),
])

tokenize returns a list of structs with token, start, and end character offsets. Pass an explicit native:, huggingface:, or lindera: model ID. Pass cache=Path("tokens.duckdb") to persist tokenization results in a DuckDB cache and reuse them by content hash; leave cache=None to compute directly through the Rust plugin.

Pass remove_punct=True to concordance to exclude punctuation and symbol-only tokens from context counts and L1/R1. The returned contexts retain the original punctuation and whitespace between lexical tokens and the match; literal and regular-expression matching are unchanged.

Embeddings

embedding accepts a string expression or a list-of-string expression. String input returns List(Float32) per row; list input returns nested List(List(Float32)) per row.

df = pl.DataFrame({"text": ["A short document."], "chunks": [["first", "second"]]})

out = df.select([
    pl.col("text").text.embedding(cache="embeddings.duckdb").alias("embedding"),
    pl.col("chunks").text.embedding(cache="embeddings.duckdb").alias("chunk_embeddings"),
])

The Rust plugin downloads and loads Hugging Face ONNX sentence-transformer repositories automatically through hf-hub. Repositories without ONNX files are not supported. Passing cache=Path("embeddings.duckdb") persists vectors in a separate DuckDB cache keyed by model, revision, execution-provider label, and text hash.

Concordance

Get left/right context windows around a search term. Output is a list of structs that you can explode and unnest for tabular use.

df = pl.DataFrame({"text": ["Hello world, hello again."]})

concordance = (
    pl.col("text")
    .text.concordance("hello", num_left_tokens=1, num_right_tokens=1)
    .list.explode()
    .struct.unnest()
)

out = df.select(concordance)

Topic modelling

topic_modeling consumes a complete document column and returns one scalar run result. The result keeps document outcomes separate from complete topic metadata:

{
  documents: [{doc_index, dominant_topic, topic_distribution}],
  topics: [{id, representative_words, x, y}],
  n_chunks,
  truncated_segment_count,
  stage_timings_ms
}

Automatic, Paragraph, and Sentence modes differ only when constructing Topic Segments. All modes then share embedding, clustering, c-TF-IDF, and document rollup. Clustering treats every segment as one observation. Rollup weights each segment by the Unicode-character length of its retained text; Automatic overlap counts repeated text again.

Token frequencies and stats

Compute corpus token counts and compare corpora with standard statistics.

series_0 = pl.Series("text", ["hello world", "hello again"])
series_1 = pl.Series("text", ["goodbye world"])

freqs_0 = pt.token_frequencies(series_0, model="native:plain_words_en")
freqs_1 = pt.token_frequencies(series_1, model="native:plain_words_en")

stats = pt.token_frequency_stats(freqs_0, freqs_1)

Output schemas

Tokenization (list of structs):

  • token
  • start
  • end

Concordance (list of structs):

  • left_context, matched_text, right_context
  • start_idx, end_idx
  • l1, r1 (first token on left/right for quick filtering)

Models and downloads

Some features download tokenizer assets on first use and run on CPU:

  • Hugging Face tokenizers: for example huggingface:bert-base-uncased (tokenizer.json via hf-hub)
  • Lindera dictionaries: lindera:cc-cedict, lindera:jieba, lindera:ja-ipadic, lindera:ja-ipadic-neologd, lindera:ja-unidic, and lindera:ko-dic from official Lindera release zips

The initial call may take longer while models download and cache.

Embedding features download ONNX artifacts on first use. Some ONNX repositories store tensor data in sidecar files such as onnx/model.onnx_data; those files are fetched automatically when present. ONNX Runtime uses DirectML on Windows when available, XNNPACK acceleration on supported CPU platforms, and CPU fallback.

Development

Build the extension locally with maturin and then import as polars_text. See the repository-level development and release runbooks for complete procedures.

make build
make test

For faster Rust iteration, use feature-scoped targets such as make check-tokenization, make build-tokenization, or make build-topic. Leave JOBS unset for Cargo's default parallelism, or pass JOBS=<n> to cap it.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

polars_text-0.5.1.tar.gz (119.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

polars_text-0.5.1-cp314-cp314-win_amd64.whl (37.0 MB view details)

Uploaded CPython 3.14Windows x86-64

polars_text-0.5.1-cp314-cp314-manylinux_2_28_x86_64.whl (35.2 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.28+ x86-64

polars_text-0.5.1-cp314-cp314-macosx_11_0_arm64.whl (24.2 MB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

File details

Details for the file polars_text-0.5.1.tar.gz.

File metadata

  • Download URL: polars_text-0.5.1.tar.gz
  • Upload date:
  • Size: 119.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for polars_text-0.5.1.tar.gz
Algorithm Hash digest
SHA256 6807e9fd6a2422c6e1e4c9d0d7cffab1f0d0c39c3e4f9a4c5350505c62b24b93
MD5 f2b58640753d140ac7df1d21e6fc931c
BLAKE2b-256 5db79e389df79ac5e63d6198b5ab620c496f312bdfdf382b9f264a7b6fc369ad

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_text-0.5.1.tar.gz:

Publisher: release.yml on Australian-Text-Analytics-Platform/polars-text

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_text-0.5.1-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: polars_text-0.5.1-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 37.0 MB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for polars_text-0.5.1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 4278f13a996444333c7cd423a76ce9a7466958e6c6b7ebed36a4a3e27a3e5d02
MD5 1d91d15da5398d6860a68abc77ed49b6
BLAKE2b-256 3645533bed340ab879d83fa580eaf9d2199ceb70e5bb2984e1f269250aa1701b

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_text-0.5.1-cp314-cp314-win_amd64.whl:

Publisher: release.yml on Australian-Text-Analytics-Platform/polars-text

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_text-0.5.1-cp314-cp314-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for polars_text-0.5.1-cp314-cp314-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0cd85d807be79015f7bce547c975a7e373f329a03f58d3fad992c3e780ca9d5d
MD5 f9ea2cb3d5960bee8baa071ef09e1151
BLAKE2b-256 07badb22a4d75e7c6530a0bdb3a5f22aa40d259d6146a714b4ea84b4215da066

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_text-0.5.1-cp314-cp314-manylinux_2_28_x86_64.whl:

Publisher: release.yml on Australian-Text-Analytics-Platform/polars-text

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_text-0.5.1-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polars_text-0.5.1-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9690483db4e5971cea56c8724b691b58450ed16f0bae18d961267ebdde1b29f7
MD5 3089c07beea5c22479bb61eff2e0a157
BLAKE2b-256 5e56d7204e69d352ed1c46cc2e700cdc898c0c8aa3f1b848656ee515bd0a9b3f

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_text-0.5.1-cp314-cp314-macosx_11_0_arm64.whl:

Publisher: release.yml on Australian-Text-Analytics-Platform/polars-text

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.6.0

4 files

This release

0.5.1 This release

4 files

0.5.0

4 files

0.4.0

4 files

0.3.0

4 files

0.2.2

4 files

0.2.1

4 files

0.2.0

4 files

0.1.7

4 files

0.1.6

4 files

0.1.5

4 files

0.1.4

4 files

0.1.3

4 files

0.1.2

4 files

0.1.1

4 files

0.1.0

4 files

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