|
def levenshtein_with_wildcards(str1, str2, wildcard='?', verbose=False): |
|
""" |
|
Calculate the Levenshtein distance between two strings with support for wildcards. |
|
|
|
Args: |
|
str1 (str): The first string. |
|
str2 (str): The second string. |
|
wildcard (str, optional): The wildcard character. Defaults to '?'. |
|
verbose (bool, optional): If True, prints the DP matrix and explains the process. |
|
|
|
Returns: |
|
int: The Levenshtein distance between the two strings. |
|
list: If verbose=True, also returns a list of operations performed. |
|
""" |
|
m, n = len(str1), len(str2) |
|
|
|
|
|
dp = [[0] * (n + 1) for _ in range(m + 1)] |
|
|
|
|
|
for i in range(m + 1): |
|
dp[i][0] = i |
|
|
|
for j in range(n + 1): |
|
dp[0][j] = j |
|
|
|
|
|
for i in range(1, m + 1): |
|
for j in range(1, n + 1): |
|
|
|
if str1[i - 1] == wildcard or str2[j - 1] == wildcard: |
|
dp[i][j] = dp[i - 1][j - 1] |
|
else: |
|
cost = 0 if str1[i - 1] == str2[j - 1] else 1 |
|
dp[i][j] = min( |
|
dp[i - 1][j] + 1, |
|
dp[i][j - 1] + 1, |
|
dp[i - 1][j - 1] + cost |
|
) |
|
|
|
if verbose: |
|
operations = explain_match(str1, str2, dp, wildcard) |
|
return dp[m][n], operations |
|
|
|
return dp[m][n] |
|
|
|
def explain_match(str1, str2, dp, wildcard='?'): |
|
""" |
|
Traces the optimal alignment path and explains each step of the matching process. |
|
|
|
Args: |
|
str1 (str): The first string. |
|
str2 (str): The second string. |
|
dp (list): The dynamic programming matrix. |
|
wildcard (str, optional): The wildcard character. Defaults to '?'. |
|
|
|
Returns: |
|
list: A list of explanation strings for each operation performed. |
|
""" |
|
m, n = len(str1), len(str2) |
|
operations = [] |
|
|
|
|
|
i, j = m, n |
|
path = [] |
|
|
|
while i > 0 or j > 0: |
|
path.append((i, j)) |
|
|
|
if i == 0: |
|
j -= 1 |
|
elif j == 0: |
|
i -= 1 |
|
else: |
|
substitution_cost = dp[i-1][j-1] |
|
deletion_cost = dp[i-1][j] |
|
insertion_cost = dp[i][j-1] |
|
|
|
min_cost = min(substitution_cost, deletion_cost, insertion_cost) |
|
|
|
if min_cost == substitution_cost: |
|
i -= 1 |
|
j -= 1 |
|
elif min_cost == deletion_cost: |
|
i -= 1 |
|
else: |
|
j -= 1 |
|
|
|
path.append((0, 0)) |
|
path.reverse() |
|
|
|
|
|
for idx in range(1, len(path)): |
|
prev_i, prev_j = path[idx-1] |
|
curr_i, curr_j = path[idx] |
|
|
|
|
|
if curr_i > prev_i and curr_j > prev_j: |
|
char1 = str1[curr_i-1] |
|
char2 = str2[curr_j-1] |
|
|
|
if char1 == wildcard or char2 == wildcard: |
|
wildcard_char = char1 if char1 == wildcard else char2 |
|
match_char = char2 if char1 == wildcard else char1 |
|
operations.append(f"Wildcard match: '{wildcard_char}' matches any character, here '{match_char}'") |
|
elif char1 == char2: |
|
operations.append(f"Match: '{char1}' matches '{char2}'") |
|
else: |
|
operations.append(f"Substitution: Replace '{char1}' with '{char2}'") |
|
|
|
|
|
elif curr_i == prev_i and curr_j > prev_j: |
|
operations.append(f"Insertion: Insert '{str2[curr_j-1]}'") |
|
|
|
|
|
elif curr_i > prev_i and curr_j == prev_j: |
|
operations.append(f"Deletion: Delete '{str1[curr_i-1]}'") |
|
|
|
return operations |
|
|
|
def print_match_summary(str1, str2, wildcard='?'): |
|
""" |
|
Prints a summary of the match between two strings, highlighting wildcards. |
|
|
|
Args: |
|
str1 (str): The first string. |
|
str2 (str): The second string. |
|
wildcard (str, optional): The wildcard character. Defaults to '?'. |
|
""" |
|
distance, operations = levenshtein_with_wildcards(str1, str2, wildcard, verbose=True) |
|
|
|
print(f"Comparing '{str1}' and '{str2}' (wildcard: '{wildcard}')") |
|
print(f"Edit distance: {distance}") |
|
print("\nMatch process:") |
|
|
|
for i, op in enumerate(operations): |
|
print(f"Step {i+1}: {op}") |
|
|
|
|
|
alignment = [] |
|
i, j = 0, 0 |
|
aligned_str1 = "" |
|
aligned_str2 = "" |
|
match_indicators = "" |
|
|
|
for op in operations: |
|
if "match" in op or "Match" in op or "Substitution" in op: |
|
aligned_str1 += str1[i] |
|
aligned_str2 += str2[j] |
|
|
|
if "Wildcard" in op: |
|
match_indicators += "*" |
|
elif "Match" in op: |
|
match_indicators += "|" |
|
else: |
|
match_indicators += "X" |
|
|
|
i += 1 |
|
j += 1 |
|
elif "Insertion" in op: |
|
aligned_str1 += "-" |
|
aligned_str2 += str2[j] |
|
match_indicators += " " |
|
j += 1 |
|
elif "Deletion" in op: |
|
aligned_str1 += str1[i] |
|
aligned_str2 += "-" |
|
match_indicators += " " |
|
i += 1 |
|
|
|
print("\nAlignment:") |
|
print(aligned_str1) |
|
print(match_indicators) |
|
print(aligned_str2) |
|
print("\nLegend:") |
|
print("| = exact match, * = wildcard match, X = substitution, - = gap (insertion/deletion)") |
|
|
|
|
|
wildcard_matches = [op for op in operations if "Wildcard" in op] |
|
if wildcard_matches: |
|
print("\nWildcard matches:") |
|
for match in wildcard_matches: |
|
print(f"- {match}") |
|
|
|
return distance, operations |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
print_match_summary("hello", "hello") |
|
print_match_summary("hello", "hallo") |
|
print_match_summary("he?lo", "hello") |
|
print_match_summary("he?lo", "hallo") |
|
print_match_summary("h?llo", "hello") |
|
print_match_summary("h?llo", "hillo") |
|
print_match_summary("c?t", "cat") |
|
print_match_summary("c?t", "cut") |
|
print_match_summary("w?rd", "word") |
|
print_match_summary("d?g", "dog") |