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):
tokenstartend
Concordance (list of structs):
left_context,matched_text,right_contextstart_idx,end_idxl1,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.jsonviahf-hub) - Lindera dictionaries:
lindera:cc-cedict,lindera:jieba,lindera:ja-ipadic,lindera:ja-ipadic-neologd,lindera:ja-unidic, andlindera:ko-dicfrom 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0827e8c755884cf0d00d33911217eea840600eec4ec669e13551ca8a7bdb5b5f
|
|
| MD5 |
ed2d4dee7617651361f277e01a3b0442
|
|
| BLAKE2b-256 |
3566481a69116704ac31fb369521596b2e39d879b69d83c6bb9872e75cfab8c7
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
polars_text-0.4.0.tar.gz -
Subject digest:
0827e8c755884cf0d00d33911217eea840600eec4ec669e13551ca8a7bdb5b5f - Sigstore transparency entry: 1952776299
- Sigstore integration time:
-
Permalink:
Australian-Text-Analytics-Platform/polars-text@f3ef03d4ed4dc90e5e9995600c98f7c8924ebd17 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/Australian-Text-Analytics-Platform
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@f3ef03d4ed4dc90e5e9995600c98f7c8924ebd17 -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df546cc14ba4e033a0f1ed545f56d8c896ec89874c97ab3a4a87f281a692082e
|
|
| MD5 |
c353e6543e95b1ccc0197666f71e49bb
|
|
| BLAKE2b-256 |
f188e7aa7409e47235ac5cfd45da3848201594f1a820c197bb2775e54e94e104
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
polars_text-0.4.0-cp314-cp314-win_amd64.whl -
Subject digest:
df546cc14ba4e033a0f1ed545f56d8c896ec89874c97ab3a4a87f281a692082e - Sigstore transparency entry: 1952776682
- Sigstore integration time:
-
Permalink:
Australian-Text-Analytics-Platform/polars-text@f3ef03d4ed4dc90e5e9995600c98f7c8924ebd17 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/Australian-Text-Analytics-Platform
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@f3ef03d4ed4dc90e5e9995600c98f7c8924ebd17 -
Trigger Event:
push
-
Statement type:
File details
Details for the file polars_text-0.4.0-cp314-cp314-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: polars_text-0.4.0-cp314-cp314-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 34.4 MB
- Tags: CPython 3.14, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f4f9cd075989670cd001a54c9a7f225185b275d4189dc323772e64419cc0a55d
|
|
| MD5 |
3e5fc2576de391b90b025ad4a1004be6
|
|
| BLAKE2b-256 |
cf5f49e3001133ed245ddd73c6db27f10b6d653c363a9a326e4385fcf3cd16b8
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
polars_text-0.4.0-cp314-cp314-manylinux_2_28_x86_64.whl -
Subject digest:
f4f9cd075989670cd001a54c9a7f225185b275d4189dc323772e64419cc0a55d - Sigstore transparency entry: 1952776845
- Sigstore integration time:
-
Permalink:
Australian-Text-Analytics-Platform/polars-text@f3ef03d4ed4dc90e5e9995600c98f7c8924ebd17 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/Australian-Text-Analytics-Platform
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@f3ef03d4ed4dc90e5e9995600c98f7c8924ebd17 -
Trigger Event:
push
-
Statement type:
File details
Details for the file polars_text-0.4.0-cp314-cp314-macosx_11_0_arm64.whl.
File metadata
- Download URL: polars_text-0.4.0-cp314-cp314-macosx_11_0_arm64.whl
- Upload date:
- Size: 23.7 MB
- Tags: CPython 3.14, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b007dafb082881f1b3e615b95771ff82edf9cb613e39d8ba839261c7aca4c070
|
|
| MD5 |
e781e7e02fd037119d149103794ad9ab
|
|
| BLAKE2b-256 |
698863884d25577f4e31d5e8c96c19f3d517d4e665b7933915358556b1bb4abd
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
polars_text-0.4.0-cp314-cp314-macosx_11_0_arm64.whl -
Subject digest:
b007dafb082881f1b3e615b95771ff82edf9cb613e39d8ba839261c7aca4c070 - Sigstore transparency entry: 1952776480
- Sigstore integration time:
-
Permalink:
Australian-Text-Analytics-Platform/polars-text@f3ef03d4ed4dc90e5e9995600c98f7c8924ebd17 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/Australian-Text-Analytics-Platform
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@f3ef03d4ed4dc90e5e9995600c98f7c8924ebd17 -
Trigger Event:
push
-
Statement type: