Polars expression plugin and Python extension for tokmat
Project description
tokmat-polars
Standalone Polars integration crate for tokmat.
This crate depends on the published tokmat package from crates.io and is
intended to provide the dataframe-facing plugin layer around the core parser.
Rust usage
tokmat-polars can also be used directly from Rust as a normal library crate.
That path is useful when you want to build Series values in Rust and reuse the
same tokenization and extraction logic without going through Python.
use polars::prelude::*;
use tokmat_polars::TokmatPolars;
let plugin = TokmatPolars::from_model_path("tests/fixtures/model_1")?;
let input = Series::new("address".into(), ["123 MAIN ST"]);
let tokenized = plugin.tokenize_series(&input)?;
let extracted = plugin.extract_series(
&tokenized,
"<<CIVIC#>> <<STREET@+>> <<TYPE::STREETTYPE>>",
)?;
# let _ = extracted;
# Ok::<(), PolarsError>(())
Python packaging
tokmat-polars can also be built and published as a Python package via
maturin. The Rust crate exposes a PyO3 extension module named
tokmat_polars, and Polars can load the compiled plugin functions from that
module path. Python support starts at 3.12.
Typical local workflow:
python -m venv .venv
. .venv/bin/activate
pip install -U pip maturin pytest polars
maturin develop
pytest -q
Release workflow
PyPI releases are published from GitHub Actions using Trusted Publishing. The
release workflow builds wheels and an sdist on tag pushes that match v*, then
uploads them through pypa/gh-action-pypi-publish. The same tag also publishes
the Rust crate to crates.io using a CARGO_REGISTRY_TOKEN GitHub secret.
Release steps:
git tag v0.3.0
git push origin v0.3.0
Before the first release, configure this repository as a Trusted Publisher in
the PyPI project settings and set the workflow environment to pypi if PyPI
prompts for it. For crates.io, create an API token and store it in GitHub as
CARGO_REGISTRY_TOKEN.
Python usage
Use the wrapper functions — tokenize, extract, encode_class_ids. They
register the plugin expressions with is_elementwise=True, so the Polars engine
can stream them and parallelize across chunks on its own thread pool:
import polars as pl
import tokmat_polars as tk
MODEL = "/path/to/model"
PATTERN = "<<CIVIC#>> <<STREET@+>> <<TYPE::STREETTYPE>>"
lf = pl.LazyFrame({"address": ["ATTN 123 MAIN ST"]})
# tokenize -> struct(raw_value, tokens, types, classes); unnest for flat columns
tok = lf.select(tk.tokenize(pl.col("address"), model_path=MODEL).alias("t")).unnest("t")
# extract -> struct(capture fields..., complement)
parsed = lf.select(
tk.extract(pl.col("address"), model_path=MODEL, pattern=PATTERN, mode="any").alias("p")
).unnest("p")
Use the streaming engine for large data
Because the expressions are elementwise, the streaming engine parallelizes them
and keeps memory bounded. On a 1M-row benchmark (benchmarks/bench_polars_engine.py),
extraction is ~7× faster under streaming and uses near-zero extra memory:
parsed.collect(engine="streaming") # parallel + bounded memory
| 1M rows | in-memory | streaming |
|---|---|---|
tokenize |
4.7 s, +298 MB | 4.3 s, +43 MB |
extract |
35.6 s | 4.9 s, +0 MB |
is_elementwise=False (raw register_plugin_function without the flag) forfeits
this — streaming can't engage and extraction stays at ~36 s. The wrapper sets the
flag for you; prefer it over registering the plugin by hand.
The word definition is also exposed: tk.model_word_definition(path),
tk.get_word_definition(), tk.set_word_definition(chars).
Plugin API
tokmat-polars exposes two Polars plugin functions:
tokenize_exprextract_expr
tokenize_expr
Required kwargs:
model_path
Returns a struct column with:
raw_valuetokenstypesclasses
extract_expr
Required kwargs:
model_pathpattern
Optional kwargs:
mode
Supported mode values:
whole(default)startendany
When extract_expr receives a tokenized struct column, it uses the embedded
raw_value field when computing complements. This preserves any-mode
behavior and keeps complement output aligned with the original text rather than
with a placeholder reconstruction.
Example:
parsed = pl.DataFrame({"address": ["ATTN 123 MAIN ST"]}).select(
pl.plugins.register_plugin_function(
plugin_path=plugin_path,
function_name="extract_expr",
args=pl.col("address"),
kwargs={
"model_path": "/path/to/model",
"pattern": "<<CIVIC#>> <<STREET@+>> <<TYPE::STREETTYPE>>",
"mode": "any",
},
use_abs_path=True,
).alias("parsed")
).unnest("parsed")
This returns capture fields plus a complement column. In the example above,
the complement contains "ATTN " because the TEL pattern matches only the
embedded address portion.
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
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 tokmat_polars-0.3.2.tar.gz.
File metadata
- Download URL: tokmat_polars-0.3.2.tar.gz
- Upload date:
- Size: 63.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e0422b12d94233445ccc2071084284b41e816b1f2d784e678f6f901ae051b7f
|
|
| MD5 |
9fbd85f3b1f949ffa684aadebe94e3ef
|
|
| BLAKE2b-256 |
25b19efb0efce2643d11a92c60f2c569c8093ec472a4fc45cbfdccdc9cfb9c1c
|
Provenance
The following attestation bundles were made for tokmat_polars-0.3.2.tar.gz:
Publisher:
release.yml on Jrakru/tokmat-polars
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tokmat_polars-0.3.2.tar.gz -
Subject digest:
7e0422b12d94233445ccc2071084284b41e816b1f2d784e678f6f901ae051b7f - Sigstore transparency entry: 1798098295
- Sigstore integration time:
-
Permalink:
Jrakru/tokmat-polars@79b05dcde973985205e710155989c925c7c52afb -
Branch / Tag:
refs/tags/v0.3.2 - Owner: https://github.com/Jrakru
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@79b05dcde973985205e710155989c925c7c52afb -
Trigger Event:
push
-
Statement type:
File details
Details for the file tokmat_polars-0.3.2-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: tokmat_polars-0.3.2-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 4.5 MB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
47c3d8922aad1212ba1197fff696085badff5ef2309971057187ec7436c2abc2
|
|
| MD5 |
85aa48ebc9b1d557aa49a51cf93033af
|
|
| BLAKE2b-256 |
e2a0950fae4b0b9beeaef262becd67c142effc100cdfad29e12d6e318551b770
|
Provenance
The following attestation bundles were made for tokmat_polars-0.3.2-cp313-cp313-win_amd64.whl:
Publisher:
release.yml on Jrakru/tokmat-polars
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tokmat_polars-0.3.2-cp313-cp313-win_amd64.whl -
Subject digest:
47c3d8922aad1212ba1197fff696085badff5ef2309971057187ec7436c2abc2 - Sigstore transparency entry: 1798100432
- Sigstore integration time:
-
Permalink:
Jrakru/tokmat-polars@79b05dcde973985205e710155989c925c7c52afb -
Branch / Tag:
refs/tags/v0.3.2 - Owner: https://github.com/Jrakru
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@79b05dcde973985205e710155989c925c7c52afb -
Trigger Event:
push
-
Statement type:
File details
Details for the file tokmat_polars-0.3.2-cp313-cp313-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: tokmat_polars-0.3.2-cp313-cp313-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 4.2 MB
- Tags: CPython 3.13, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e5f4dc8c437ad028d7c41cbd5b63011d5983e9637d2739bf214814a5102be072
|
|
| MD5 |
445cbeb60c8912fe233385e2416430bb
|
|
| BLAKE2b-256 |
6ed487c8035ddede5ca2d59ca00b1a218af6d8e46d7adb3186485d5b725ab413
|
Provenance
The following attestation bundles were made for tokmat_polars-0.3.2-cp313-cp313-manylinux_2_34_x86_64.whl:
Publisher:
release.yml on Jrakru/tokmat-polars
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tokmat_polars-0.3.2-cp313-cp313-manylinux_2_34_x86_64.whl -
Subject digest:
e5f4dc8c437ad028d7c41cbd5b63011d5983e9637d2739bf214814a5102be072 - Sigstore transparency entry: 1798098891
- Sigstore integration time:
-
Permalink:
Jrakru/tokmat-polars@79b05dcde973985205e710155989c925c7c52afb -
Branch / Tag:
refs/tags/v0.3.2 - Owner: https://github.com/Jrakru
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@79b05dcde973985205e710155989c925c7c52afb -
Trigger Event:
push
-
Statement type:
File details
Details for the file tokmat_polars-0.3.2-cp313-cp313-macosx_11_0_arm64.whl.
File metadata
- Download URL: tokmat_polars-0.3.2-cp313-cp313-macosx_11_0_arm64.whl
- Upload date:
- Size: 3.7 MB
- Tags: CPython 3.13, 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 |
d81a82effce30cc27a60b186ee615dc2f555e659b440cb11f569e17c3e5be812
|
|
| MD5 |
a50a758363c3491132595b4a7c56763d
|
|
| BLAKE2b-256 |
32e799bc73b6e61d9f48b9995d6af48b594128e01f9f0ea6a26c4dd7fa764559
|
Provenance
The following attestation bundles were made for tokmat_polars-0.3.2-cp313-cp313-macosx_11_0_arm64.whl:
Publisher:
release.yml on Jrakru/tokmat-polars
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tokmat_polars-0.3.2-cp313-cp313-macosx_11_0_arm64.whl -
Subject digest:
d81a82effce30cc27a60b186ee615dc2f555e659b440cb11f569e17c3e5be812 - Sigstore transparency entry: 1798100091
- Sigstore integration time:
-
Permalink:
Jrakru/tokmat-polars@79b05dcde973985205e710155989c925c7c52afb -
Branch / Tag:
refs/tags/v0.3.2 - Owner: https://github.com/Jrakru
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@79b05dcde973985205e710155989c925c7c52afb -
Trigger Event:
push
-
Statement type:
File details
Details for the file tokmat_polars-0.3.2-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: tokmat_polars-0.3.2-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 4.5 MB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
355f18117e5943a1f91267ec7735c76b754d438f5c8ef9f1e30d8fdc0ad0deba
|
|
| MD5 |
0ab6e891fc4fe832eafa1a75dc412334
|
|
| BLAKE2b-256 |
350732eea876ebef909bf4e886e20c750a6d4fd4abd26bdd678a11e969e47549
|
Provenance
The following attestation bundles were made for tokmat_polars-0.3.2-cp312-cp312-win_amd64.whl:
Publisher:
release.yml on Jrakru/tokmat-polars
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tokmat_polars-0.3.2-cp312-cp312-win_amd64.whl -
Subject digest:
355f18117e5943a1f91267ec7735c76b754d438f5c8ef9f1e30d8fdc0ad0deba - Sigstore transparency entry: 1798099837
- Sigstore integration time:
-
Permalink:
Jrakru/tokmat-polars@79b05dcde973985205e710155989c925c7c52afb -
Branch / Tag:
refs/tags/v0.3.2 - Owner: https://github.com/Jrakru
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@79b05dcde973985205e710155989c925c7c52afb -
Trigger Event:
push
-
Statement type:
File details
Details for the file tokmat_polars-0.3.2-cp312-cp312-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: tokmat_polars-0.3.2-cp312-cp312-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 4.2 MB
- Tags: CPython 3.12, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
615af9cc24df5fd1299cc1f4f706fe373261dd70767b5219176254c537caf855
|
|
| MD5 |
24d54914eb6db3a930e999abb22327cf
|
|
| BLAKE2b-256 |
3e8cfcbc1b922ac37268b3311281069135d26ca7c792b52034f956fe17d0fff7
|
Provenance
The following attestation bundles were made for tokmat_polars-0.3.2-cp312-cp312-manylinux_2_34_x86_64.whl:
Publisher:
release.yml on Jrakru/tokmat-polars
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tokmat_polars-0.3.2-cp312-cp312-manylinux_2_34_x86_64.whl -
Subject digest:
615af9cc24df5fd1299cc1f4f706fe373261dd70767b5219176254c537caf855 - Sigstore transparency entry: 1798099435
- Sigstore integration time:
-
Permalink:
Jrakru/tokmat-polars@79b05dcde973985205e710155989c925c7c52afb -
Branch / Tag:
refs/tags/v0.3.2 - Owner: https://github.com/Jrakru
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@79b05dcde973985205e710155989c925c7c52afb -
Trigger Event:
push
-
Statement type:
File details
Details for the file tokmat_polars-0.3.2-cp312-cp312-macosx_11_0_arm64.whl.
File metadata
- Download URL: tokmat_polars-0.3.2-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 3.7 MB
- Tags: CPython 3.12, 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 |
6fb9f38c05ca1d51947104af0d535a2e369d2a8ed10948e35cca452fddcf8837
|
|
| MD5 |
95855be5b9ae99a6b7b870ea2c489aa1
|
|
| BLAKE2b-256 |
b85a876d60d2c185d78c7ee954fd20fa56cd4dbdb29faf40eb1c02486da30f41
|
Provenance
The following attestation bundles were made for tokmat_polars-0.3.2-cp312-cp312-macosx_11_0_arm64.whl:
Publisher:
release.yml on Jrakru/tokmat-polars
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tokmat_polars-0.3.2-cp312-cp312-macosx_11_0_arm64.whl -
Subject digest:
6fb9f38c05ca1d51947104af0d535a2e369d2a8ed10948e35cca452fddcf8837 - Sigstore transparency entry: 1798098559
- Sigstore integration time:
-
Permalink:
Jrakru/tokmat-polars@79b05dcde973985205e710155989c925c7c52afb -
Branch / Tag:
refs/tags/v0.3.2 - Owner: https://github.com/Jrakru
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@79b05dcde973985205e710155989c925c7c52afb -
Trigger Event:
push
-
Statement type: