From d02506db169291c60cb68ab3b8a6dd3e1e82ec3b Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sat, 11 May 2024 10:43:40 -0700 Subject: [PATCH] aider: Implemented `find_similar_text` function to fuzzy search the content for the closest match to the original text. # Aider chat conversation: USER: I did `git reset --hard HEAD~1` to discard the last edits. Please wait for further instructions before attempting that change again. Feel free to ask relevant questions about why the changes were reverted. ASSISTANT: I understand you reverted the previous changes. Before I attempt to implement the `find_similar_lines` function again, I have a few questions to ensure I follow your guidelines correctly: 1. When proposing changes, should I only modify the `aider/coders/editblock_coder.py` file since that is the only one you have explicitly added to the chat ^C KeyboardInterrupt USER: implement `find_similar_text` which will fuzzy search the content for the original text and return the part of content which is the closest match ASSISTANT: Here is the implementation of `find_similar_text` to fuzzy search the content for the closest match to the original text: aider/coders/editblock_coder.py python <<<<<<< SEARCH ======= from difflib import SequenceMatcher def find_similar_text(original, content): content_len = len(content) original_len = len(original) best_ratio = 0 best_match = None for i in range(content_len - original_len + 1): chunk = content[i:i+original_len] ratio = SequenceMatcher(None, original, chunk).ratio() if ratio > best_ratio: best_ratio = ratio best_match = chunk return best_match >>>>>>> REPLACE This function: 1. Iterates through the content, considering each chunk of text the same length as the original 2. Calculates the SequenceMatcher ratio between the original text and each chunk 3. Keeps track of the chunk with the highest ratio 4. Returns the chunk of content that best matches the original text It will return the portion of `content` that is the closest fuzzy match to `original`. Let me know if you have any other questions! --- aider/coders/editblock_coder.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/aider/coders/editblock_coder.py b/aider/coders/editblock_coder.py index b16fd3c84..9e8ea266b 100644 --- a/aider/coders/editblock_coder.py +++ b/aider/coders/editblock_coder.py @@ -464,3 +464,21 @@ Tooooo Hope you like it! """ print(list(find_original_update_blocks(edit))) +from difflib import SequenceMatcher + +def find_similar_text(original, content): + content_len = len(content) + original_len = len(original) + + best_ratio = 0 + best_match = None + + for i in range(content_len - original_len + 1): + chunk = content[i:i+original_len] + ratio = SequenceMatcher(None, original, chunk).ratio() + + if ratio > best_ratio: + best_ratio = ratio + best_match = chunk + + return best_match