Skip to main content

divvun-hfst

Divvun HFST for Python. It reads optimized-lookup transducers (.hfstol), pmatch tokenisers (.pmhfst) and DHFST files. It looks words up, tokenises text and checks words against DHFST acceptors, and gives the results the hfst command-line tools give. It is the Divvun HFST library, written in Rust, as a native extension module: one wheel per platform for CPython 3.11 and later. Its surface is that of the npm package @divvun/hfst, in Python's idioms, and the two run the same code.

The package reads and looks up. It does not compile, write or change transducers.

pip install divvun-hfst

It is imported as divvun_hfst: hfst is the HFST project's own Python package.

Lookup

import divvun_hfst

analyser = divvun_hfst.load_transducer("analyser-gt-desc.hfstol")

analyser.lookup("teach")
# [LookupResult(output='teach+Noun+Masc+Com+Sg',
#               symbols=('t', 'e', 'a', 'c', 'h', '+Noun', '+Masc', '+Com', '+Sg'), weight=0.0),
#  LookupResult(output='teach+Noun+Masc+Com+Sg+DefArt', symbols=(...), weight=0.0),
#  ...]

load_transducer(path) reads a file; read_transducer(data) takes its bytes. Both take a weighted or unweighted optimized-lookup transducer, with or without an HFST header. load_transducers and read_transducers take a stream of several and return them in stream order, a pmatch container as TOP and then its definitions. Every reader has both forms, and a load_ form reads the file in the library, without the GIL.

lookup returns every result the hfst library finds, lightest first, then by output. output is the output symbols joined; symbols lists them, without flag diacritics and epsilons. It takes two keyword-only limits: limit stops the search after that many paths, and time_limit, in seconds, returns the results found so far once it runs out.

analyser.lookup("teach", limit=1, time_limit=0.5)

A transducer's type says which optimized-lookup format it is in, name is the name its header gives it, and properties is a dict of the rest of its header.

The optimised lookup

load_optimised_transducer(path) and read_optimised_transducer(data) read the same files for divvunspell's lookup engine, its divvun-fst crate. Its lookup returns what divvunspell returns for the same transducer and word, in the same shape, and is usually several times faster.

fast = divvun_hfst.load_optimised_transducer("analyser-gt-desc.hfstol")
fast.lookup("teach")           # the same four analyses
fast.lookup("teach", limit=1)  # the lightest one

For most words the two lookups agree. They differ on Unicode normalisation, text outside the alphabet, cycles of epsilon and flag arcs, duplicate paths and what limit means; MIGRATING.md gives an example of each. Use Transducer where results must match hfst's, and OptimisedTransducer where they must match divvunspell's, or where speed matters most. The optimised lookup has no time limit.

Tokenising

tokeniser = divvun_hfst.load_tokeniser("tokeniser-disamb-gt-desc.pmhfst")

text = "Inuit nunaat."
for token in tokeniser.tokenise(text):
    print(text[token.start:token.end], token.analysed, token.readings)
# Inuit True (Reading(analysis='Inuit Sem/Hum Prop Abs Pl', weight=0.0), ...)
# nunaat True (Reading(analysis='nuna N Abs Pl 3PlPoss', weight=0.0), ...)
# . True (Reading(analysis='. CLB', weight=0.0),)

print(tokeniser.format(text, "giella-cg"))
# "<Inuit>"
# 	"Inuit" Sem/Hum Prop Abs Pl <W:0.0>
# 	...

start and end count code points, so text[start:end] is the token's surface. format returns, byte for byte, what hfst tokenise prints, in segment, space-separated, xerox, cg, finnpos, giella-cg, conllu or visl format; divvun_hfst.TokeniseFormat is their type.

Options are one TokeniseOptions for tokenise and one FormatOptions for format, made once and passed to as many calls as need them:

options = divvun_hfst.TokeniseOptions(max_weight_classes=1, print_all=True)
tokeniser.tokenise(text, options)

weights = divvun_hfst.FormatOptions(print_weights=True)
tokeniser.format(text, "cg", weights)

Each field is named after the hfst tokenise setting it stands for, in snake_case, with time limits in seconds. TokeniseOptions takes dedupe, max_weight_classes, beam, time_limit, tokenize_multichar, print_all, and weight_cutoff, which hfst tokenise lacks. FormatOptions takes all of them but weight_cutoff, and print_weights, hack_uncompose, newline, keep_newline and superblanks; a field left as None has the value the format's flag gives it. Both are frozen dataclasses, checked when they are made: a value of the wrong type raises TypeError, and one hfst tokenise would turn down ValueError. dataclasses.replace makes a variant.

DHFST

model = divvun_hfst.load_dhfst("acceptor.default.dhfst")
if isinstance(model, divvun_hfst.DhfstAcceptor):
    model.lookup("sámi")  # the word's least weight, or None when the lexicon rejects it

load_dhfst and read_dhfst read either type of DHFST file and make the checks divvunspell makes when it loads one, returning a DhfstAcceptor or a DhfstErrorModel as its type says. An acceptor, a speller's lexicon, answers lookup(word) as divvunspell's speller checks a word as typed, and its info says how it stores its arcs. An error model gives its version, state_count, symbols and meta. For spelling suggestions, use divvunspell.

Threads

Loading, both lookups, tokenising and acceptor lookup release the GIL while the library works. Every loaded object is immutable, so threads can share one and run in parallel:

from concurrent.futures import ThreadPoolExecutor

with ThreadPoolExecutor() as pool:
    analyses = list(pool.map(analyser.lookup, words))

Errors

A failure the library reports raises a divvun_hfst.HfstError, whose code is the code @divvun/hfst gives the same failure (divvun_hfst.HfstErrorCode lists them). Bytes a reader does not load raise its subclass divvun_hfst.FormatError:

  • unsupported-format: a format the package does not read, such as a tropical OpenFst or foma transducer. The message names it.
  • invalid-format: bytes that are not well formed in the format they claim, or that end partway through it.
  • type-mismatch: a stream mixing transducer types.
  • transducer-count: more transducers than the reader takes.

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

Logging

The library's log events go to the standard logging module once the call returns, on loggers named after the Rust module they came from: under hfst for the hfst library, such as hfst.pmatch_core, and under divvun_fst for the optimised lookup's engine. Python's own levels apply, with the library's trace level at 5. Events below the level of hfst or divvun_fst at the start of a call are not kept, so set those loggers' levels to see more:

import logging

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

How it is checked

The suite runs the npm package's parity fixtures through the built package: every lookup, optimised lookup, token list, tokenise format, acceptor lookup and DHFST description in them, and compares each with what the native library gives, value for value, and each format with what hfst tokenise prints, byte for byte.

Types

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

Developing

The package is built from crates/hfst-python in the hfst-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
uv run --no-sync mypy                   # the stubs and the tests
uv run --no-sync maturin build --release

The tests read the transducers the npm package's tests read, and the native library's results for them. They build them with node js/test/fixtures.mjs <dir>, which needs Node 20 or later and cargo, or take them from the directory DIVVUN_HFST_FIXTURES names, such as the target/js-test/fixtures that node js/build.mjs --test writes. They check the wheel DIVVUN_HFST_WHEEL names, or build one.

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

Licence

LGPL-3.0-or-later. The package carries the licence in COPYING.LESSER and the GPL it builds on in COPYING. Divvun HFST is copyright UiT The Arctic University of Norway.

Metadata

Release files for divvun-hfst 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-hfst 0.1.0
File Size Uploaded Provenance Actions
divvun_hfst-0.1.0.tar.gz 971.6 kB Without provenance Details for divvun_hfst-0.1.0.tar.gz

Built distributions (wheels)

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

Total release size: 6.4 MB

Release files / divvun_hfst-0.1.0.tar.gz

Download URL divvun_hfst-0.1.0.tar.gz
Size 971.6 kB
Tags Source
SHA-256 checksum
How to use checksums
fa8969036c3b1ff7cf8c8beb3a8a4d380c353548678ddfb69d2e55dfd7270cc4
BLAKE2b-256 checksum
How to use checksums
e6ee6406e0312cc6b43ec8947d6e8e659c17e22f423ecf64602cd36fee810f6c
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_hfst-0.1.0-cp311-abi3-win_arm64.whl

Download URL divvun_hfst-0.1.0-cp311-abi3-win_arm64.whl
Size 624.0 kB
Tags CPython 3.11 Windows ARM64 abi3
SHA-256 checksum
How to use checksums
a7fc21d01ca48a7273b3facb20575b3cf67d6f1cae3f4601786915339bc0ca70
BLAKE2b-256 checksum
How to use checksums
c994203277453192c05b8cb52672c269039440da7fc4a048b7ffb62a0a985515
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_hfst-0.1.0-cp311-abi3-win_amd64.whl

Download URL divvun_hfst-0.1.0-cp311-abi3-win_amd64.whl
Size 650.7 kB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
cba91011ac98f0e60c800f124a7013ef4de9b4892d0a26946e5a64557cf4890d
BLAKE2b-256 checksum
How to use checksums
240a07404853dcca5dca573a3552e4bc8ca40a1d97cc2a75f564e2c2f0d8dd05
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_hfst-0.1.0-cp311-abi3-musllinux_1_2_x86_64.whl

Download URL divvun_hfst-0.1.0-cp311-abi3-musllinux_1_2_x86_64.whl
Size 860.6 kB
Tags CPython 3.11 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
02e0eeac931d6e3df2c2474bfe91aa0b72c560a1c735bdaaa6e0574501511ff1
BLAKE2b-256 checksum
How to use checksums
42236874e6b0f2a41d3464b0f3b5e09a62c14de5a5690518d77c3e0e74e41063
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_hfst-0.1.0-cp311-abi3-musllinux_1_2_aarch64.whl

Download URL divvun_hfst-0.1.0-cp311-abi3-musllinux_1_2_aarch64.whl
Size 819.9 kB
Tags CPython 3.11 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
8a8753e04945b379a1a8e458a465001fc8fb60efbc792c0cea3994448df72e0e
BLAKE2b-256 checksum
How to use checksums
534984e2ddbfe5abf3457482755dde374a367a2c59018a1af9c164be360c2240
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_hfst-0.1.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL divvun_hfst-0.1.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 862.1 kB
Tags CPython 3.11 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
9e5feb5b5abb188dc940fe10e95ed6a1fbc74226cf71c17628cacdf6a6c69ebd
BLAKE2b-256 checksum
How to use checksums
bc60f1c76bed6bb30900a3e8cbb17ee58fa14411aa516b8286173d9e5ce240d1
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_hfst-0.1.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl

Download URL divvun_hfst-0.1.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Size 821.1 kB
Tags CPython 3.11 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
eca9624995cd3b8afc24d705ca2368344b7e9de228ee200ab396e02b8cca6fb3
BLAKE2b-256 checksum
How to use checksums
8d787b6b5670951c756085c886757a6678ee8ace8cc34d2e9841829eda8785a1
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_hfst-0.1.0-cp311-abi3-macosx_11_0_arm64.whl

Download URL divvun_hfst-0.1.0-cp311-abi3-macosx_11_0_arm64.whl
Size 765.5 kB
Tags CPython 3.11 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
3f31460b270bb36ed9c24bd12aeffccf0b8511c51cdac807af6aceb0d37a8319
BLAKE2b-256 checksum
How to use checksums
aac7d2deba967fa327cca365bf0b08cdade633c62333287930692c180cdcd6b1
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-hfst 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