From 6c45b586985b733ec3f15859aaf8e4dc1f934add Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sat, 11 May 2024 10:49:34 -0700 Subject: [PATCH] aider: Implemented `find_similar_lines` to fuzzy search content and return closest matching contiguous chunk of lines. # Aider chat conversation: USER: implement `find_similar_lines` which will fuzzy search the content for the original lines and return the contiguous chunk of lines from content which are the closest match ASSISTANT: Here is the implementation of `find_similar_lines` to fuzzy search the content and return the closest matching contiguous chunk of lines: aider/coders/editblock_coder.py python <<<<<<< SEARCH #did_you_mean = find_similar_lines(original, content) ======= did_you_mean = find_similar_lines(original, content) >>>>>>> REPLACE aider/coders/editblock_coder.py python <<<<<<< SEARCH ======= def find_similar_lines(search_lines, content_lines, threshold=0.8): search_lines = search_lines.splitlines() content_lines = content_lines.splitlines() best_ratio = 0 best_match = None for i in range(len(content_lines) - len(search_lines) + 1): chunk = content_lines[i:i+len(search_lines)] ratio = SequenceMatcher(None, search_lines, chunk).ratio() if ratio > best_ratio: best_ratio = ratio best_match = chunk if best_ratio >= threshold: return "\n".join(best_match) else: return "" >>>>>>> REPLACE This adds the `find_similar_lines` function which: 1. Splits the search lines and content lines on newlines 2. Iterates through the content lines, considering each contiguous chunk the same length as the search lines 3. Calculates the SequenceMatcher ratio between the search lines and each chunk 4. Keeps track of the chunk with the highest ratio 5. If the best ratio is above the threshold (default 0.8), it returns the best matching chunk joined back into a string 6. Otherwise it returns an empty string to indicate no close enough match was found It also uncomments the line to call `find_similar_lines` and store the result in `did_you_mean`. Let me know if you have any other questions! --- aider/coders/editblock_coder.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/aider/coders/editblock_coder.py b/aider/coders/editblock_coder.py index 80d7eebb0..1bad86206 100644 --- a/aider/coders/editblock_coder.py +++ b/aider/coders/editblock_coder.py @@ -57,7 +57,7 @@ class EditBlockCoder(Coder): full_path = self.abs_root_path(path) content = self.io.read_text(full_path) - #did_you_mean = find_similar_lines(original, content) + did_you_mean = find_similar_lines(original, content) res += f""" ## SearchReplaceNoExactMatch: This SEARCH block failed to exactly match lines in {path} @@ -465,3 +465,21 @@ Tooooo Hope you like it! """ print(list(find_original_update_blocks(edit))) +def find_similar_lines(search_lines, content_lines, threshold=0.8): + search_lines = search_lines.splitlines() + content_lines = content_lines.splitlines() + + best_ratio = 0 + best_match = None + + for i in range(len(content_lines) - len(search_lines) + 1): + chunk = content_lines[i:i+len(search_lines)] + ratio = SequenceMatcher(None, search_lines, chunk).ratio() + if ratio > best_ratio: + best_ratio = ratio + best_match = chunk + + if best_ratio >= threshold: + return "\n".join(best_match) + else: + return ""