Skip to main content

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_expr
  • extract_expr

tokenize_expr

Required kwargs:

  • model_path

Returns a struct column with:

  • raw_value
  • tokens
  • types
  • classes

extract_expr

Required kwargs:

  • model_path
  • pattern

Optional kwargs:

  • mode

Supported mode values:

  • whole (default)
  • start
  • end
  • any

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

tokmat_polars-0.3.1.tar.gz (61.9 kB view details)

Uploaded Source

Built Distributions

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

tokmat_polars-0.3.1-cp313-cp313-win_amd64.whl (4.5 MB view details)

Uploaded CPython 3.13Windows x86-64

tokmat_polars-0.3.1-cp313-cp313-manylinux_2_34_x86_64.whl (4.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

tokmat_polars-0.3.1-cp313-cp313-macosx_11_0_arm64.whl (3.7 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

tokmat_polars-0.3.1-cp312-cp312-win_amd64.whl (4.5 MB view details)

Uploaded CPython 3.12Windows x86-64

tokmat_polars-0.3.1-cp312-cp312-manylinux_2_34_x86_64.whl (4.2 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

tokmat_polars-0.3.1-cp312-cp312-macosx_11_0_arm64.whl (3.7 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

Details for the file tokmat_polars-0.3.1.tar.gz.

File metadata

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

File hashes

Hashes for tokmat_polars-0.3.1.tar.gz
Algorithm Hash digest
SHA256 436d295a4450d6d5837fa40abaeddb0e75d657457e24cd916009338d012a1b30
MD5 19f20ca3ebf5dcb9bd88cd66365665f4
BLAKE2b-256 462cf7f8b9dd869c133c5e189cd124362bc1613f88111a62c342b6227f286285

See more details on using hashes here.

Provenance

The following attestation bundles were made for tokmat_polars-0.3.1.tar.gz:

Publisher: release.yml on Jrakru/tokmat-polars

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

File details

Details for the file tokmat_polars-0.3.1-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for tokmat_polars-0.3.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 a7d4291743afd6be8d18fd59a2352d75ddeafd9b97f1d95204206f575a85b20c
MD5 3b38f17d9d1a6cb97d018d158dee2dad
BLAKE2b-256 b233e08bc9c83927338d5f80c878753d88aae6bbbf534b0acbcd875f66bb2988

See more details on using hashes here.

Provenance

The following attestation bundles were made for tokmat_polars-0.3.1-cp313-cp313-win_amd64.whl:

Publisher: release.yml on Jrakru/tokmat-polars

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

File details

Details for the file tokmat_polars-0.3.1-cp313-cp313-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for tokmat_polars-0.3.1-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 a55ba7d19057cd0604f1e04c0815444f4b692618e8becdb5137682d756a29e22
MD5 ef2d1d8cbe0820c9209f01d3cd9cca01
BLAKE2b-256 c5e1454ae574c4bce9191d58720497691e9f449527fe9b04659beb540e0de91f

See more details on using hashes here.

Provenance

The following attestation bundles were made for tokmat_polars-0.3.1-cp313-cp313-manylinux_2_34_x86_64.whl:

Publisher: release.yml on Jrakru/tokmat-polars

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

File details

Details for the file tokmat_polars-0.3.1-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tokmat_polars-0.3.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b992b4c63c71bfe6e366ae11934f1781bc1e9ace3f093c5de40232559c9f566d
MD5 7b10b9b5a7981d1566ac81d82c6ae3ea
BLAKE2b-256 4574d570ba152d390e62ecf7ad4b608294f9b7510a63d606ef5491cb9e5bff0e

See more details on using hashes here.

Provenance

The following attestation bundles were made for tokmat_polars-0.3.1-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: release.yml on Jrakru/tokmat-polars

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

File details

Details for the file tokmat_polars-0.3.1-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for tokmat_polars-0.3.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 938c048b9be4e1189666f58cdd852614a15e66961f8a84dd07239cb9e8867290
MD5 c6e4436256f24e7d457e2f52fa5739e2
BLAKE2b-256 d1ddd41d111014a205c1ddd7ade0d2896429b3dc2c85a6347d5c4fda827cafd3

See more details on using hashes here.

Provenance

The following attestation bundles were made for tokmat_polars-0.3.1-cp312-cp312-win_amd64.whl:

Publisher: release.yml on Jrakru/tokmat-polars

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

File details

Details for the file tokmat_polars-0.3.1-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for tokmat_polars-0.3.1-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 93bc73553e8d18d7b301dc86933e651197deebc8238f5ebb07cebdb16ec58ca6
MD5 c36cd0b7280ade16d386131f5cae24c7
BLAKE2b-256 b7b2dee01d41cba267a8cca9a00e621283ecf1021430e012ca80f2dcc09fdfca

See more details on using hashes here.

Provenance

The following attestation bundles were made for tokmat_polars-0.3.1-cp312-cp312-manylinux_2_34_x86_64.whl:

Publisher: release.yml on Jrakru/tokmat-polars

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

File details

Details for the file tokmat_polars-0.3.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tokmat_polars-0.3.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d2614ca06ba12f79dd211c61a2623b7faa77014889cfb4fa61aa2ec613939df9
MD5 a81a70c77c58ac5b51e7ebab5d330865
BLAKE2b-256 aa43706d1d632cb7cfa7285c0b811b570864fba7251c8f34ece49bebeb2e1316

See more details on using hashes here.

Provenance

The following attestation bundles were made for tokmat_polars-0.3.1-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: release.yml on Jrakru/tokmat-polars

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