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

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.1.tar.gz (38.2 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.1-cp313-cp313-win_amd64.whl (5.0 MB view details)

Uploaded CPython 3.13Windows x86-64

tokmat_polars-0.2.1-cp313-cp313-manylinux_2_34_x86_64.whl (5.6 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

tokmat_polars-0.2.1-cp313-cp313-macosx_11_0_arm64.whl (4.7 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

tokmat_polars-0.2.1-cp312-cp312-win_amd64.whl (5.0 MB view details)

Uploaded CPython 3.12Windows x86-64

tokmat_polars-0.2.1-cp312-cp312-manylinux_2_34_x86_64.whl (5.6 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

tokmat_polars-0.2.1-cp312-cp312-macosx_11_0_arm64.whl (4.7 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: tokmat_polars-0.2.1.tar.gz
  • Upload date:
  • Size: 38.2 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.1.tar.gz
Algorithm Hash digest
SHA256 8ee0f33a10dc67097b6cfe120dd44e2d295d01fd8bfb735bbe7047ea11af1949
MD5 0cd27202c6912eb16471be6906fdf8a2
BLAKE2b-256 e71942665020fcab1f8e1ece393c1e4919a784a26448ce30dd79693e401473b1

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tokmat_polars-0.2.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 f639dbac2c91b0c7334be170b73cf5e56d45ddbde79aae8a5d42d1282b70529e
MD5 1fd682d9c3e21f114f3918de1ea3bb28
BLAKE2b-256 abf910bb47f163b74316e795fd54572fa31ea55d78e6ab272d98a457c24fbc4b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tokmat_polars-0.2.1-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 f2f5211e204cc178c9cfee49661d798ed0994365b4654f3552b3239008d5b512
MD5 d4a65f6aa9fa428c44a55ef254df0bd5
BLAKE2b-256 bc94c4de86c3857a2e185a1efb7333ff54d4baac38b468b5268d73a77a467e17

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tokmat_polars-0.2.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9c608fbf4562f91f77e99700e7de7481768ad4b4311821be8e04c080fb27c699
MD5 4d6541b4139124b8a9e50fdf822381e5
BLAKE2b-256 b704efb9c41b4e5df8d57c2f02a3fd2f1b80468f69e324a465adca8be51c8570

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tokmat_polars-0.2.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 8d6114eae616500605c1856b1c2fb19f2cf934b478315b997605f73e763e85f9
MD5 c5cdf0e5e2f2dd20808ddf764dec797b
BLAKE2b-256 6986fbaf9ac024a8fcc438ef316c493869aa16b46e951fbc30e66ce98d3c09e8

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tokmat_polars-0.2.1-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 f186aff2eac0117ce6937d063303e1b53ff320c6e1a89c730ab2c068d8b64749
MD5 dd9745dc59acefcce982de40a56a349c
BLAKE2b-256 dde7a05be4d9334fa203fa79be89d72710d6add8d2728da3b7bfd7445ed78458

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tokmat_polars-0.2.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 66cbb6260468504f5f7bd8c5f14057dd6e08001c19cd776b694e8cf75a106c25
MD5 05ea5f567f4bc4b8fcd3e193c72af10e
BLAKE2b-256 5bc461bffc52410d5b0b97156e4b03dda0baddd697f61bccf0ecee681abf03e5

See more details on using hashes here.

Provenance

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