Skip to main content

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


Download files

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

Source Distribution

sentalign-0.2.1.tar.gz (153.2 kB view details)

Uploaded Source

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

Hashes for sentalign-0.2.1.tar.gz
Algorithm Hash digest
SHA256 afe5bc7b959ba371f6f53de6a8309bc0167dfaaf0d897fa2c3758abf1b076bcc
MD5 2f43c1a78c65f09b5a8cfbd362ae597c
BLAKE2b-256 d288e7b0d4f1d46161572a36fcf329733fb3bea459a39ef3966fffb670ee4c22

See more details on using hashes here.

Provenance

The following attestation bundles were made for sentalign-0.2.1.tar.gz:

Publisher: publish.yml on amajdalawi/sentalign

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page