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 does what the other tools do too: it writes a grammar out as cg-comp does, relabels one as cg-relabel does, runs one as cg-proc does, and converts and splits streams as cg-conv and cg-mwesplit do. 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.

parse_grammar can also take the files a grammar's INCLUDEs read from memory, as a mapping from names to text or bytes. Then every INCLUDE reads from those files and no others: a relative name is found beside the source that includes it, else at the top of the names, as it would be on disk.

grammar = cg3.parse_grammar(
    "INCLUDE sets.cg3 ;\nSELECT Nouns ;",
    "grammar.cg3",
    files={"sets.cg3": 'DELIMITERS = "<.>" ;\nLIST Nouns = n ;'},
)
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.

Writing a grammar

A grammar writes itself out without changing:

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

grammar.to_cg3b()                         # the .cg3b cg-comp writes, as bytes
grammar.to_text()                         # what vislcg3 --grammar-out writes
grammar.write_cg3b("disambiguator.cg3b")  # the .cg3b and its companion file

write_cg3b writes the .cg3b and, as cg-comp does, the companion file beside it, disambiguator.cg3b.cg3src, which holds the grammar's sources so that a runtime error in a rule can quote it. A grammar loaded from a .cg3b has no sources, so nothing is written beside it.

The other tools

cg3.convert converts a stream from one format to another, as cg-conv does, and cg3.mwesplit splits multi-word cohorts, as cg-mwesplit does:

cg3.convert('"<word>"\n\t"word" n\n', cg3.ConvertOptions(output_format="apertium"))
# '^word/word<n>$'

cg3.mwesplit(text)

grammar.proc(input, options) runs a grammar as cg-proc does, on the Apertium stream unless the options name another, and grammar.relabel(rules) gives the grammar cg-relabel makes of it and the relabel rules, as a new grammar:

grammar.proc("^word/word<n>/word<vblex>$\n", cg3.ProcOptions(trace=True))

rules = cg3.load_grammar("relabel.cg3r")
grammar.relabel(rules).write_cg3b("relabelled.cg3b")

load_grammar reads the rules as cg-relabel reads its rule file, compiled or textual. ConvertOptions and ProcOptions are frozen dataclasses like RunOptions, checked when they are made, with a field for each option named after the tool's long option in snake_case: cg-conv --sub-delim "#" is sub_delim="#", and cg-proc --wordform-case is wordform_case=True. ConvertOptions.input_format and output_format stand for --in-* and --out-*, and the input's format is detected from the input, as cg-conv detects it, when input_format is left out. ProcOptions.stream_format is "apertium" (the default), "cg" or "matxin", the formats --stream-format numbers 1, 0 and 2; cg3.ProcFormat is their type. The binary stream format is offered by neither. The options that do nothing in the tools, rtl and cg-proc's disambiguation and null_flush, do nothing here.

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, applying, writing and the other tools 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, parse or relabel. A parse failure's message quotes every error in the grammar's source, as vislcg3 prints it.
  • cg3.RunError for a run, a conversion or a split 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 or written raises what open() would.

Logging

The library's log events, from loading a grammar, running one and the other tools, 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. All 69 tests match both ways, the EXTERNAL rule's included, with the options their command lines give read as a LoadOptions and a RunOptions; none is exempt.

It holds the package to the other tools the same way. Every corpus grammar, loaded from disk and from memory, writes the .cg3b and companion file cg-comp writes for it, and its to_text is what vislcg3 --grammar-out writes. Every input cg3-rs's tests give cg-conv, cg-mwesplit, cg-proc and cg-relabel gives the tool's bytes through convert, mwesplit, proc and relabel, with the tool's command line read into a ConvertOptions or ProcOptions: 255 cases, the JavaScript package's, which js/test/tool-fixtures.mjs lists. Four are exempt, because they ask cg-conv for the binary stream; 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 the cg3 tools and the program the corpus's EXTERNAL rule runs with cargo, or take them from the directory CG3_BIN names. The tool parity check writes its fixtures with node js/test/tool-fixtures.mjs, or reads the directory CG3_TOOL_FIXTURES names, which that command wrote. 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.2.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.2.0
File Size Uploaded Provenance Actions
divvun_cg3-0.2.0.tar.gz 772.1 kB Without provenance Details for divvun_cg3-0.2.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for divvun-cg3 0.2.0
File Actions
divvun_cg3-0.2.0-cp311-abi3-win_arm64.whl CPython 3.11 abi3 Windows ARM64 Details for divvun_cg3-0.2.0-cp311-abi3-win_arm64.whl
divvun_cg3-0.2.0-cp311-abi3-win_amd64.whl CPython 3.11 abi3 Windows x86-64 Details for divvun_cg3-0.2.0-cp311-abi3-win_amd64.whl
divvun_cg3-0.2.0-cp311-abi3-musllinux_1_2_x86_64.whl CPython 3.11 abi3 Linux musl 1.2+ x86-64 Details for divvun_cg3-0.2.0-cp311-abi3-musllinux_1_2_x86_64.whl
divvun_cg3-0.2.0-cp311-abi3-musllinux_1_2_aarch64.whl CPython 3.11 abi3 Linux musl 1.2+ ARM64 Details for divvun_cg3-0.2.0-cp311-abi3-musllinux_1_2_aarch64.whl
divvun_cg3-0.2.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.2.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
divvun_cg3-0.2.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl CPython 3.11 abi3 Linux glibc 2.17+ ARM64 Details for divvun_cg3-0.2.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
divvun_cg3-0.2.0-cp311-abi3-macosx_11_0_arm64.whl CPython 3.11 abi3 macOS 11.0+ ARM64 Details for divvun_cg3-0.2.0-cp311-abi3-macosx_11_0_arm64.whl

Total release size: 16.0 MB

Release files / divvun_cg3-0.2.0.tar.gz

Download URL divvun_cg3-0.2.0.tar.gz
Size 772.1 kB
Tags Source
SHA-256 checksum
How to use checksums
2de43caf0a53f5a60b95ba3f6725b4851c0b755fa7dd9045685962b0fd1c9cc9
BLAKE2b-256 checksum
How to use checksums
7758f6170b14b7d49bdbba81ed1424ab74a976cd82d830b73d34d744f5be963c
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.2.0-cp311-abi3-win_arm64.whl

Download URL divvun_cg3-0.2.0-cp311-abi3-win_arm64.whl
Size 2.0 MB
Tags CPython 3.11 Windows ARM64 abi3
SHA-256 checksum
How to use checksums
4b1d7e7c1e0b3941ac323f6fc68cd4e88fa34af44da2aced480a4a9aefceae10
BLAKE2b-256 checksum
How to use checksums
10512ab1beb04239e3e294b358bb4f398c58ae1acc02909dfd966f7e2e0edad2
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.2.0-cp311-abi3-win_amd64.whl

Download URL divvun_cg3-0.2.0-cp311-abi3-win_amd64.whl
Size 2.2 MB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
6379ca09f029d1ccea9c79eec5cc203cad9e2a3d27fe83a9f9d197df3b6dfe26
BLAKE2b-256 checksum
How to use checksums
4683b52f3bbda473b3360ca9813f59ac385c3538a52cb281b408da4ca43a26ee
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.2.0-cp311-abi3-musllinux_1_2_x86_64.whl

Download URL divvun_cg3-0.2.0-cp311-abi3-musllinux_1_2_x86_64.whl
Size 2.3 MB
Tags CPython 3.11 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
2d09f44ad20a4d0eaedb0775544234cf09de48875a1c8ba7f57941c24a645c87
BLAKE2b-256 checksum
How to use checksums
86d00def5ffd7623f61fee1b6c200234a499eab71691300f165f717ebfa01895
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.2.0-cp311-abi3-musllinux_1_2_aarch64.whl

Download URL divvun_cg3-0.2.0-cp311-abi3-musllinux_1_2_aarch64.whl
Size 2.1 MB
Tags CPython 3.11 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
4a62ed790ed1e609facd2a58a7f33e98595fb6a430e68ec73041c74df4a72f09
BLAKE2b-256 checksum
How to use checksums
01285ab164bd5d1fa8a1c63f590cfe8082f174104580b379395dc09ac3ac568c
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.2.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL divvun_cg3-0.2.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 2.3 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
49c5ae9331d83dcdfa516fbc67979fd8c948034e04eefa9cfc30fec95434aa6a
BLAKE2b-256 checksum
How to use checksums
28c2ef800d622f3f9d1307affb060cdee5b1f7ab4e9dbe11187759c981732c80
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.2.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl

Download URL divvun_cg3-0.2.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Size 2.1 MB
Tags CPython 3.11 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
cbee3b523b9960b1006f0e3e64dd46e9ed9abe5176a837b575f06ce5339da483
BLAKE2b-256 checksum
How to use checksums
101ea09c6e7f27b9f0721c6f173aff378b4ac18ae0dc819784d340b032220cf8
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.2.0-cp311-abi3-macosx_11_0_arm64.whl

Download URL divvun_cg3-0.2.0-cp311-abi3-macosx_11_0_arm64.whl
Size 2.2 MB
Tags CPython 3.11 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
21907f2723fdf8f049ff8e1733bb25113f1d4109ca4722ad23fc72d93d7a3232
BLAKE2b-256 checksum
How to use checksums
35a1f4aad3697619ecb86ca14a2b2017c77723a9be4be6e1b630a0d7ea6a4f83
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.2.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.2.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