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

Uploaded CPython 3.13Windows x86-64

tokmat_polars-0.3.2-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.2-cp313-cp313-macosx_11_0_arm64.whl (3.7 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

tokmat_polars-0.3.2-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.2-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.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

Hashes for tokmat_polars-0.3.2.tar.gz
Algorithm Hash digest
SHA256 7e0422b12d94233445ccc2071084284b41e816b1f2d784e678f6f901ae051b7f
MD5 9fbd85f3b1f949ffa684aadebe94e3ef
BLAKE2b-256 25b19efb0efce2643d11a92c60f2c569c8093ec472a4fc45cbfdccdc9cfb9c1c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tokmat_polars-0.3.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 47c3d8922aad1212ba1197fff696085badff5ef2309971057187ec7436c2abc2
MD5 85aa48ebc9b1d557aa49a51cf93033af
BLAKE2b-256 e2a0950fae4b0b9beeaef262becd67c142effc100cdfad29e12d6e318551b770

See more details on using hashes here.

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

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.2-cp313-cp313-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for tokmat_polars-0.3.2-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 e5f4dc8c437ad028d7c41cbd5b63011d5983e9637d2739bf214814a5102be072
MD5 445cbeb60c8912fe233385e2416430bb
BLAKE2b-256 6ed487c8035ddede5ca2d59ca00b1a218af6d8e46d7adb3186485d5b725ab413

See more details on using hashes here.

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

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.2-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tokmat_polars-0.3.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d81a82effce30cc27a60b186ee615dc2f555e659b440cb11f569e17c3e5be812
MD5 a50a758363c3491132595b4a7c56763d
BLAKE2b-256 32e799bc73b6e61d9f48b9995d6af48b594128e01f9f0ea6a26c4dd7fa764559

See more details on using hashes here.

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

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.2-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for tokmat_polars-0.3.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 355f18117e5943a1f91267ec7735c76b754d438f5c8ef9f1e30d8fdc0ad0deba
MD5 0ab6e891fc4fe832eafa1a75dc412334
BLAKE2b-256 350732eea876ebef909bf4e886e20c750a6d4fd4abd26bdd678a11e969e47549

See more details on using hashes here.

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

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.2-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for tokmat_polars-0.3.2-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 615af9cc24df5fd1299cc1f4f706fe373261dd70767b5219176254c537caf855
MD5 24d54914eb6db3a930e999abb22327cf
BLAKE2b-256 3e8cfcbc1b922ac37268b3311281069135d26ca7c792b52034f956fe17d0fff7

See more details on using hashes here.

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

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.2-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tokmat_polars-0.3.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6fb9f38c05ca1d51947104af0d535a2e369d2a8ed10948e35cca452fddcf8837
MD5 95855be5b9ae99a6b7b870ea2c489aa1
BLAKE2b-256 b85a876d60d2c185d78c7ee954fd20fa56cd4dbdb29faf40eb1c02486da30f41

See more details on using hashes here.

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

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