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.2.0
git push origin v0.2.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.2.0.tar.gz (30.8 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.2.0-cp313-cp313-win_amd64.whl (4.5 MB view details)

Uploaded CPython 3.13Windows x86-64

tokmat_polars-0.2.0-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.2.0-cp313-cp313-macosx_11_0_arm64.whl (4.2 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

tokmat_polars-0.2.0-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.2.0-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.2.0.tar.gz.

File metadata

  • Download URL: tokmat_polars-0.2.0.tar.gz
  • Upload date:
  • Size: 30.8 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.2.0.tar.gz
Algorithm Hash digest
SHA256 ddafc60c698a07e686ea0395c0604a0fc2a58cd380a3dde84d4288115d4cd76b
MD5 34c9177d60941d36f5fa27c1e8802403
BLAKE2b-256 e92599961fcaf91b79a76a9e2097815d1b7d8c3c4ac0ff5f5d3a24ba20e0a245

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tokmat_polars-0.2.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 b7776ee43072e17889e7b9eb94274ef28bba40bc7f6fe11f27338092f630fb07
MD5 4ced493d5add8468dda171a07dbc574d
BLAKE2b-256 818f9e6504e13ec7415c25f97bc645f05c1f0ddef0009a56ca4c7dd5afda7f21

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tokmat_polars-0.2.0-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 cd99bc0f74868b0ae6428c200e7bcc993cae1911d1fa8b91695b60434a26e935
MD5 a91f668c5e708364e328c837949bbf0a
BLAKE2b-256 f873963f8ca3afaf8d1bce57eeba4aa346e031a41494058246eae3634dfc7c4f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tokmat_polars-0.2.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4ddcd77f53c568c35d03c13d518403e8164e17005bb5fa3d4add232464c460ae
MD5 2dfb9900050f85ca809482263461bb6d
BLAKE2b-256 b6197a48329247f6527c0e692f52e54f2cd92f1e2de2cfd4bade09140c9dad99

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tokmat_polars-0.2.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 2b98bc9b4cf6c656f071699ca05a4e17a3ae5eb8581760d278a26185bcc4543a
MD5 1dbfafab7ecdf4f101a14c0be0f40b18
BLAKE2b-256 f967b661cf38f209a124bb0fff94335fa1306b4bc0782ec78c462e50106708f7

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tokmat_polars-0.2.0-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 d727a5ce839eedd180aea14ab9c02805fc4707b1945c66f30fa4a01a8ccfe47b
MD5 0bd687bce64c94012e9a662b378a4ae2
BLAKE2b-256 8938554383255ff2e34a4a71062096fb1b82ad40e9d6049eedbc56d42bcb7476

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tokmat_polars-0.2.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 87bd59540c8c2bc22bbc2cbc77df21f0b088119e7220696efe4f6472a0a93cf2
MD5 08d2f5f22fcdfc35051b3a423fdd3d5d
BLAKE2b-256 dfd2f997bb13682dd269cfdcd7c46b85e02526f9e0e0e015d41f79d17d16e9a6

See more details on using hashes here.

Provenance

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