super fast cpp implementation of longest common subsequence
Project description
pylcs
The original repository stop maintenance. This is a transfer version
pylcs is a super fast c++ library which adopts dynamic programming(DP) algorithm to solve two classic LCS problems as below .
The longest common subsequence problem is the problem of finding the longest subsequence common to all sequences in a set of sequences (often just two sequences).
The longest common substring problem is to find the longest string (or strings) that is a substring (or are substrings) of two or more strings.
Levenshtein distance, aka edit distance
is also supported. Emm...forget the package name. Example usage is in tests.
We also support Chinese(or any UTF-8) string.
Colorful Visualization: After 0.1.0, you can visualize the lcs result with colorful output.
Install
To install, simply do pip install pylcs
to pull down the latest version from PyPI.
Python code example
import pylcs
# finding the longest common subsequence length of string A and string B
A = 'We are shannonai'
B = 'We like shannonai'
pylcs.lcs_sequence_length(A, B)
"""
>>> pylcs.lcs_sequence_length(A, B)
14
"""
# finding alignment from string A to B
A = 'We are shannonai'
B = 'We like shannonai'
res = pylcs.lcs_sequence_idx(A, B)
''.join([B[i] for i in res if i != -1])
"""
>>> res
[0, 1, 2, -1, -1, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
>>> ''.join([B[i] for i in res if i != -1])
'We e shannonai'
"""
# finding the longest common subsequence length of string A and a list of string B
A = 'We are shannonai'
B = ['We like shannonai', 'We work in shannonai', 'We are not shannonai']
pylcs.lcs_sequence_of_list(A, B)
"""
>>> pylcs.lcs_sequence_of_list(A, B)
[14, 14, 16]
"""
# finding the longest common substring length of string A and string B
A = 'We are shannonai'
B = 'We like shannonai'
pylcs.lcs_string_length(A, B)
"""
>>> pylcs.lcs_string_length(A, B)
11
"""
# finding alignment from string A to B
A = 'We are shannonai'
B = 'We like shannonai'
res = pylcs.lcs_string_idx(A, B)
''.join([B[i] for i in res if i != -1])
"""
>>> res
[-1, -1, -1, -1, -1, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
>>> ''.join([B[i] for i in res if i != -1])
'e shannonai'
"""
# finding the longest common substring length of string A and a list of string B
A = 'We are shannonai'
B = ['We like shannonai', 'We work in shannonai', 'We are not shannonai']
pylcs.lcs_string_of_list(A, B)
"""
>>> pylcs.lcs_string_of_list(A, B)
[11, 10, 10]
"""
# finding the weighted edit distance from string A to B
pylcs.edit_distance("aaa", "aba")
pylcs.edit_distance("aaa", "aba", {'a': {'b': 2.0}})
pylcs.edit_distance("", "aa", {'': {'a': 0.5}})
# weight['']['a'] means inserting a char 'a' costs 0.5
# similarly, weight['a'][''] means the score of deleting a char 'a'
"""
>>> pylcs.edit_distance("aaa", "aba")
1
>>> pylcs.edit_distance("aaa", "aba", {'a': {'b': 2.0}})
2.0
>>> pylcs.edit_distance("", "aa", {'': {'a': 0.5}})
1.0
"""
# finding edit distance alignment from string A to B
pylcs.edit_distance_idx("aaa", "aba")
pylcs.edit_distance_idx("aaa", "aba", {'a': {'b': 3}})
pylcs.edit_distance_idx("aa", "aabb", {'a': {'a': 2, 'b': 0}})
"""
>>> pylcs.edit_distance_idx("aaa", "aba")
[0, 1, 2]
>>> pylcs.edit_distance_idx("aaa", "aba", {'a': {'b': 3}})
[0, -1, 2]
>>> pylcs.edit_distance_idx("aa", "aabb", {'a': {'a': 2, 'b': 0}})
[2, 3]
"""
After 0.1.0, you can make a visualized comparison with colorful output. Using coloring_match_sequence
to color the s1 and s2 by a match list like:
s1, s2 = "abcdefghijklmnopq", "-c-fgh-kl-nop-q"
match_list = pylcs.lcs_sequence_idx(s1, s2)
colored_s1, colored_s2 = pylcs.coloring_match_sequence(match_list, s1, s2, 1, 1, "#FFEEAA", "#FFEEAA", t=1)
print(colored_s1, colored_s2)
colored_s1, colored_s2 = pylcs.coloring_match_sequence(match_list, s1, s2, 1, 1, "#FFEEAA", "#FFEEAA", t=2)
print(colored_s1, colored_s2)
colored_s1, colored_s2 = pylcs.coloring_match_sequence(match_list, s1, s2, 1, 1, "#FFEEAA", "#FFEEAA", t=3)
print(colored_s1, colored_s2)
Note that the colorful output uses ANSI escape codes. Referring to https://en.wikipedia.org/wiki/ANSI_escape_code.
The ANSI codes may not work in win32 command line.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for pylcs-0.1.0-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 29a8595daaaf4c75126964bf77ee4963fc446eb6cb0dc1516feb8e9888c3977d |
|
MD5 | 3184a940d9ec41ea24b17188fdcb2ed4 |
|
BLAKE2b-256 | a608fe55c8ec761d8e74c63b8a8ee94bedd7e9b1c940ba48430c6e7c07bfc216 |
Hashes for pylcs-0.1.0-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 82e56f36a6c43a77890388d3f3c1bdb18d314adc89c306973df843d9c1d2bf72 |
|
MD5 | c760324f6c2cd5d2c7309b85e8d9e76a |
|
BLAKE2b-256 | 1f55082aa2375c04fdb429c555d8fe1fc62ae96de6a91bae2a850edc439ba73d |
Hashes for pylcs-0.1.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 771a107a9da3e3aedc139e8045c104874803cf1ddcf1f0b90af5ba8902363a12 |
|
MD5 | 33fbb4cdff84a478417fabb826845bc0 |
|
BLAKE2b-256 | 1e76ca4dd3302ce6488c45a0c7c06852256e58b3a2c4d945709e7ada03593044 |
Hashes for pylcs-0.1.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2d6195f51bd8c3e22ec8df9acfccac98be66c22e952f26ef11f2952e51a6527 |
|
MD5 | e1299dd312acebab8a59f3069650f9ac |
|
BLAKE2b-256 | 7f19bb8bb4ad5ac7838d8d8011eef48218a5ada81c0b3123fe9cd4201cde0fae |
Hashes for pylcs-0.1.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d54df183f24d7ad0a04a14fe483eabc05bfdc8bf7938b1f06a351588f4ba503 |
|
MD5 | 6f421fcb3ce27df076de388a915fe58b |
|
BLAKE2b-256 | fc648e60c959fa5230f8a3581798a8936d53de517fdf48ba6ff38f946ba7c2b0 |
Hashes for pylcs-0.1.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a8b93bddc00bdbe79be004ebe1eb778a24b44fe386398b0ded0a9324ebaea31a |
|
MD5 | c6fcc129b8be47df4860309aaa3d7bc9 |
|
BLAKE2b-256 | 6327f7cba436783755ed69062a38bc7a1ecdc4d9d43c2da3140f09dc1d390e4d |
Hashes for pylcs-0.1.0-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e8e36988710a7c7998aa78975dc9f4bee19dc68cd699788e314b65bb03c2e8ad |
|
MD5 | 229ed63131588c61744d9a600ca3b452 |
|
BLAKE2b-256 | 4783fe874675164a3c9ef1054af162d9e6636dfd0d6879021aa58a43dc83c411 |