The SentenceAligner class is the primary interface for performing word alignments.
Initialization:
You can specify the embedding model and alignment settings in the constructor. Common parameters include model, token_type, and matching_methods.
Alignment Process:
- Initialize
SentenceAligner. - Provide source and target sentences as lists of tokens (they must be pre-tokenized to words).
- Call
get_word_aligns(src_sentence, trg_sentence).
Output Format:
The method returns a dictionary where keys are the matching methods used and values are lists of tuples. Each tuple contains a pair of zero-indexed integers representing the aligned word indices (source_index, target_index).
from simalign import SentenceAligner
# Initialize the aligner
myaligner = SentenceAligner(model="bert", token_type="bpe", matching_methods="mai")
# Sentences must be tokenized to words
src_sentence = ["This", "is", "a", "test", "."]
trg_sentence = ["Das", "ist", "ein", "Test", "."]
# Get alignments
alignments = myaligner.get_word_aligns(src_sentence, trg_sentence)
# Example iteration over results
for matching_method in alignments:
print(matching_method, ":", alignments[matching_method])
# Expected output format:
# mwmf (Match): [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
# inter (ArgMax): [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
# itermax (IterMax): [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]