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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bertalign-2.0.1.tar.gz (31.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bertalign-2.0.1-py3-none-any.whl (32.1 kB view details)

Uploaded Python 3

File details

Details for the file bertalign-2.0.1.tar.gz.

File metadata

  • Download URL: bertalign-2.0.1.tar.gz
  • Upload date:
  • Size: 31.3 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for bertalign-2.0.1.tar.gz
Algorithm Hash digest
SHA256 0afe3d154e34b24be17e0d7172b4e5d522447f8cfc0eb94430a4ad1af5011811
MD5 09043bc6b3bd9927ebc3d086cedb4231
BLAKE2b-256 fcc989a4149978e52ebeec541487332d7b082aa46b41b24b3c8272518a3f1671

See more details on using hashes here.

File details

Details for the file bertalign-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: bertalign-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 32.1 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for bertalign-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d8224c160e488436141a80c6acb05595c2c47bc6b3e1b3966adde5eb97cc6e33
MD5 a82817608f254cf8581bb7c085b6bb95
BLAKE2b-256 dce6655f486cbc6f512ec5b7d5d63c5fefc44871d30ec7f6d69206b301d4d296

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 files

2.0.0

2 files

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