Spaces:
Running
Running
Chae
commited on
Commit
·
ae55733
1
Parent(s):
a187d8f
fix: parse diffs by word
Browse files- inplace_chat.py +13 -9
inplace_chat.py
CHANGED
|
@@ -60,15 +60,17 @@ def summarize_edit(old: str, new: str) -> tuple[str, str]:
|
|
| 60 |
Produce coarse 'removed_text' and 'edited_text' by joining all deletions and insertions.
|
| 61 |
Good enough for a single bullet like: - Replaced {removed_text} with {edited_text}
|
| 62 |
"""
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
removed_chunks, added_chunks = [], []
|
| 65 |
for tag, i1, i2, j1, j2 in sm.get_opcodes():
|
| 66 |
if tag in ("delete", "replace"):
|
| 67 |
-
chunk =
|
| 68 |
if chunk:
|
| 69 |
removed_chunks.append(chunk)
|
| 70 |
if tag in ("insert", "replace"):
|
| 71 |
-
chunk =
|
| 72 |
if chunk:
|
| 73 |
added_chunks.append(chunk)
|
| 74 |
removed_text = " / ".join(removed_chunks) if removed_chunks else "(none)"
|
|
@@ -80,18 +82,20 @@ def get_detailed_diff(old: str, new: str) -> list[dict]:
|
|
| 80 |
Returns a list of diff chunks with tags: 'equal', 'delete', 'insert', 'replace'
|
| 81 |
Each chunk has: {'tag': str, 'text': str}
|
| 82 |
"""
|
| 83 |
-
|
|
|
|
|
|
|
| 84 |
diff_chunks = []
|
| 85 |
for tag, i1, i2, j1, j2 in sm.get_opcodes():
|
| 86 |
if tag == 'equal':
|
| 87 |
-
diff_chunks.append({'tag': 'equal', 'text':
|
| 88 |
elif tag == 'delete':
|
| 89 |
-
diff_chunks.append({'tag': 'delete', 'text':
|
| 90 |
elif tag == 'insert':
|
| 91 |
-
diff_chunks.append({'tag': 'insert', 'text':
|
| 92 |
elif tag == 'replace':
|
| 93 |
-
diff_chunks.append({'tag': 'delete', 'text':
|
| 94 |
-
diff_chunks.append({'tag': 'insert', 'text':
|
| 95 |
return diff_chunks
|
| 96 |
|
| 97 |
# === Render current conversation ===
|
|
|
|
| 60 |
Produce coarse 'removed_text' and 'edited_text' by joining all deletions and insertions.
|
| 61 |
Good enough for a single bullet like: - Replaced {removed_text} with {edited_text}
|
| 62 |
"""
|
| 63 |
+
old_words = old.split()
|
| 64 |
+
new_words = new.split()
|
| 65 |
+
sm = difflib.SequenceMatcher(a=old_words, b=new_words)
|
| 66 |
removed_chunks, added_chunks = [], []
|
| 67 |
for tag, i1, i2, j1, j2 in sm.get_opcodes():
|
| 68 |
if tag in ("delete", "replace"):
|
| 69 |
+
chunk = " ".join(old_words[i1:i2]).strip()
|
| 70 |
if chunk:
|
| 71 |
removed_chunks.append(chunk)
|
| 72 |
if tag in ("insert", "replace"):
|
| 73 |
+
chunk = " ".join(new_words[j1:j2]).strip()
|
| 74 |
if chunk:
|
| 75 |
added_chunks.append(chunk)
|
| 76 |
removed_text = " / ".join(removed_chunks) if removed_chunks else "(none)"
|
|
|
|
| 82 |
Returns a list of diff chunks with tags: 'equal', 'delete', 'insert', 'replace'
|
| 83 |
Each chunk has: {'tag': str, 'text': str}
|
| 84 |
"""
|
| 85 |
+
old_words = old.split()
|
| 86 |
+
new_words = new.split()
|
| 87 |
+
sm = difflib.SequenceMatcher(a=old_words, b=new_words)
|
| 88 |
diff_chunks = []
|
| 89 |
for tag, i1, i2, j1, j2 in sm.get_opcodes():
|
| 90 |
if tag == 'equal':
|
| 91 |
+
diff_chunks.append({'tag': 'equal', 'text': ' '.join(old_words[i1:i2])})
|
| 92 |
elif tag == 'delete':
|
| 93 |
+
diff_chunks.append({'tag': 'delete', 'text': ' '.join(old_words[i1:i2])})
|
| 94 |
elif tag == 'insert':
|
| 95 |
+
diff_chunks.append({'tag': 'insert', 'text': ' '.join(new_words[j1:j2])})
|
| 96 |
elif tag == 'replace':
|
| 97 |
+
diff_chunks.append({'tag': 'delete', 'text': ' '.join(old_words[i1:i2])})
|
| 98 |
+
diff_chunks.append({'tag': 'insert', 'text': ' '.join(new_words[j1:j2])})
|
| 99 |
return diff_chunks
|
| 100 |
|
| 101 |
# === Render current conversation ===
|