From 2ce85ddc9a9c3c091afff1aa608ac30e413da452 Mon Sep 17 00:00:00 2001 From: jez Date: Wed, 1 Jun 2011 01:22:15 +0000 Subject: [PATCH] Add comments. --- autojump | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/autojump b/autojump index e56400f..f18eb7c 100755 --- a/autojump +++ b/autojump @@ -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