Compute the longest or optimal weighted, most contiguous common subsequence and diff for a pair of sequences
Project description
lcs2
lcs2
is a Python package that helps find the longest common—or optimal weighted—subsequence of a pair of sequences and compute their diff. Among all optimal subsequences, it returns the one with the fewest runs (i.e., contiguous segments) across the two sequences.
Installation
pip install lcs2
Reference
The package provides the following functions, where weight[A, B] = Callable[[A, B], int | float]
.
If the weight
function is provided, it should return positive values if and only if the two elements can be matched. If the weight
function is omitted, the elements are considered equal if they are identical, and the classical LCS sequence is returned.
Function | Signature | Result |
---|---|---|
lcs |
Iterable[A], Iterable[B], weight[A, B]?, take_b? -> list[A] | list[B] |
Longest (or optimal weighted) common subsequence (LCS/OWCS) |
lcs_indices |
Iterable[A], Iterable[B], weight[A, B]? -> list[tuple[int, int]] |
Indices of the LCS/OWCS |
lcs_length |
Iterable[A], Iterable[B] -> int |
Length of the LCS |
lcs_weight |
Iterable[A], Iterable[B], weight[A, B] -> int | float |
Weight of the OWCS |
diff |
Iterable[A], Iterable[B], weight[A, B]? -> list[tuple[list[A], list[B]]] |
Differing segments of the sequences based on the LCS/OWCS |
diff_ranges |
Iterable[A], Iterable[B], weight[A, B]? -> list[tuple[range, range]] |
Ranges of indices of the differing segments |
Sample Usage
from lcs2 import diff, diff_ranges, lcs, lcs_indices, lcs_length, lcs_weight
a = 'Hello, world!'
b = 'Foobar'
print(lcs(a, b)) # ['o', 'o', 'r']
print(lcs_indices(a, b)) # [(4, 1), (8, 2), (9, 5)]
print(lcs_length(a, b)) # 3
print(diff(a, b)) # [(['H', 'e', 'l', 'l'], ['F']),
# ([',', ' ', 'w'], []),
# ([], ['b', 'a']),
# (['l', 'd', '!'], [])]
print(diff_ranges(a, b)) # [(range(0, 4), range(0, 1)),
# (range(5, 8), range(2, 2)),
# (range(9, 9), range(3, 5)),
# (range(10, 13), range(6, 6))]
print(lcs(
'xo',
'xXOo',
weight=lambda ca, cb: 1 if ca.lower() == cb.lower() else 0,
take_b=True,
)) # ['X', 'O']
print(lcs_weight(
'xxxoo',
'ooxxx',
weight=lambda ca, cb: {'x': 1, 'o': 2}[ca] if ca == cb else 0,
)) # 4
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
Built Distribution
File details
Details for the file lcs2-2.0.0.tar.gz
.
File metadata
- Download URL: lcs2-2.0.0.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
eb6fb6531d12f84075d22c6af875f178c8d9e2984d9079234a50da5af0743722
|
|
MD5 |
82f2972b91b568d1dadf0de6b0ce30de
|
|
BLAKE2b-256 |
04d704c489df97ce02fa9b54bf6675d72169d11e14e758752fc0cef85bad4e90
|
File details
Details for the file lcs2-2.0.0-py3-none-any.whl
.
File metadata
- Download URL: lcs2-2.0.0-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
cf016b131c37bf058d967fb6aba8c9c5f17e7911793f043eab4f0956ae829d26
|
|
MD5 |
b4a44942cf1e668eb823e74a0bdf3973
|
|
BLAKE2b-256 |
c1c92a7f2cf173c25ed9ebe6784ea88cd67cb935f7ba6e1b4ae8dc7545490122
|