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.1.0
git push origin v0.1.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.

Minimal Python usage:

from pathlib import Path

import polars as pl
import tokmat_polars

plugin_path = Path(tokmat_polars.__file__).parent

expr = pl.plugins.register_plugin_function(
    plugin_path=plugin_path,
    function_name="tokenize_expr",
    args=pl.col("address"),
    kwargs={"model_path": "/path/to/model"},
    use_abs_path=True,
)

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.1.1.tar.gz (30.3 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.1.1-cp313-cp313-win_amd64.whl (4.5 MB view details)

Uploaded CPython 3.13Windows x86-64

tokmat_polars-0.1.1-cp313-cp313-manylinux_2_34_x86_64.whl (5.1 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

tokmat_polars-0.1.1-cp313-cp313-macosx_11_0_arm64.whl (4.2 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

tokmat_polars-0.1.1-cp312-cp312-manylinux_2_34_x86_64.whl (5.1 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

tokmat_polars-0.1.1-cp312-cp312-macosx_11_0_arm64.whl (4.2 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for tokmat_polars-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b49bf4de281d9131a85cc2bf491d4a04221e4b9655a97a2326a15ed256ff8674
MD5 2b01c40460a32edc9c94821e2565089f
BLAKE2b-256 6741f10a30ae5e4c6b137251544f33bfa3157cc1b1b052914e171bd7f226a77c

See more details on using hashes here.

Provenance

The following attestation bundles were made for tokmat_polars-0.1.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.1.1-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for tokmat_polars-0.1.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 33755618ea4e236294a4851126885f27d1f716b4ae3bae351bf6f82bdfef4ca1
MD5 f47f4f2cb37d3bdfe3f65624433e226b
BLAKE2b-256 27cbdbce17c739b8831f7687bd700f4ebf15ad1da8070ba097b0aa0ccb39a26c

See more details on using hashes here.

Provenance

The following attestation bundles were made for tokmat_polars-0.1.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.1.1-cp313-cp313-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for tokmat_polars-0.1.1-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 25da389b5e78f4aab573963edc8a93e5f83fe555fb144b1f0b58dc620bafbf52
MD5 cc105b9e5a6d514862123bcbf80538f1
BLAKE2b-256 957ed0dbc78f6140fae125f2d163bc6515125ad3e362201597ed04545e16c9dd

See more details on using hashes here.

Provenance

The following attestation bundles were made for tokmat_polars-0.1.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.1.1-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tokmat_polars-0.1.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4d0933394429eeecd048dce38ec325518b95d2e0f2d693728de24853b367a221
MD5 641698a4a0c5a8ae1db53d88ffd180cd
BLAKE2b-256 7197bf2d7818dec46a04f602df6a3299db0686c1bef576a1f8bec58e748639b8

See more details on using hashes here.

Provenance

The following attestation bundles were made for tokmat_polars-0.1.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.1.1-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for tokmat_polars-0.1.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 320a6cc70dae128ad2d3cdfaec9ddc58062292d3b3f8e31a737d9001541cb953
MD5 4664528ab9abb1ad7897f29155b0e7df
BLAKE2b-256 9424137d2da75c23b4ddee71f00e153025a9096f0d755b23b8a737a626c4676f

See more details on using hashes here.

Provenance

The following attestation bundles were made for tokmat_polars-0.1.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.1.1-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for tokmat_polars-0.1.1-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 2a812031c2b36513ff8df55eab99c26905dea4637373e9aee6161fb3109e6ebb
MD5 a72fa745dacda373b8ad1daf063020d1
BLAKE2b-256 5f1a45a209dfb14a28b46d23820fcaf415508f7a37e43d89d8fceceeb283bbf5

See more details on using hashes here.

Provenance

The following attestation bundles were made for tokmat_polars-0.1.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.1.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tokmat_polars-0.1.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9640cb75ec29de165821367e142690cb702f67a7d4aa65a695b97a457119e262
MD5 27a8f0f16bd7f4ac88b2b0cc28fcdb0c
BLAKE2b-256 34fa0df9e4a5cd71b12c1eb642b56dfdfbe651e1520abb4592c65b4257aaee48

See more details on using hashes here.

Provenance

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