pradachan commited on
Commit
12defd4
·
verified ·
1 Parent(s): e9723c5

fix: diff compares words only (ignore punctuation/casing), strike true deletions only — removes false-positive strikethrough noise

Browse files
Files changed (1) hide show
  1. app.py +42 -44
app.py CHANGED
@@ -132,54 +132,52 @@ def _tokenize(text):
132
  return _TOKEN_RE.findall(text or "")
133
 
134
 
135
- def diff_html(vanilla_text, tuned_text):
136
- """Render the vanilla transcript with the parts the cleaned model REMOVED
137
- shown as red strikethrough with tooltips on hover. Token-level via difflib.SequenceMatcher.
138
-
139
- Comparison is case-insensitive (the tuned model lowercases) so casing
140
- differences don't masquerade as edits; the original-cased vanilla token is
141
- always the text shown to the user.
142
- """
143
- a = _tokenize(vanilla_text)
144
- b = _tokenize(tuned_text)
145
- a_lower = [t.lower() for t in a]
146
- b_lower = [t.lower() for t in b]
147
-
148
- sm = difflib.SequenceMatcher(a=a_lower, b=b_lower, autojunk=False)
149
- out = []
150
 
151
- def _kept(token):
152
- escaped = html.escape(token)
153
- if token.strip() and any(c.isalnum() for c in token):
154
- return f'<span class="fw-token fw-kept" data-tooltip="Kept: present as is">{escaped}</span>'
155
- return escaped
156
 
157
- def _removed(token):
158
- escaped = html.escape(token)
159
- if token.strip() and any(c.isalnum() for c in token):
160
- return f'<span class="fw-token fw-removed" data-tooltip="Removed by clean LoRa">{escaped}</span>'
161
- return f'<span class="fw-removed">{escaped}</span>'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
 
 
 
163
  for tag, i1, i2, j1, j2 in sm.get_opcodes():
164
- if tag == "equal":
165
- out.extend(_kept(t) for t in a[i1:i2])
166
- elif tag == "delete":
167
- out.extend(_removed(t) for t in a[i1:i2])
168
- elif tag == "replace":
169
- # Show the original (vanilla) tokens struck through. The
170
- # replacement appears on the cleaned side already; here we only
171
- # surface what vanilla had and the cleaned model changed/dropped.
172
- out.extend(_removed(t) for t in a[i1:i2])
173
- elif tag == "insert":
174
- # Tokens only in the cleaned output (rare: re-casing / re-spelling).
175
- # Nothing to strike through on the vanilla side; skip.
176
- pass
177
-
178
- # Re-join with spaces, then tighten spaces before punctuation for readability.
179
- joined = " ".join(out)
180
- joined = re.sub(r"\s+([,.;:!?])", r"\1", joined)
181
- joined = re.sub(r"\s+(</span>)", r"\1", joined)
182
- return f'<div class="fw-diff-text">{joined}</div>'
183
 
184
 
185
  # ---------------------------------------------------------------------------
 
132
  return _TOKEN_RE.findall(text or "")
133
 
134
 
135
+ _WORD_RE = re.compile(r"\w+(?:'\w+)*", re.UNICODE) # words, keeping contractions whole
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
 
 
 
 
 
 
137
 
138
+ def diff_html(vanilla_text, tuned_text):
139
+ """Render the vanilla transcript with the words the cleaned model REMOVED
140
+ shown as red strikethrough, and everything else left intact.
141
+
142
+ The cleaned model lowercases its output and strips most punctuation, so a
143
+ raw text diff would flag every comma, capital letter, and full stop as an
144
+ "edit" and bury the actual disfluency deletions in noise (this is exactly
145
+ what made the diff look broken). To avoid that:
146
+
147
+ * Comparison runs on lowercased *word* tokens only; punctuation and casing
148
+ are ignored, so they never count as edits.
149
+ * The display preserves the original vanilla text verbatim (its casing,
150
+ punctuation, and spacing). Only whole words are ever struck through.
151
+ * Only true deletions are struck. Word substitutions (an ASR variant like
152
+ "mum" vs "mom", or an acronym respelled "CNN" vs "c n n") are left alone,
153
+ because those are not disfluency removals and would only confuse a viewer.
154
+ """
155
+ vanilla_text = vanilla_text or ""
156
+ a_matches = list(_WORD_RE.finditer(vanilla_text))
157
+ a_words = [m.group(0).lower() for m in a_matches]
158
+ b_words = [m.group(0).lower() for m in _WORD_RE.finditer(tuned_text or "")]
159
 
160
+ sm = difflib.SequenceMatcher(a=a_words, b=b_words, autojunk=False)
161
+ removed = set()
162
  for tag, i1, i2, j1, j2 in sm.get_opcodes():
163
+ if tag == "delete": # only words dropped outright count as removed
164
+ removed.update(range(i1, i2))
165
+
166
+ out = []
167
+ cursor = 0
168
+ for i, m in enumerate(a_matches):
169
+ # Inter-word text (spaces + punctuation) is passed through untouched.
170
+ out.append(html.escape(vanilla_text[cursor:m.start()]))
171
+ word = html.escape(m.group(0))
172
+ if i in removed:
173
+ out.append(
174
+ f'<span class="fw-token fw-removed" data-tooltip="Removed by the cleaning LoRA">{word}</span>'
175
+ )
176
+ else:
177
+ out.append(f'<span class="fw-token fw-kept" data-tooltip="Kept">{word}</span>')
178
+ cursor = m.end()
179
+ out.append(html.escape(vanilla_text[cursor:]))
180
+ return f'<div class="fw-diff-text">{"".join(out)}</div>'
 
181
 
182
 
183
  # ---------------------------------------------------------------------------