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.0.tar.gz (153.8 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.2.0-cp310-cp310-win_amd64.whl (221.0 kB view details)

Uploaded CPython 3.10Windows x86-64

File details

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

File metadata

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

File hashes

Hashes for sentalign-0.2.0.tar.gz
Algorithm Hash digest
SHA256 37f6adfa1cdd0ec3ff6418afa0c268ebdb362d886ef50b4f20d4b1458d4dda94
MD5 93c31ffc98a65a00792e64e3f6db17c8
BLAKE2b-256 61d979d4fb5ede8d7c3f373a0feffaad1cd4712991268a2ee66e26472d00dc56

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sentalign-0.2.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 221.0 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.2.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 5efe52873731b1b8226bcef63fd9c7f9e4a18a23d1aaeb92dfa2017ab0004472
MD5 debf5566bbbe4170d8fe12fe57137bba
BLAKE2b-256 4f28ea13e3863f2b52c3a865d5c7ba482223d6faeb0659ad97f0a1bfcc822f10

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