RJ3vans commited on
Commit
fd604bf
·
verified ·
1 Parent(s): 7e540c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -10
app.py CHANGED
@@ -755,6 +755,7 @@ def highlight_tokens_html(tokens, labels):
755
  j += 1
756
  chunk = " ".join(tokens[i:j])
757
  chunk = re.sub(r"\s+([.,!?;:])", r"\1", chunk)
 
758
  esc = html.escape(chunk)
759
  title = html.escape(lab)
760
 
@@ -2296,35 +2297,74 @@ def clean_bpe_text(text):
2296
 
2297
  ###############################################################################
2298
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2299
 
2300
  ###############################################################################
2301
  def clean_output(text):
2302
  if not text:
2303
  return ""
2304
-
2305
  # brackets and WordPiece fragments
2306
  text = re.sub(r'[\[\]]+', '', text)
2307
  text = re.sub(r'\s*##\s*', '', text)
2308
  text = re.sub(r'#', '', text)
2309
-
2310
  text = re.sub(r'\s+', ' ', text).strip()
2311
-
2312
  # normal spacing around punctuation
2313
  text = re.sub(r'\s+([.,!?;:])', r'\1', text)
2314
  text = re.sub(r'([.,!?;:])\s+', r'\1 ', text)
2315
-
2316
  # --- fix the trailing ",." pattern ---
2317
- text = re.sub(r',+\s*\.', '.', text) # ",." or ", ." → "."
2318
- text = re.sub(r',+\s*$', '', text) # trailing commas at end of string
2319
- text = re.sub(r'\.+$', '.', text) # collapse multiple periods
2320
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2321
  text = text.strip()
2322
-
2323
  if text:
2324
  text = text[0].upper() + text[1:]
2325
  if not text.endswith(('.', '!', '?')):
2326
  text += '.'
2327
-
2328
  return text.strip()
2329
 
2330
  ###############################################################################
 
755
  j += 1
756
  chunk = " ".join(tokens[i:j])
757
  chunk = re.sub(r"\s+([.,!?;:])", r"\1", chunk)
758
+ chunk = fix_apostrophe_spacing(chunk)
759
  esc = html.escape(chunk)
760
  title = html.escape(lab)
761
 
 
2297
 
2298
  ###############################################################################
2299
 
2300
+ ###############################################################################
2301
+ def fix_apostrophe_spacing(s):
2302
+ if not s:
2303
+ return s
2304
+ s = s.replace("’", "'")
2305
+
2306
+ # didn't / don't / isn't / can't ...
2307
+ s = re.sub(r"\bn\s+'\s+t\b", "n't", s, flags=re.I)
2308
+ s = re.sub(r"(\w)n\s+'\s+t\b", r"\1n't", s, flags=re.I)
2309
+
2310
+ # she's / it's / he's / I'm / you're / we'll / I've ...
2311
+ s = re.sub(
2312
+ r"(\w)\s+'\s+(s|re|ll|ve|d|m)\b",
2313
+ r"\1'\2",
2314
+ s,
2315
+ flags=re.I,
2316
+ )
2317
+
2318
+ # any leftover spaces touching an apostrophe
2319
+ s = re.sub(r"\s+'\s+", "'", s)
2320
+ s = re.sub(r"(\w)'\s+(\w)", r"\1'\2", s)
2321
+ s = re.sub(r"(\w)\s+'(\w)", r"\1'\2", s)
2322
+
2323
+ return s
2324
+ ###############################################################################
2325
 
2326
  ###############################################################################
2327
  def clean_output(text):
2328
  if not text:
2329
  return ""
2330
+
2331
  # brackets and WordPiece fragments
2332
  text = re.sub(r'[\[\]]+', '', text)
2333
  text = re.sub(r'\s*##\s*', '', text)
2334
  text = re.sub(r'#', '', text)
2335
+
2336
  text = re.sub(r'\s+', ' ', text).strip()
2337
+
2338
  # normal spacing around punctuation
2339
  text = re.sub(r'\s+([.,!?;:])', r'\1', text)
2340
  text = re.sub(r'([.,!?;:])\s+', r'\1 ', text)
2341
+
2342
  # --- fix the trailing ",." pattern ---
2343
+ text = re.sub(r',+\s*\.', '.', text)
2344
+ text = re.sub(r',+\s*$', '', text)
2345
+ text = re.sub(r'\.+$', '.', text)
2346
+
2347
+ # --- spaces around apostrophes (didn't / she's / it's) ---
2348
+ text = text.replace("’", "'")
2349
+ text = re.sub(r"\bn\s+'\s+t\b", "n't", text, flags=re.I)
2350
+ text = re.sub(r"(\w)n\s+'\s+t\b", r"\1n't", text, flags=re.I)
2351
+ text = re.sub(
2352
+ r"(\w)\s+'\s+(s|re|ll|ve|d|m)\b",
2353
+ r"\1'\2",
2354
+ text,
2355
+ flags=re.I,
2356
+ )
2357
+ text = re.sub(r"\s+'\s+", "'", text)
2358
+ text = re.sub(r"(\w)'\s+(\w)", r"\1'\2", text)
2359
+ text = re.sub(r"(\w)\s+'(\w)", r"\1'\2", text)
2360
+
2361
  text = text.strip()
2362
+
2363
  if text:
2364
  text = text[0].upper() + text[1:]
2365
  if not text.endswith(('.', '!', '?')):
2366
  text += '.'
2367
+
2368
  return text.strip()
2369
 
2370
  ###############################################################################