Skip to main content

Polars expression plugins for text analysis

Project description

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)

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.

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)

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.

For release and publishing procedures, see PUBLISH.md.

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.

Project details


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.4.0.tar.gz (120.5 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.4.0-cp314-cp314-win_amd64.whl (29.6 MB view details)

Uploaded CPython 3.14Windows x86-64

polars_text-0.4.0-cp314-cp314-manylinux_2_28_x86_64.whl (34.4 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.28+ x86-64

polars_text-0.4.0-cp314-cp314-macosx_11_0_arm64.whl (23.7 MB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: polars_text-0.4.0.tar.gz
  • Upload date:
  • Size: 120.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for polars_text-0.4.0.tar.gz
Algorithm Hash digest
SHA256 0827e8c755884cf0d00d33911217eea840600eec4ec669e13551ca8a7bdb5b5f
MD5 ed2d4dee7617651361f277e01a3b0442
BLAKE2b-256 3566481a69116704ac31fb369521596b2e39d879b69d83c6bb9872e75cfab8c7

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_text-0.4.0.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.4.0-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: polars_text-0.4.0-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 29.6 MB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for polars_text-0.4.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 df546cc14ba4e033a0f1ed545f56d8c896ec89874c97ab3a4a87f281a692082e
MD5 c353e6543e95b1ccc0197666f71e49bb
BLAKE2b-256 f188e7aa7409e47235ac5cfd45da3848201594f1a820c197bb2775e54e94e104

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_text-0.4.0-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.4.0-cp314-cp314-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for polars_text-0.4.0-cp314-cp314-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f4f9cd075989670cd001a54c9a7f225185b275d4189dc323772e64419cc0a55d
MD5 3e5fc2576de391b90b025ad4a1004be6
BLAKE2b-256 cf5f49e3001133ed245ddd73c6db27f10b6d653c363a9a326e4385fcf3cd16b8

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_text-0.4.0-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.4.0-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polars_text-0.4.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b007dafb082881f1b3e615b95771ff82edf9cb613e39d8ba839261c7aca4c070
MD5 e781e7e02fd037119d149103794ad9ab
BLAKE2b-256 698863884d25577f4e31d5e8c96c19f3d517d4e665b7933915358556b1bb4abd

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_text-0.4.0-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.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page