Skip to main content

Bertalign

ci PyPI Python Licence

An automatic multilingual sentence aligner. Bertalign takes two texts that are translations of each other and works out which sentences correspond — including 1-to-many, many-to-1, and many-to-many mappings — producing aligned sentence pairs for translation study, corpus construction, or training data mining.

It implements the two-pass algorithm of Liu & Zhu (2022): sentences (and their overlapping concatenations) are embedded with LaBSE, a first dynamic programming pass finds approximate 1-1 anchors via top-k similarity search, and a second pass finds the optimal m-n alignment along that path.

This is a modernized fork of bfsujason/bertalign: packaged for PyPI, fully typed, GPU-accelerated end to end, with offline language detection and no network calls at alignment time.

Install

uv add bertalign        # or: pip install bertalign
uv add "bertalign[sat]" # optional: neural sentence splitting (see below)

Python 3.12+. A CUDA GPU is used automatically when available, for both embedding and similarity search; everything also runs on CPU.

Usage

from bertalign import Bertalign

aligner = Bertalign(src_text, tgt_text)
aligner.align_sents()
for src_segment, tgt_segment in aligner.sent_pairs():
    print(src_segment, "|||", tgt_segment)

Languages are detected automatically (offline, via lingua). If your texts already hold one sentence per line, pass is_split=True to skip splitting.

Aligning many document pairs reuses one loaded model automatically; to be explicit, share an encoder:

from bertalign import Bertalign, Encoder

encoder = Encoder("LaBSE")
for src_text, tgt_text in documents:
    aligner = Bertalign(src_text, tgt_text, model=encoder)
    aligner.align_sents()

Command line

bertalign source.txt target.txt > pairs.tsv

Inputs are plain-text files; output is one tab-separated pair per line (internal whitespace is normalized during cleaning, so fields never contain tabs). Options: --model (any sentence-transformers model), --max-align (largest bead size), --is-split, --sat, --trust-remote-code, --version.

Choosing a model

Any sentence-transformers model works via model=/--model. On the text+berg German–French gold standard (RTX 5070, this repository's scripts/benchmark_model.py):

Model Strict F1 Lax F1 Load + align
LaBSE (default) 0.936 0.989 4.7 + 9.9 s
BAAI/bge-m3 0.935 0.992 38 + 35 s
google/embeddinggemma-300m 0.928 0.986 33 + 29 s
intfloat/multilingual-e5-large 0.919 0.988 35 + 35 s
Qwen/Qwen3-Embedding-0.6B 0.915 0.990 27 + 24 s

LaBSE remains the default: it was trained specifically for translation-pair retrieval, and it is both the most accurate on strict F1 and by far the fastest. Retrieval models that expect instruction prefixes (e5, Qwen3) lose accuracy here because bertalign embeds raw sentences.

Sentence splitting

The default splitter is rule-based (sentence-splitter) and supports 25 languages: Catalan, Chinese, Czech, Danish, Dutch, English, Finnish, French, German, Greek, Hungarian, Icelandic, Italian, Latvian, Lithuanian, Norwegian, Polish, Portuguese, Romanian, Russian, Slovak, Slovenian, Spanish, Swedish, and Turkish.

The sat extra adds neural splitting with wtpsplit's SaT models (state of the art on multilingual segmentation benchmarks, language-agnostic, GPU-accelerated) — useful for noisy text or languages the rule splitter does not cover:

from bertalign import Bertalign
from bertalign.sat import sat_splitter

aligner = Bertalign(src_text, tgt_text, splitter=sat_splitter())

or bertalign --sat source.txt target.txt on the command line.

Evaluation

The scorer from Vecalign ships as bertalign.eval, and the repository carries the text+berg evaluation corpus:

just eval                                            # gold-standard regression gate
uv run python scripts/benchmark_model.py BAAI/bge-m3 # benchmark another model

just eval must reproduce Strict F1 0.936 / Lax F1 0.989 exactly; it is the correctness gate for any change to the aligner, kernels, or dependencies.

Development

uv sync --extra sat
just check   # ruff (select = ALL), ty, vulture, pytest, build

Licence and citation

GPL-3.0-or-later, inherited from upstream. If you use Bertalign in research, cite:

Lei Liu & Min Zhu. 2022. Bertalign: Improved word embedding-based sentence alignment for Chinese–English parallel corpora of literary texts. Digital Scholarship in the Humanities. https://doi.org/10.1093/llc/fqac089

Metadata

Release files for bertalign 2.0.1

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

Source distribution (sdist)

Source distribution for bertalign 2.0.1
File Size Uploaded Provenance
bertalign-2.0.1.tar.gz 31.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for bertalign 2.0.1
File Interpreter ABI Platform
bertalign-2.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 63.4 kB

Release files / bertalign-2.0.1.tar.gz

Download URL bertalign-2.0.1.tar.gz
Size 31.3 kB
Tags Source
SHA-256 checksum
How to use checksums
0afe3d154e34b24be17e0d7172b4e5d522447f8cfc0eb94430a4ad1af5011811
BLAKE2b-256 checksum
How to use checksums
fcc989a4149978e52ebeec541487332d7b082aa46b41b24b3c8272518a3f1671
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.0 {"installer":{"name":"uv","version":"0.12.0","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 / bertalign-2.0.1-py3-none-any.whl

Download URL bertalign-2.0.1-py3-none-any.whl
Size 32.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d8224c160e488436141a80c6acb05595c2c47bc6b3e1b3966adde5eb97cc6e33
BLAKE2b-256 checksum
How to use checksums
dce6655f486cbc6f512ec5b7d5d63c5fefc44871d30ec7f6d69206b301d4d296
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.0 {"installer":{"name":"uv","version":"0.12.0","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 bertalign 2.0.1

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

2.0.1 This release

2 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