Skip to main content

In-memory sentence alignment with Vecalign dynamic programming

Project description

Vecalign

New: Python library API (sentalign)

This repository now supports installation as a Python package and an in-memory API:

pip install .
from sentence_transformers import SentenceTransformer
from sentalign import sentalign

model = SentenceTransformer("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")

src = ["Hallo Welt.", "Wie geht es dir?"]
tgt = ["Hello world.", "How are you?"]

result = sentalign(src, tgt, encoder=model)
print(result.overall_score)
for block in result.alignments:
    print(block.src_indices, block.tgt_indices, block.score)

The encoder argument is intentionally model-agnostic: pass any object with an encode(list[str]) -> 2D array method (or a callable with the same behavior).

overall_score is a heuristic aggregate quality score in [0, 1] (higher is better), derived from alignment costs and penalizing insertion/deletion blocks.

Run tests and inspect English/French alignment output

A small test suite for the new in-memory API lives under tests/.

python -m pip install -e .[test]
python -m pytest -q

# Optional: run integration test with a real multilingual encoder
python -m pip install -e .[test-real]
python -m pytest -q -k sentence_transformers

If you want to force GPU usage and verify it explicitly during tests, you can run:

python - <<'PY'
import torch
from sentence_transformers import SentenceTransformer

device = "cuda" if torch.cuda.is_available() else "cpu"
print("Chosen device:", device)
if device == "cuda":
    print("GPU name:", torch.cuda.get_device_name(0))

model = SentenceTransformer(
    "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2",
    device=device,
)
print("Model device:", next(model._first_module().auto_model.parameters()).device)
PY

python -m pytest -q -k sentence_transformers

Tip: run nvidia-smi -l 1 in another terminal while the test runs to confirm GPU utilization.

To quickly see alignment output on two in-memory lists (English vs French), run:

python examples/english_french_demo.py

For a more complex French→English example with intentional sentence splits/merges (N:M alignment blocks), run:

python examples/complex_french_english_demo.py

This prints per-block alignments and an overall quality score in [0, 1]. For production quality embeddings, pass your own encoder model (e.g. LASER or SentenceTransformers) to sentalign(...).

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.1.1.tar.gz (161.9 kB view details)

Uploaded Source

Built Distribution

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

sentalign-0.1.1-cp310-cp310-win_amd64.whl (89.1 kB view details)

Uploaded CPython 3.10Windows x86-64

File details

Details for the file sentalign-0.1.1.tar.gz.

File metadata

  • Download URL: sentalign-0.1.1.tar.gz
  • Upload date:
  • Size: 161.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.4

File hashes

Hashes for sentalign-0.1.1.tar.gz
Algorithm Hash digest
SHA256 556250c2ef6eb38d9f486ee784dc08433c8a45e87e4b8435548ec556e1361801
MD5 cd93ab7af834ed88f8e012f3f01da806
BLAKE2b-256 ec94cbcd3236e55dd197ffb55d2e3b2e8d518daf7b0f22fc127cc847883bc8e8

See more details on using hashes here.

File details

Details for the file sentalign-0.1.1-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: sentalign-0.1.1-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 89.1 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.4

File hashes

Hashes for sentalign-0.1.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 0bdee1b9cf29ba91df6ada8f6831651727bf7eafbe17e7949a9b744b5aee6191
MD5 a8dae220bf9a5f0f9280bd5320a1b6e0
BLAKE2b-256 16e3129f456882e99c7edd36689340fc4ad42a2dca5c62a64a85522de3351a6b

See more details on using hashes here.

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