1
0
mirror of https://github.com/wting/autojump synced 2024-10-27 20:34:07 +00:00

Add comments.

This commit is contained in:
jez 2011-06-01 01:22:15 +00:00
parent d6a92e4582
commit 2ce85ddc9a

View File

@ -89,6 +89,11 @@ def clean_dict(sorted_dirs, path_dict):
else: return False
def approximatch(pat, text, max_errors):
"""Calculate the Damerau-Levenshtein distance between :pat and :text,
minimized over all possible positions of :pat within :text. As an
optimization, this distance is only accurate if it is <= :max_errors.
Return values greater than :max_errors indicate that the distance is _at
least_ that much. Runs in O(:max_errors * len(:text)) time."""
cols = [list(range(0, len(pat)+1))]
errors = len(pat)
for i in range(0, len(text)): cols.append([errors] * (len(pat) + 1))
@ -108,6 +113,9 @@ def approximatch(pat, text, max_errors):
if i1 and j1:
cols[i+1][j+1] = min(cols[i+1][j+1], 1 + (i - i1) + (j - j1) + cols[i1-1][j1-1])
#Ukkonen's cut-off heuristic. See 'Theoretical and Empirical
#Comparisons of Approximate String Matching Algorithms by Chang and
#Lampe for details.
if j + 1 == len(pat):
errors = min(errors, cols[i+1][j+1])
elif j + 1 == last_active + 1:
@ -146,6 +154,8 @@ def find_matches(dirs, patterns, result_list, ignore_case, approx, max_matches):
bad_match = False
for pattern, match_string in get_pattern_and_match(patterns, path):
errors = approximatch(pattern, match_string, 2)
#If the number of errors are >= than the string length, then a
#match is always possible, so this result is useless.
if errors >= len(pattern) or errors >= len(match_string):
bad_match = True
break