Skip to main content

divvun-cg3

VISL CG-3 for Python. It loads Constraint Grammars, compiled (.cg3b) or textual, and runs them on text, and a run gives the output vislcg3 gives for the same grammar, input and options, byte for byte. It is the cg3-rs library, written in Rust, as a native extension module: one wheel per platform for CPython 3.11 and later.

pip install divvun-cg3

Running a grammar

import cg3

grammar = cg3.load_grammar("disambiguator.cg3b")

result = grammar.apply('"<word>"\n\t"word" n\n\t"word" v\n')
result.output    # the output, as a string
result.warnings  # every warning and non-fatal error the run logged, in order

There are three ways to load a grammar:

  • cg3.load_grammar(path) reads a file. It loads it as compiled when it begins with the .cg3b magic, as vislcg3 decides, and as textual otherwise, with relative INCLUDEs found beside it.
  • cg3.read_grammar(data) loads the bytes of a .cg3b, as cg-comp writes it.
  • cg3.parse_grammar(source, name=None) parses a textual grammar from a string. Its diagnostics call it name, and its relative INCLUDEs resolve against the directory of the file name names.
grammar = cg3.parse_grammar('DELIMITERS = "<.>" ; SELECT (n) ;')
grammar.apply('"<word>"\n\t"word" n\n\t"word" v\n').output
# '"<word>"\n\t"word" n\n'

A grammar is immutable and loaded once. Each apply is a run of its own over it: nothing one run learns reaches the next.

Options

A run's options are one cg3.RunOptions, made once and passed to as many runs as need it:

options = cg3.RunOptions(trace=True, num_windows=4)
grammar.apply(text, options)

apertium = cg3.RunOptions(input_format="apertium", output_format="apertium")
grammar.apply(text, apertium)

Each field is named after its vislcg3 long option in snake_case: --num-windows 4 is num_windows=4. An option whose value vislcg3 makes optional is True without one, as --trace is trace=True. input_format and output_format stand for the --in-* and --out-* options: "cg" (the default), "niceline", "apertium", "fst", "plain" and "jsonl"; cg3.Format is their type.

RunOptions is a frozen dataclass, checked when it is made: a value of the wrong type raises TypeError, a number outside 0 to 4294967295 OverflowError, and a format the package does not offer ValueError. An option it does not know raises TypeError, as any Python class does. dataclasses.replace(options, trace=False) makes a variant, and its repr names only the options given. The options a grammar sets with CMDARGS apply as they do in vislcg3, and an EXTERNAL rule starts its process as vislcg3 starts it.

Grammar information

grammar.info() gives what vislcg3 -v reports about a grammar:

grammar.info()
# GrammarInfo(sections=1, templates=0, rules=3, sets=25, tags=31, rules_any=0,
#             has_dependency_rules=False, has_relation_rules=False)

Threads

Loading and applying release the GIL while the library works, so threads can apply one grammar at the same time and run in parallel:

from concurrent.futures import ThreadPoolExecutor

with ThreadPoolExecutor() as pool:
    outputs = [r.output for r in pool.map(grammar.apply, texts)]

Errors

A failure the library reports raises a cg3.Cg3Error:

  • cg3.GrammarError for a grammar that does not load or parse. A parse failure's message quotes every error in the grammar's source, as vislcg3 prints it.
  • cg3.RunError for a run that fails. The grammar stays usable.

An argument of the wrong type or value raises the TypeError, ValueError, OverflowError or OSError Python raises for it, and a file that cannot be read raises what open() would.

Logging

The library's log events, from loading a grammar and from running one, go to the standard logging module once the call returns, on loggers under cg3 named after the Rust module they came from, such as cg3.apertium_applicator. Python's own levels apply, with the library's trace level at 5. Events below the level of the cg3 logger at the start of a call are not kept, so set that logger's level to see more:

import logging

logging.getLogger("cg3").setLevel(logging.DEBUG)

Whatever the logging setup, apply still returns its run's warnings.

How it is checked

The suite runs every grammar of cg3-rs's golden corpus through the built package twice, loaded from its textual source and from the .cg3b cg-comp writes for it, and compares each output with vislcg3's byte for byte. 67 of the 69 tests match both ways, the EXTERNAL rule's included. Two are exempt, because they pass an option the package does not offer (--nrules, --vislcg-compat); none differ.

Types

The package ships type stubs and the py.typed marker, and mypy --strict checks them.

Developing

The package is built from crates/cg3-python in the cg3-rs repository, with maturin and uv:

cd python
uv sync --no-install-project
uv run --no-sync maturin develop --uv   # build the extension into .venv
uv run --no-sync pytest                 # the tests, with cg-comp and vislcg3
uv run --no-sync mypy                   # the stubs and the tests
uv run --no-sync maturin build --release

The tests build cg-comp, vislcg3 and the program the corpus's EXTERNAL rule runs with cargo, or take them from the directory CG3_BIN names. They check the wheel CG3_WHEEL names, or build one.

The package's version is the one crates/cg3-python/Cargo.toml carries. A python/v<version> tag naming it releases the wheels and the sdist to PyPI.

Licence

GPL-3.0-or-later. The package carries the licence's text.

Metadata

Release files for divvun-cg3 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for divvun-cg3 0.1.0
File Size Uploaded Provenance Actions
divvun_cg3-0.1.0.tar.gz 752.4 kB Without provenance Details for divvun_cg3-0.1.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for divvun-cg3 0.1.0
File Actions
divvun_cg3-0.1.0-cp311-abi3-win_arm64.whl CPython 3.11 abi3 Windows ARM64 Details for divvun_cg3-0.1.0-cp311-abi3-win_arm64.whl
divvun_cg3-0.1.0-cp311-abi3-win_amd64.whl CPython 3.11 abi3 Windows x86-64 Details for divvun_cg3-0.1.0-cp311-abi3-win_amd64.whl
divvun_cg3-0.1.0-cp311-abi3-musllinux_1_2_x86_64.whl CPython 3.11 abi3 Linux musl 1.2+ x86-64 Details for divvun_cg3-0.1.0-cp311-abi3-musllinux_1_2_x86_64.whl
divvun_cg3-0.1.0-cp311-abi3-musllinux_1_2_aarch64.whl CPython 3.11 abi3 Linux musl 1.2+ ARM64 Details for divvun_cg3-0.1.0-cp311-abi3-musllinux_1_2_aarch64.whl
divvun_cg3-0.1.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.11 abi3 Linux glibc 2.17+ x86-64 Details for divvun_cg3-0.1.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
divvun_cg3-0.1.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl CPython 3.11 abi3 Linux glibc 2.17+ ARM64 Details for divvun_cg3-0.1.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
divvun_cg3-0.1.0-cp311-abi3-macosx_11_0_arm64.whl CPython 3.11 abi3 macOS 11.0+ ARM64 Details for divvun_cg3-0.1.0-cp311-abi3-macosx_11_0_arm64.whl

Total release size: 14.6 MB

Release files / divvun_cg3-0.1.0.tar.gz

Download URL divvun_cg3-0.1.0.tar.gz
Size 752.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c3fb92705edf03cb06761c7afaff5725e72b0a37aa5ae417103fde1031e79148
BLAKE2b-256 checksum
How to use checksums
69b28b01d608a41db8bf2722382a0c36f83fbab2885acd36e9db99d19eaa3ed5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / divvun_cg3-0.1.0-cp311-abi3-win_arm64.whl

Download URL divvun_cg3-0.1.0-cp311-abi3-win_arm64.whl
Size 1.8 MB
Tags CPython 3.11 Windows ARM64 abi3
SHA-256 checksum
How to use checksums
944d5e9d91d189e08e602376d1e87004086e4e5df8fb38997410df4a8a822658
BLAKE2b-256 checksum
How to use checksums
dcd23aa9e5e2d9d3ff14452296d0987ea9d6c320bd52c55da3e2ac108ca1a208
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / divvun_cg3-0.1.0-cp311-abi3-win_amd64.whl

Download URL divvun_cg3-0.1.0-cp311-abi3-win_amd64.whl
Size 2.0 MB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
3d2aba32f2d6fdd074e89017bd9d4f83871142125d8f6b3cc62cee10ea89dd3b
BLAKE2b-256 checksum
How to use checksums
1efcd27f3bf476314abd8439333ad144ef030a1272ec843dbee5fb333f872bd5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / divvun_cg3-0.1.0-cp311-abi3-musllinux_1_2_x86_64.whl

Download URL divvun_cg3-0.1.0-cp311-abi3-musllinux_1_2_x86_64.whl
Size 2.1 MB
Tags CPython 3.11 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
712a3fb7ba6603f26ebbe6064c51054ee995033cc172263c39afb6d65953907e
BLAKE2b-256 checksum
How to use checksums
cfca8eee8522b08cf1854dba06040c7fe5ca38e9dd5f38ab22101f199e87cc8e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / divvun_cg3-0.1.0-cp311-abi3-musllinux_1_2_aarch64.whl

Download URL divvun_cg3-0.1.0-cp311-abi3-musllinux_1_2_aarch64.whl
Size 1.9 MB
Tags CPython 3.11 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
e0cee94f4fc6322e6633ac138b5b8a93708a0b5e4f3ff3792de7ba9c5cd7c3c4
BLAKE2b-256 checksum
How to use checksums
28d56c208f0ddd430fb5565e45315b4241a3311e12fcdefa4ef4eb44ec423a12
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / divvun_cg3-0.1.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL divvun_cg3-0.1.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 2.1 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
4fbc3b9d89b530d945bd4ab8be95782ecd28c70c44b2b4d3e657815d7994fbae
BLAKE2b-256 checksum
How to use checksums
d281f623da6ce91975fa255b2736187e656fec040ae051bdba9ed92d0f8ed64a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / divvun_cg3-0.1.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl

Download URL divvun_cg3-0.1.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Size 1.9 MB
Tags CPython 3.11 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
383abb9d9418ffb770cec1e53b6de2c02f2cb8df00db9884dea1db0acd20054e
BLAKE2b-256 checksum
How to use checksums
baeb559c1f40fbae85405b19c70a304a2f584fdc02e7edbf9f7b901e96b87067
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / divvun_cg3-0.1.0-cp311-abi3-macosx_11_0_arm64.whl

Download URL divvun_cg3-0.1.0-cp311-abi3-macosx_11_0_arm64.whl
Size 2.0 MB
Tags CPython 3.11 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
fdbdeb3be16c9de17f811a44664c47604063fc97809cda0e39e6f355eee44b98
BLAKE2b-256 checksum
How to use checksums
730585da436f64233498084ef8408bac89d9b79ec495f92c5bb98f060c4cbcb3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for divvun-cg3 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

8 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page