Welcome to textalignment

Python package textalignment provides functionalities to align texts using the Smith-Waterman algorithm. This is especially usefull if you want to

  • Find names in documents even if they are not correctly spelled
  • Match 2 texts
  • Find relevant sequences of texts in other texts

Example looking up one element in a text

from textalignment import smith_waterman
text = "I am looking for John McNrow, where can I find John McAndRow?"
sw = smith_waterman(text, "John McEnroe", type = 'characters', method = 'biopython', 
                    match = 2, mismatch = -1, gap = -1)
sw
{'type': 'characters', 'method': 'biopython', 'edit_mark': '#', 'weights': {'match': 2, 'mismatch': -1, 'gap': -1}, 'sw': 19.0, 'similarity': 0.7916666666666666, 'matches': 10, 'mismatches': 0, 'a': {'text': 'I am looking for John McNrow, where can I find John McAndRow?', 'tokens': ['I', ' ', 'a', 'm', ' ', 'l', 'o', 'o', 'k', 'i', 'n', 'g', ' ', 'f', 'o', 'r', ' ', 'J', 'o', 'h', 'n', ' ', 'M', 'c', 'N', 'r', 'o', 'w', ',', ' ', 'w', 'h', 'e', 'r', 'e', ' ', 'c', 'a', 'n', ' ', 'I', ' ', 'f', 'i', 'n', 'd', ' ', 'J', 'o', 'h', 'n', ' ', 'M', 'c', 'A', 'n', 'd', 'R', 'o', 'w', '?'], 'n': 61, 'similarity': 0.1557377049180328, 'alignment': {'text': 'John Mc#Nro', 'tokens': ['J', 'o', 'h', 'n', ' ', 'M', 'c', '#', 'N', 'r', 'o'], 'n': 11, 'gaps': 1, 'from': np.int64(17), 'to': np.int64(26), 'similarity': 0.9090909090909091}}, 'b': {'text': 'John McEnroe', 'tokens': ['J', 'o', 'h', 'n', ' ', 'M', 'c', 'E', 'n', 'r', 'o', 'e'], 'n': 12, 'similarity': 0.7916666666666666, 'alignment': {'text': 'John McEnro', 'tokens': ['J', 'o', 'h', 'n', ' ', 'M', 'c', 'E', 'n', 'r', 'o'], 'n': 11, 'gaps': 0, 'from': np.int64(0), 'to': np.int64(10), 'similarity': 0.9090909090909091}}}

Example looking up several elements in a text, possibly recursive

from textalignment import Smith_Waterman
text = "I am looking for John McNrow, where can I find John McAndRow?"
sw = Smith_Waterman(a = text, b = ["John McEnroe", "McEnroe John"])

Optimal alignment

sw.optimal_alignment(type = 'characters', method = 'biopython')
[{'type': 'characters', 'method': 'biopython', 'edit_mark': '#', 'weights': {'match': 2, 'mismatch': -1, 'gap': -1}, 'sw': 19.0, 'similarity': 0.7916666666666666, 'matches': 10, 'mismatches': 0, 'a': {'text': 'I am looking for John McNrow, where can I find John McAndRow?', 'tokens': ['I', ' ', 'a', 'm', ' ', 'l', 'o', 'o', 'k', 'i', 'n', 'g', ' ', 'f', 'o', 'r', ' ', 'J', 'o', 'h', 'n', ' ', 'M', 'c', 'N', 'r', 'o', 'w', ',', ' ', 'w', 'h', 'e', 'r', 'e', ' ', 'c', 'a', 'n', ' ', 'I', ' ', 'f', 'i', 'n', 'd', ' ', 'J', 'o', 'h', 'n', ' ', 'M', 'c', 'A', 'n', 'd', 'R', 'o', 'w', '?'], 'n': 61, 'similarity': 0.1557377049180328, 'alignment': {'text': 'John Mc#Nro', 'tokens': ['J', 'o', 'h', 'n', ' ', 'M', 'c', '#', 'N', 'r', 'o'], 'n': 11, 'gaps': 1, 'from': np.int64(17), 'to': np.int64(26), 'similarity': 0.9090909090909091}}, 'b': {'text': 'John McEnroe', 'tokens': ['J', 'o', 'h', 'n', ' ', 'M', 'c', 'E', 'n', 'r', 'o', 'e'], 'n': 12, 'similarity': 0.7916666666666666, 'alignment': {'text': 'John McEnro', 'tokens': ['J', 'o', 'h', 'n', ' ', 'M', 'c', 'E', 'n', 'r', 'o'], 'n': 11, 'gaps': 0, 'from': np.int64(0), 'to': np.int64(10), 'similarity': 0.9090909090909091}}}, {'type': 'characters', 'method': 'biopython', 'edit_mark': '#', 'weights': {'match': 2, 'mismatch': -1, 'gap': -1}, 'sw': 11.0, 'similarity': 0.4583333333333333, 'matches': 6, 'mismatches': 1, 'a': {'text': 'I am looking for John McNrow, where can I find John McAndRow?', 'tokens': ['I', ' ', 'a', 'm', ' ', 'l', 'o', 'o', 'k', 'i', 'n', 'g', ' ', 'f', 'o', 'r', ' ', 'J', 'o', 'h', 'n', ' ', 'M', 'c', 'N', 'r', 'o', 'w', ',', ' ', 'w', 'h', 'e', 'r', 'e', ' ', 'c', 'a', 'n', ' ', 'I', ' ', 'f', 'i', 'n', 'd', ' ', 'J', 'o', 'h', 'n', ' ', 'M', 'c', 'A', 'n', 'd', 'R', 'o', 'w', '?'], 'n': 61, 'similarity': 0.09016393442622951, 'alignment': {'text': 'or John', 'tokens': ['o', 'r', ' ', 'J', 'o', 'h', 'n'], 'n': 7, 'gaps': 0, 'from': np.int64(14), 'to': np.int64(20), 'similarity': 0.8571428571428571}}, 'b': {'text': 'McEnroe John', 'tokens': ['M', 'c', 'E', 'n', 'r', 'o', 'e', ' ', 'J', 'o', 'h', 'n'], 'n': 12, 'similarity': 0.4583333333333333, 'alignment': {'text': 'oe John', 'tokens': ['o', 'e', ' ', 'J', 'o', 'h', 'n'], 'n': 7, 'gaps': 0, 'from': np.int64(5), 'to': np.int64(11), 'similarity': 0.8571428571428571}}}]
similarities = sw.as_data_frame()

Recursive alignment

sw.recursive_alignment(type = 'characters', method = 'biopython', 
                       which = 'both', threshold = 0.5)
[[{'type': 'characters', 'method': 'biopython', 'edit_mark': '#', 'weights': {'match': 2, 'mismatch': -1, 'gap': -1}, 'sw': 19.0, 'similarity': 0.7916666666666666, 'matches': 10, 'mismatches': 0, 'a': {'text': 'I am looking for John McNrow, where can I find John McAndRow?', 'tokens': ['I', ' ', 'a', 'm', ' ', 'l', 'o', 'o', 'k', 'i', 'n', 'g', ' ', 'f', 'o', 'r', ' ', 'J', 'o', 'h', 'n', ' ', 'M', 'c', 'N', 'r', 'o', 'w', ',', ' ', 'w', 'h', 'e', 'r', 'e', ' ', 'c', 'a', 'n', ' ', 'I', ' ', 'f', 'i', 'n', 'd', ' ', 'J', 'o', 'h', 'n', ' ', 'M', 'c', 'A', 'n', 'd', 'R', 'o', 'w', '?'], 'n': 61, 'similarity': 0.1557377049180328, 'alignment': {'text': 'John Mc#Nro', 'tokens': ['J', 'o', 'h', 'n', ' ', 'M', 'c', '#', 'N', 'r', 'o'], 'n': 11, 'gaps': 1, 'from': np.int64(17), 'to': np.int64(26), 'similarity': 0.9090909090909091}}, 'b': {'text': 'John McEnroe', 'tokens': ['J', 'o', 'h', 'n', ' ', 'M', 'c', 'E', 'n', 'r', 'o', 'e'], 'n': 12, 'similarity': 0.7916666666666666, 'alignment': {'text': 'John McEnro', 'tokens': ['J', 'o', 'h', 'n', ' ', 'M', 'c', 'E', 'n', 'r', 'o'], 'n': 11, 'gaps': 0, 'from': np.int64(0), 'to': np.int64(10), 'similarity': 0.9090909090909091}}}, {'type': 'characters', 'method': 'biopython', 'edit_mark': '#', 'weights': {'match': 2, 'mismatch': -1, 'gap': -1}, 'sw': 18.0, 'similarity': 0.75, 'matches': 10, 'mismatches': 1, 'a': {'text': 'w, where can I find John McAndRow?', 'tokens': ['w', ',', ' ', 'w', 'h', 'e', 'r', 'e', ' ', 'c', 'a', 'n', ' ', 'I', ' ', 'f', 'i', 'n', 'd', ' ', 'J', 'o', 'h', 'n', ' ', 'M', 'c', 'A', 'n', 'd', 'R', 'o', 'w', '?'], 'n': 34, 'similarity': 0.2647058823529412, 'alignment': {'text': 'John McAndRo', 'tokens': ['J', 'o', 'h', 'n', ' ', 'M', 'c', 'A', 'n', 'd', 'R', 'o'], 'n': 12, 'gaps': 0, 'from': np.int64(47), 'to': np.int64(58), 'similarity': 0.8333333333333334}}, 'b': {'text': 'John McEnroe', 'tokens': ['J', 'o', 'h', 'n', ' ', 'M', 'c', 'E', 'n', 'r', 'o', 'e'], 'n': 12, 'similarity': 0.75, 'alignment': {'text': 'John McEn#ro', 'tokens': ['J', 'o', 'h', 'n', ' ', 'M', 'c', 'E', 'n', '#', 'r', 'o'], 'n': 12, 'gaps': 1, 'from': np.int64(0), 'to': np.int64(10), 'similarity': 0.8333333333333334}}}], [{'type': 'characters', 'method': 'biopython', 'edit_mark': '#', 'weights': {'match': 2, 'mismatch': -1, 'gap': -1}, 'sw': 11.0, 'similarity': 0.4583333333333333, 'matches': 6, 'mismatches': 1, 'a': {'text': 'I am looking for John McNrow, where can I find John McAndRow?', 'tokens': ['I', ' ', 'a', 'm', ' ', 'l', 'o', 'o', 'k', 'i', 'n', 'g', ' ', 'f', 'o', 'r', ' ', 'J', 'o', 'h', 'n', ' ', 'M', 'c', 'N', 'r', 'o', 'w', ',', ' ', 'w', 'h', 'e', 'r', 'e', ' ', 'c', 'a', 'n', ' ', 'I', ' ', 'f', 'i', 'n', 'd', ' ', 'J', 'o', 'h', 'n', ' ', 'M', 'c', 'A', 'n', 'd', 'R', 'o', 'w', '?'], 'n': 61, 'similarity': 0.09016393442622951, 'alignment': {'text': 'or John', 'tokens': ['o', 'r', ' ', 'J', 'o', 'h', 'n'], 'n': 7, 'gaps': 0, 'from': np.int64(14), 'to': np.int64(20), 'similarity': 0.8571428571428571}}, 'b': {'text': 'McEnroe John', 'tokens': ['M', 'c', 'E', 'n', 'r', 'o', 'e', ' ', 'J', 'o', 'h', 'n'], 'n': 12, 'similarity': 0.4583333333333333, 'alignment': {'text': 'oe John', 'tokens': ['o', 'e', ' ', 'J', 'o', 'h', 'n'], 'n': 7, 'gaps': 0, 'from': np.int64(5), 'to': np.int64(11), 'similarity': 0.8571428571428571}}}]]
similarities = sw.as_data_frame()
similarities
                                                   a  ...                           b_fromto
0  I am looking for John McNrow, where can I find...  ...  [J, o, h, n,  , M, c, E, n, r, o]
1                 w, where can I find John McAndRow?  ...  [J, o, h, n,  , M, c, E, n, r, o]
2  I am looking for John McNrow, where can I find...  ...              [o, e,  , J, o, h, n]

[3 rows x 20 columns]