Skip to main content

divvun-fst

divvunspell for Python. It loads BHFST and ZHFST speller archives and checks and corrects words with them, and looks words up in optimized-lookup, THFST and DHFST transducers. It is divvunspell's own Rust library, divvun-fst, as a native extension module, so it gives the results native divvunspell gives: one wheel per platform for CPython 3.11 and later. It is the Python counterpart of the npm package @divvun/fst.

pip install divvun-fst

Spelling

import divvun_fst

speller = divvun_fst.load_speller("se.bhfst")

speller.is_correct("sámi")  # True
speller.suggest("sami")
# [Suggestion(value='sámi', weight=14.58..., completed=None, weight_details=None), ...]
# The weights depend on the speller's build.

speller.locale               # 'se'
speller.metadata.info.title  # [SpellerTitle(lang=None, value='...'), ...]

There are two ways to load a speller:

  • divvun_fst.load_speller(path) opens an archive and maps its members from the file, as the divvunspell tools do: a BHFST by the extension .bhfst, a ZHFST by .zhfst.
  • divvun_fst.read_speller(data) reads the bytes of one: a BHFST of either box version, with a THFST or DHFST error model and lexicon, or a ZHFST, stored or deflated, told apart by their first bytes. The archive's members are read in place from one copy of the bytes.

A speller also has analyze_input(word), the lexicon's analyses of the word as typed, with its tags, and analyze_output(word), the suggestions with their tags. Results are Suggestions: value, weight (lower is better), completed, and weight_details when the config sets verbose. Each weight is the 32-bit float divvunspell computed, as the Python float that holds it exactly.

Configuration

Every method takes an optional SpellerConfig, divvunspell's config with the same fields and defaults:

from divvun_fst import ReweightingConfig, SpellerConfig

config = SpellerConfig(n_best=3, max_weight=40, reweight=ReweightingConfig(mid_penalty=3))
speller.suggest("sami", config)

With no config a speller runs with the config its archive bundles (speller.bundled_config), or else divvunspell's defaults, which SpellerConfig() is. A config given is the whole config: a field it leaves at its default takes divvunspell's default, as in a config file, not the bundled value. To change one field of the bundled config, replace it:

import dataclasses

tuned = speller.bundled_config or SpellerConfig()
speller.suggest("sami", dataclasses.replace(tuned, n_best=3))
Field Default
n_best 10 the most suggestions; None for all
max_weight 10000.0 the greatest weight a suggestion may have; None for no limit
beam None how much heavier than the best the worst suggestion may be
reweight ReweightingConfig(): start 10, end 10, mid 5, no curve penalties by where an edit falls; None for none
recase True keep the input's capitals, and offer the lexicon's
completion_marker None the marker for incomplete word parts
astar_lookahead False order the search by the A* heuristic
search_dedup True walk states rather than paths; leave it on
mutator_subsets True determinise the error model as the search goes; leave it on
search_budget None how many nodes the search may examine per word
word_split_weight None what putting a space back into a word costs
boundary_edit_weight None what a lexicon form one separator edit away costs
initial_case_weight None what starting a capitalised form from a lower-case letter costs
verbose False give each suggestion its weight_details

SpellerConfig and ReweightingConfig are frozen, keyword-only dataclasses, checked when they are made: a value of the wrong type raises TypeError, NaN or an infinity ValueError (None is how a field says there is no limit), and a number out of range OverflowError. A field divvunspell does not have raises TypeError, as for any Python class, so a misspelt setting never silently does nothing. A config file an archive bundles has the same fields in kebab-case: n-best for n_best.

Lookup

analyser = divvun_fst.load_transducer("analyser-gt-desc.hfstol")
analyser.lookup("gïele")
# [LookupResult(output='gïele+N+Sg+Nom', symbols=['g', 'ï', 'e', 'l', 'e', '+N', '+Sg', '+Nom'], weight=0.0)]

analyser.lookup("gïele", limit=1)  # the cheapest result only

load_transducer(path) and read_transducer(data) take HFST optimized lookup, a DHFST acceptor or error model, or a box archive holding a THFST directory or one of those files; load_transducer also takes a THFST directory. A box holding several, such as a BHFST speller, needs the member named, or raises ValueError naming the ones it holds:

lexicon = divvun_fst.load_transducer("se.bhfst", member="acceptor.default.thfst")

lookup gives every output the transducer has for the word, cheapest first, then by output. The word is read as hfst-optimized-lookup reads it, by longest match over the input symbols, so a generator's tags such as +N are read whole. transducer.format says what was loaded: optimized-lookup, thfst, dhfst-acceptor or dhfst-error-model.

Tokenizing

divvunspell's tokenizer, with offsets in code points, as Python indexes a str:

text = "Sámi giella, 😀 dál!"
divvun_fst.word_indices(text)        # [(0, 'Sámi'), (5, 'giella'), (15, 'dál')]
divvun_fst.word_bound_indices(text)  # every run between word boundaries, spaces and punctuation too
divvun_fst.cursor_context("Sámi gie", "lla dál")
# WordContext(current=(5, 'giella'), first_before=(0, 'Sámi'), second_before=None,
#             first_after=(12, 'dál'), second_after=None)

For each word text[start:start + len(word)] is the word. cursor_context gives the word at a cursor between two halves of a text and the two words on either side, with offsets into the two halves together.

Threads

Loading, checking, suggesting, analysing and lookup release the GIL while the library works. Spellers and transducers are immutable, so threads can share one and run in parallel:

from concurrent.futures import ThreadPoolExecutor

with ThreadPoolExecutor() as pool:
    corrections = list(pool.map(speller.suggest, words))

Errors

A failure the library reports, such as bytes that are no speller archive or a broken transducer, raises a divvun_fst.FstError, whose code is the one @divvun/fst gives the same failure:

  • unsupported-format: input in a format the package does not read. The message says what it starts with.
  • invalid-format: input in a format it reads that is not well formed in it: truncated tables, a missing member, broken metadata.
  • internal: a failure inside the library that no input should cause.
  • failed: any other failure the library reports; the message says what.

An argument of the wrong type or value raises the TypeError or ValueError Python raises for it, a box member that is not there among them, and a file that cannot be opened raises the OSError open() would, such as FileNotFoundError.

Logging

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

import logging

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

How it is checked

The suite loads every fixture of @divvun/fst's parity check twice, by path and from its bytes, and compares every result with native divvun-fst's, value for value: speller results from small spellers made from the repository's test fixtures and, where they are on the machine, the sma, se and ga spellers, as BHFST of both box versions with THFST and DHFST members and as ZHFST stored and deflated, under ten configs that between them set every field; and lookups in THFST, DHFST and optimized-lookup transducers.

Types

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

Developing

The package is built from crates/divvun-fst-python in the divvunspell 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
uvx --with 'pytest>=8' --from 'mypy>=1.13' mypy   # the stubs and the tests
uv run --no-sync maturin build --release

uv sync uninstalls the extension maturin develop built, so sync first and build after.

The tests read @divvun/fst's parity fixtures from the directory DIVVUN_FST_FIXTURES names, as node js/test/fixtures.mjs <dir> writes it, or else run that script with node, which builds the native expectations with cargo. Real spellers and analysers are found where js/test/fixtures.mjs says, each at a path an environment variable can override; a test that needs one that is not there is skipped, or fails when DIVVUN_FST_REQUIRE_ALL=1. The package tests check the wheel DIVVUN_FST_WHEEL names, and that it is the package imported, or else build one.

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

Licence

MIT or Apache-2.0, at your option. The package carries both licences' text.

Metadata

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

Built distributions (wheels)

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

Total release size: 12.1 MB

Release files / divvun_fst-0.1.0.tar.gz

Download URL divvun_fst-0.1.0.tar.gz
Size 472.0 kB
Tags Source
SHA-256 checksum
How to use checksums
5c70e9ca9cc1f04458bcf0bf3a6e9c8bd33ab7a88e1b14619a804e58519f486d
BLAKE2b-256 checksum
How to use checksums
83b2487ee9c311389206d1c94d63eb1c36353b3c4411ccec06e81d6446de2b68
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_fst-0.1.0-cp311-abi3-win_arm64.whl

Download URL divvun_fst-0.1.0-cp311-abi3-win_arm64.whl
Size 1.7 MB
Tags CPython 3.11 Windows ARM64 abi3
SHA-256 checksum
How to use checksums
6dcf7c5b6aee8b325cf4d32a645777ca9496031d17cf39dc42cec4404f5b2418
BLAKE2b-256 checksum
How to use checksums
186cae7e60a1070efcdf05ac96ba9c1ee3cfe1d4677a70178c1ecaac1bf4f1c5
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_fst-0.1.0-cp311-abi3-win_amd64.whl

Download URL divvun_fst-0.1.0-cp311-abi3-win_amd64.whl
Size 1.8 MB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
7366a8fad61818448a762d790d8f885ed995c46a4cd1837bcef362b22d256619
BLAKE2b-256 checksum
How to use checksums
60d79ed571ccb995cc24e5a5046fd361a246e3d259032f98e8ccd6cdc6dbed62
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_fst-0.1.0-cp311-abi3-musllinux_1_2_x86_64.whl

Download URL divvun_fst-0.1.0-cp311-abi3-musllinux_1_2_x86_64.whl
Size 1.8 MB
Tags CPython 3.11 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
02a60580fd291e0accbac04575af12e80ef5a70b36ff02a59866a520fe7206fd
BLAKE2b-256 checksum
How to use checksums
4b445b42a961bd7ae885c8290ccb4c46ef2a528d5e3b74652c854fff09cf1782
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_fst-0.1.0-cp311-abi3-musllinux_1_2_aarch64.whl

Download URL divvun_fst-0.1.0-cp311-abi3-musllinux_1_2_aarch64.whl
Size 1.5 MB
Tags CPython 3.11 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
8ad7fc545a24546c699b4269d6c98f63ca7a78315608fafc29acb37864462e1e
BLAKE2b-256 checksum
How to use checksums
8af6930dc15baf07780a1cdcd55a031bb1d2018ccd5cc171ae4fe1423528efd8
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_fst-0.1.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL divvun_fst-0.1.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 1.8 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
2116d1949183855ba6d933031da27d30c822ae63d7e6ad8004f713c800a021f5
BLAKE2b-256 checksum
How to use checksums
b05606ed0fd40016573a180fc28e5f02b756eaf6dcae7cbe5b9c4756ee0d9ab4
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_fst-0.1.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl

Download URL divvun_fst-0.1.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Size 1.5 MB
Tags CPython 3.11 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
6ff510e88b05bf0d3548e34089d1431922253a164c0e0b2d7500ba02c55ff53e
BLAKE2b-256 checksum
How to use checksums
4f7193c9b2e310960a50193c676e3011e989550955c4f91abaed1ee23714dd54
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_fst-0.1.0-cp311-abi3-macosx_11_0_arm64.whl

Download URL divvun_fst-0.1.0-cp311-abi3-macosx_11_0_arm64.whl
Size 1.5 MB
Tags CPython 3.11 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
21b1f869f51303556d85390d32d88032b39229bea8d097df573f202a87896264
BLAKE2b-256 checksum
How to use checksums
86d8c031c809b0ce2d06b97ff3fab2495354158d276dcc2dd532b8e7358c8ec4
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-fst 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