In-memory sentence alignment with Vecalign dynamic programming
Project description
sentalign
sentalign is a small Python package for aligning sentences between two languages using multilingual sentence embeddings and VecAlign-style dynamic programming.
It works fully in memory: pass two lists of sentences, get back aligned sentence blocks.
Installation
pip install sentalign
For real multilingual alignment, you also need an embedding model. For example:
pip install sentence-transformers
Basic usage
from sentence_transformers import SentenceTransformer
from sentalign import sentalign
src = [
"Hello world.",
"My name is Abdulrahman.",
"I like machine learning.",
]
tgt = [
"Bonjour le monde.",
"Je m'appelle Abdulrahman.",
"J'aime l'apprentissage automatique.",
]
encoder = SentenceTransformer(
"sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"
)
result = sentalign(
src,
tgt,
encoder=encoder,
)
print("Overall score:", result.overall_score)
for alignment in result.alignments:
print(alignment.src_indices, alignment.tgt_indices)
print(alignment.src_sentences)
print(alignment.tgt_sentences)
print("score:", alignment.score)
print()
Important: use a multilingual encoder
sentalign does not create embeddings by itself. You must pass an encoder.
For cross-language alignment, use a multilingual encoder such as:
SentenceTransformer("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
or a model like LaBSE.
Do not use a monolingual English-only model for English/French, English/Arabic, Hebrew/English, etc. The encoder must place sentences from both languages in the same embedding space.
API
sentalign(src_sentences, tgt_sentences, encoder)
Returns a SentAlignResult:
result.alignments
result.overall_score
result.average_alignment_score
Each alignment block contains:
alignment.src_indices
alignment.tgt_indices
alignment.score
alignment.src_sentences
alignment.tgt_sentences
Development
Install locally in editable mode:
pip install -e .
Run a simple multilingual test:
pip install sentence-transformers
python tests/test_multilingual.py
Build the package:
python -m build
twine check dist/*
License
Apache-2.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file sentalign-0.2.1.tar.gz.
File metadata
- Download URL: sentalign-0.2.1.tar.gz
- Upload date:
- Size: 153.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
afe5bc7b959ba371f6f53de6a8309bc0167dfaaf0d897fa2c3758abf1b076bcc
|
|
| MD5 |
2f43c1a78c65f09b5a8cfbd362ae597c
|
|
| BLAKE2b-256 |
d288e7b0d4f1d46161572a36fcf329733fb3bea459a39ef3966fffb670ee4c22
|
Provenance
The following attestation bundles were made for sentalign-0.2.1.tar.gz:
Publisher:
publish.yml on amajdalawi/sentalign
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sentalign-0.2.1.tar.gz -
Subject digest:
afe5bc7b959ba371f6f53de6a8309bc0167dfaaf0d897fa2c3758abf1b076bcc - Sigstore transparency entry: 2016281000
- Sigstore integration time:
-
Permalink:
amajdalawi/sentalign@828dbead42652f0182960df75a4c0106a9fee8e4 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/amajdalawi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@828dbead42652f0182960df75a4c0106a9fee8e4 -
Trigger Event:
push
-
Statement type: