Skip to main content

A package with a simple 1D-DTW implementation for sentence alignment.

Project description

DTW-Sentence-Alignment

A simple, low-dependency package for aligning sentences by minimizing a chosen metric.

Motivation

I needed to match sentences between two lists and was disappointed to find that there wasn't a simple package available for this task without excessive dependencies or unintuitive interfaces.

Overview

DTW-Sentence-Alignment is a Python package for aligning sentences using Dynamic Time Warping (DTW). It supports custom similarity functions and predefined metrics, maximizing alignment scores. Unlike traditional implementations, it allows flexible starting and ending points for alignment.

Installation

To install the package, you can use pip:

pip install dtwsa

Usage

Here's a basic example of how to use the package:

from dtwsa import SentenceAligner
from dtwsa.metrics import WER_similarity

# Align sentences
list_1 = [
    "Something which does not match",
    "Matching sentence number one",
    "Something which does not match",
    "Another matching sentence",
    "Something which does not match",
    "Random Sentence which should match",
    "This should be matched with something",
    "Yet another matching sentence",
    "Random Sentence which should match",
    "This should be matched with something",
    "Yet another matching sentence",
    "Something which does not match",
    "Something which does not match",
    "Something that matches again",
    "Something which does not match",
]

list_2 = [
    "Matching sentence number one",
    "Another matching sentence",
    "Random Sentence which should match",
    "This should be matched with something",
    "Yet another matching sentence",
    "Random Sentence which should match",
    "This should be matched with something",
    "Yet another matching sentence",
    "Something that matches again",
    "Something leftover",
]

# Create a SentenceAligner object with the WER_similarity metric and 0.7 as the minimum matching value
# The minimum matching value is useful to avoid matching sentences that are not similar enough.
# Better to not match anything than to match something that is not similar enough.

aligner = SentenceAligner(WER_similarity, min_matching_value=0.7)

# Align the sentences and get the alignment and score
alignment, score = aligner.align_sentences(list_1, list_2)

print(f"Alignment: {alignment}") # [(0, 0), (1, 1), (3, 2), (5, 3), (6, 4), (7, 5), (8, 6), (9, 7), (10, 8), (13, 9)]
print(f"Score: {score}") # 10.0

# Plot the alignment
aligner.visualize_alignment(list_1, list_2)

Visuaization of the alignment

Features

  • Flexible sentence alignment using custom similarity functions
  • Predefined metrics like Word Error Rate (WER) similarity
  • Simple API for easy integration

TODO

  1. Improve efficiency of the alignment algorithm by limiting the choices of the alignment by limiting the maximum distance of indexes between matches.
  2. Improve efficiency of the alignment algorithm by implementing a version of PrunedDTW.
  3. Improve efficiency of the alignment algorithm by parallelization.
  4. Add new metrics for sentence comparison (e.g., BLEU score, cosine similarity)

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

dtwsa-0.0.8.tar.gz (158.6 kB view details)

Uploaded Source

Built Distribution

dtwsa-0.0.8-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file dtwsa-0.0.8.tar.gz.

File metadata

  • Download URL: dtwsa-0.0.8.tar.gz
  • Upload date:
  • Size: 158.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for dtwsa-0.0.8.tar.gz
Algorithm Hash digest
SHA256 c355d93e9d126263dfdad7141f42eb3376858f07b761b6f0dceafda63cd63436
MD5 e33f3958afb07bdc37616d339f2e6560
BLAKE2b-256 bde9a8f20d982cf6ef3a993dc1142dd25472119126492875c7e0d82261f59235

See more details on using hashes here.

File details

Details for the file dtwsa-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: dtwsa-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for dtwsa-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 84d88342d3e737d6fc7e5033bab4b50324e9226ad62b38486110ac79d418f811
MD5 6bed1e12a2cc562021964b6b2b3d6540
BLAKE2b-256 87d03c24a11b603a76ae70bc6b1c9d6deed67997407c7c52f1748b0c3b22f42f

See more details on using hashes here.

Supported by

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