RJ3vans commited on
Commit
5b260fa
Β·
verified Β·
1 Parent(s): 1831f1f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +119 -112
app.py CHANGED
@@ -863,6 +863,14 @@ def highlight_final_output(final_text):
863
  return '<div class="simp-out">' + "".join(para_html) + "</div>"
864
  ###############################################################################
865
 
 
 
 
 
 
 
 
 
866
  ###############################################################################
867
  def tag_SSCCV_span(sentence):
868
  if re.search(r"\[\:\]\s*$", sentence):
@@ -1080,6 +1088,14 @@ def tag_CCV_span(sentence):
1080
  if not simplifiable:
1081
  True # placeholder for future logic
1082
 
 
 
 
 
 
 
 
 
1083
  return (
1084
  clean_tokens,
1085
  clean_pos_tags,
@@ -1156,6 +1172,14 @@ def tag_CMV1_span(sentence):
1156
  if not simplifiable:
1157
  True # placeholder for future logic
1158
 
 
 
 
 
 
 
 
 
1159
  return (
1160
  clean_tokens,
1161
  clean_pos_tags,
@@ -1231,6 +1255,14 @@ def tag_CMN1_span(sentence):
1231
  if not simplifiable:
1232
  True # placeholder for future logic
1233
 
 
 
 
 
 
 
 
 
1234
  return (
1235
  clean_tokens,
1236
  clean_pos_tags,
@@ -1307,6 +1339,14 @@ def tag_CLN_span(sentence):
1307
  if not simplifiable:
1308
  True # placeholder for future logic
1309
 
 
 
 
 
 
 
 
 
1310
  return (
1311
  clean_tokens,
1312
  clean_pos_tags,
@@ -2007,10 +2047,18 @@ def fix_subject_verb_agreement(sent: str) -> str:
2007
  return True
2008
  if t.lemma_ == "be" and t.lower_ in {"be", "bes", "beses"}:
2009
  return True
 
 
 
 
 
2010
  return False
2011
-
2012
  def to_3sg(word: str) -> str:
2013
  low = word.lower()
 
 
 
2014
  irregular = {
2015
  "are": "is",
2016
  "am": "is",
@@ -2022,7 +2070,7 @@ def fix_subject_verb_agreement(sent: str) -> str:
2022
  }
2023
  if low in irregular:
2024
  new = irregular[low]
2025
- elif low.endswith(("s", "sh", "ch", "x", "z", "o")):
2026
  new = low + "es"
2027
  elif low.endswith("y") and len(low) > 1 and low[-2] not in "aeiou":
2028
  new = low[:-1] + "ies"
@@ -2032,6 +2080,7 @@ def fix_subject_verb_agreement(sent: str) -> str:
2032
  new = new[0].upper() + new[1:]
2033
  return new
2034
 
 
2035
  target = None
2036
  for t in doc:
2037
  if skip_target(t):
@@ -2053,7 +2102,8 @@ def fix_subject_verb_agreement(sent: str) -> str:
2053
  out = sent
2054
  if target is not None and target.lower_ not in {"is", "was", "has", "does", "be"}:
2055
  new = to_3sg(target.text)
2056
- out = sent[: target.idx] + new + sent[target.idx + len(target.text) :]
 
2057
 
2058
  # Safety net: if a 3sg form still sits after do-support / a modal,
2059
  # put it back to the lemma (wantsβ†’want, hasβ†’have, goesβ†’go).
@@ -2095,8 +2145,7 @@ def CompoundSimplify(xml_sent):
2095
  sent2 = BEFORE + AFTERSIGN + AFTER
2096
  The coordinator (IN_COMPOUND_CONSTITUENT) is discarded.
2097
 
2098
- Recovery: if AFTERSIGN is empty but IN_COMPOUND contains tokens
2099
- after the coordinator, treat those tokens as AFTERSIGN.
2100
  """
2101
  try:
2102
  def get_text(elem):
@@ -2105,64 +2154,28 @@ def CompoundSimplify(xml_sent):
2105
  parts = []
2106
  for w in elem.findall("W"):
2107
  if w.text:
2108
- t = str(w.text).strip()
2109
- t = re.sub(r"[\[\]]+", "", t)
2110
  if t:
2111
  parts.append(t)
2112
  return " ".join(parts).strip()
2113
 
2114
- def get_tokens(elem):
2115
- if elem is None:
2116
- return []
2117
- parts = []
2118
- for w in elem.findall("W"):
2119
- if w.text:
2120
- t = str(w.text).strip()
2121
- t = re.sub(r"[\[\]]+", "", t)
2122
- if t:
2123
- parts.append(t)
2124
- return parts
2125
-
2126
- before_elem = xml_sent.find(".//BEFORE_COMPOUND_CONSTITUENT")
2127
- beforesign_elem = xml_sent.find(".//IN_COMPOUND_CONSTITUENT_BEFORESIGN")
2128
- sign_elem = xml_sent.find(".//IN_COMPOUND_CONSTITUENT")
2129
- aftersign_elem = xml_sent.find(".//IN_COMPOUND_CONSTITUENT_AFTERSIGN")
2130
- after_elem = xml_sent.find(".//AFTER_COMPOUND_CONSTITUENT")
2131
-
2132
- before_text = get_text(before_elem)
2133
- beforesign_text = get_text(beforesign_elem)
2134
- aftersign_text = get_text(aftersign_elem)
2135
- after_text = get_text(after_elem)
2136
-
2137
- # ----- recovery: second clause folded into IN_COMPOUND -----
2138
- if not aftersign_text and sign_elem is not None:
2139
- sign_toks = get_tokens(sign_elem)
2140
- coord = {",", ";", ":", "and", "but", "or", "nor"}
2141
- i = 0
2142
- while i < len(sign_toks) and sign_toks[i].lower() in coord:
2143
- i += 1
2144
- if i < len(sign_toks):
2145
- aftersign_text = " ".join(sign_toks[i:]).strip()
2146
- # -----------------------------------------------------------
2147
-
2148
- sent1 = " ".join(
2149
- x for x in [before_text, beforesign_text, after_text] if x
2150
- ).strip()
2151
- sent2 = " ".join(
2152
- x for x in [before_text, aftersign_text, after_text] if x
2153
- ).strip()
2154
 
 
 
 
 
2155
 
2156
  def tidy(s):
2157
  if not s:
2158
  return ""
2159
- s = str(s)
2160
- s = re.sub(r"\s+", " ", s).strip()
2161
  s = re.sub(r"\s+([.,!?;:])", r"\1", s)
2162
  s = re.sub(r"([.,!?;:])\s+", r"\1 ", s)
2163
- # decimals: 8 . 2 β†’ 8.2
2164
  s = re.sub(r"(\d)\s*\.\s*(\d)", r"\1.\2", s)
2165
- # list debris only at the end / before final period
2166
  s = re.sub(r"[,;:]+\s*\.", ".", s)
2167
  s = re.sub(r"[,;:]+\s*$", "", s)
2168
  s = s.strip()
@@ -2172,10 +2185,8 @@ def CompoundSimplify(xml_sent):
2172
  s = s[0].upper() + s[1:]
2173
  return s
2174
 
2175
-
2176
-
2177
- sent1 = tidy(sent1)
2178
- sent2 = tidy(sent2)
2179
 
2180
  result = []
2181
  if sent1:
@@ -2188,8 +2199,7 @@ def CompoundSimplify(xml_sent):
2188
 
2189
  except Exception as e:
2190
  print(f"CompoundSimplify error: {e}")
2191
- return []
2192
-
2193
  ###############################################################################
2194
 
2195
 
@@ -2534,33 +2544,35 @@ def drop_bracket_tokens(tokens, pos, lemmas, preds):
2534
  ###############################################################################
2535
 
2536
  ###############################################################################
 
 
 
 
2537
  def build_single_sign_bracketed(tagged_list, mode="ccv", skip_indices=None):
2538
  """
2539
- mode = "ccv" β†’ bracket only the rightmost CCV sign
2540
- mode = "cmv1" β†’ bracket only the rightmost CMV1 sign
2541
- mode = "cmn1" β†’ bracket only the rightmost CMN1 sign
2542
- mode = "cln" β†’ bracket only the rightmost CLN sign
2543
- mode = "ssccv" β†’ bracket one relative SSCCV trigger:
2544
- - nested relatives (2+ opens before next ESCCV): innermost
2545
- - coordinated relatives (CCV between opens): leftmost
2546
- - otherwise: leftmost relative trigger
2547
- - fallback: leftmost SSCCV/ESCCV (e.g. bare "that")
2548
- mode = "ssmn" β†’ bracket only the leftmost SSMN sign
2549
-
2550
- skip_indices: token offsets already tried and rejected
2551
- (e.g. SSCCV signs whose span is a cognitive/communicative VP).
2552
  Returns None if nothing remains to bracket for this mode.
2553
  """
2554
-
2555
  skip_indices = set(skip_indices or [])
2556
 
2557
  def surface_of(item):
2558
- if item.startswith("<M:Y_") and ">" in item:
2559
- start = item.find(">") + 1
2560
- end = item.rfind("<")
 
2561
  if end > start:
2562
- return item[start:end]
2563
- return item
 
 
 
2564
 
2565
  ccv_indices = []
2566
  cmv1_indices = []
@@ -2574,13 +2586,15 @@ def build_single_sign_bracketed(tagged_list, mode="ccv", skip_indices=None):
2574
  if i in skip_indices:
2575
  continue
2576
  s = str(item)
2577
- if "<M:Y_CCV>" in s or "M:Y_CCV" in s:
 
 
2578
  ccv_indices.append(i)
2579
- if "<M:Y_CMV1>" in s or "M:Y_CMV1" in s:
2580
  cmv1_indices.append(i)
2581
- if "<M:Y_CMN1>" in s or "M:Y_CMN1" in s:
2582
  cmn1_indices.append(i)
2583
- if "<M:Y_CLN>" in s or "M:Y_CLN" in s:
2584
  cln_indices.append(i)
2585
  if "M:Y_SSCCV" in s:
2586
  ssccv_indices.append(i)
@@ -2593,37 +2607,32 @@ def build_single_sign_bracketed(tagged_list, mode="ccv", skip_indices=None):
2593
 
2594
  target = set()
2595
 
 
2596
  if mode == "ccv" and ccv_indices:
2597
- target.add(ccv_indices[-1])
2598
-
2599
  elif mode == "cmv1" and cmv1_indices:
2600
- target.add(cmv1_indices[-1])
2601
-
2602
  elif mode == "cmn1" and cmn1_indices:
2603
- target.add(cmn1_indices[-1])
2604
-
2605
  elif mode == "cln" and cln_indices:
2606
- target.add(cln_indices[-1])
2607
-
2608
  elif mode == "ssmn" and ssmn_indices:
2609
  target.add(ssmn_indices[0])
2610
 
2611
  elif mode == "ssccv" and ssccv_indices:
2612
- relative_pronouns = {"who", "which", "whom", "whose"}
2613
  preferred = []
2614
-
2615
  for i in ssccv_indices:
2616
  surface = surface_of(tagged_list[i]).strip().lower()
2617
 
2618
- if surface in relative_pronouns:
2619
  preferred.append(i)
2620
  continue
2621
 
 
2622
  if surface in {",", ";", ":"} and i + 1 < len(tagged_list):
2623
  nxt = surface_of(tagged_list[i + 1]).strip().lower()
2624
- if nxt in relative_pronouns or nxt == "that":
2625
  preferred.append(i)
2626
- continue
2627
 
2628
  if preferred:
2629
  nested = False
@@ -2649,42 +2658,40 @@ def build_single_sign_bracketed(tagged_list, mode="ccv", skip_indices=None):
2649
  if not nested and not coordinated:
2650
  target.add(preferred[0])
2651
  else:
2652
- pure_ssccv = [
 
2653
  i
2654
- for i, item in enumerate(tagged_list)
2655
- if i not in skip_indices and "M:Y_SSCCV" in str(item)
2656
  ]
2657
- pool = pure_ssccv or ssccv_indices
2658
- if pool:
2659
- target.add(pool[0])
2660
 
2661
  if not target:
2662
  return None
2663
 
2664
  parts = []
2665
  for i, item in enumerate(tagged_list):
2666
- if item.startswith("<M:Y_") and item.endswith(">"):
2667
- start = item.find(">") + 1
2668
- end = item.rfind("<")
2669
- surface = item[start:end] if end > start else item
 
2670
  if i in target:
2671
- parts.append(f"[{surface}]")
 
2672
  else:
2673
  parts.append(surface)
2674
  else:
2675
- parts.append(item)
2676
 
2677
  text = " ".join(parts)
2678
-
2679
- text = re.sub(r"\s+\[([,;:])", r"[\1", text)
2680
- text = re.sub(r"(?<!\[)\[([A-Za-z])", r" [\1", text)
2681
- text = re.sub(r"\[\s+", "[", text)
2682
- text = re.sub(r"\s+\]", "]", text)
2683
  text = re.sub(r"\s+", " ", text).strip()
2684
 
 
2685
  text = re.sub(
2686
- r"\[([,;:])\]\s+(who|which|that|whom|whose)\b",
2687
- r"[\1 \2]",
2688
  text,
2689
  flags=re.I,
2690
  )
 
863
  return '<div class="simp-out">' + "".join(para_html) + "</div>"
864
  ###############################################################################
865
 
866
+ ###############################################################################
867
+ def is_contiguous(predictions, wanted) -> bool:
868
+ idxs = [i for i, p in enumerate(predictions) if p == wanted]
869
+ if not idxs:
870
+ return False
871
+ return idxs == list(range(idxs[0], idxs[-1] + 1))
872
+ ###############################################################################
873
+
874
  ###############################################################################
875
  def tag_SSCCV_span(sentence):
876
  if re.search(r"\[\:\]\s*$", sentence):
 
1088
  if not simplifiable:
1089
  True # placeholder for future logic
1090
 
1091
+ if any(p == "ERROR" for p in clean_predictions):
1092
+ return []
1093
+
1094
+ if not is_contiguous(clean_predictions, "IN_COMPOUND_BEFORESIGN"):
1095
+ return []
1096
+ if not is_contiguous(clean_predictions, "IN_COMPOUND_AFTERSIGN"):
1097
+ return []
1098
+
1099
  return (
1100
  clean_tokens,
1101
  clean_pos_tags,
 
1172
  if not simplifiable:
1173
  True # placeholder for future logic
1174
 
1175
+ if any(p == "ERROR" for p in clean_predictions):
1176
+ return []
1177
+
1178
+ if not is_contiguous(clean_predictions, "IN_COMPOUND_BEFORESIGN"):
1179
+ return []
1180
+ if not is_contiguous(clean_predictions, "IN_COMPOUND_AFTERSIGN"):
1181
+ return []
1182
+
1183
  return (
1184
  clean_tokens,
1185
  clean_pos_tags,
 
1255
  if not simplifiable:
1256
  True # placeholder for future logic
1257
 
1258
+ if any(p == "ERROR" for p in clean_predictions):
1259
+ return []
1260
+
1261
+ if not is_contiguous(clean_predictions, "IN_COMPOUND_BEFORESIGN"):
1262
+ return []
1263
+ if not is_contiguous(clean_predictions, "IN_COMPOUND_AFTERSIGN"):
1264
+ return []
1265
+
1266
  return (
1267
  clean_tokens,
1268
  clean_pos_tags,
 
1339
  if not simplifiable:
1340
  True # placeholder for future logic
1341
 
1342
+ if any(p == "ERROR" for p in clean_predictions):
1343
+ return []
1344
+
1345
+ if not is_contiguous(clean_predictions, "IN_COMPOUND_BEFORESIGN"):
1346
+ return []
1347
+ if not is_contiguous(clean_predictions, "IN_COMPOUND_AFTERSIGN"):
1348
+ return []
1349
+
1350
  return (
1351
  clean_tokens,
1352
  clean_pos_tags,
 
2047
  return True
2048
  if t.lemma_ == "be" and t.lower_ in {"be", "bes", "beses"}:
2049
  return True
2050
+ # already 3sg, or already ends in -s: do not add another
2051
+ if t.tag_ == "VBZ":
2052
+ return True
2053
+ if t.lower_.endswith(("s", "es")):
2054
+ return True
2055
  return False
2056
+
2057
  def to_3sg(word: str) -> str:
2058
  low = word.lower()
2059
+ # never stack an ending onto an already-inflected form
2060
+ if low.endswith(("s", "es")) or low in {"is", "was", "has", "does"}:
2061
+ return word
2062
  irregular = {
2063
  "are": "is",
2064
  "am": "is",
 
2070
  }
2071
  if low in irregular:
2072
  new = irregular[low]
2073
+ elif low.endswith(("sh", "ch", "x", "z", "o")):
2074
  new = low + "es"
2075
  elif low.endswith("y") and len(low) > 1 and low[-2] not in "aeiou":
2076
  new = low[:-1] + "ies"
 
2080
  new = new[0].upper() + new[1:]
2081
  return new
2082
 
2083
+
2084
  target = None
2085
  for t in doc:
2086
  if skip_target(t):
 
2102
  out = sent
2103
  if target is not None and target.lower_ not in {"is", "was", "has", "does", "be"}:
2104
  new = to_3sg(target.text)
2105
+ if new != target.text:
2106
+ out = sent[: target.idx] + new + sent[target.idx + len(target.text) :]
2107
 
2108
  # Safety net: if a 3sg form still sits after do-support / a modal,
2109
  # put it back to the lemma (wantsβ†’want, hasβ†’have, goesβ†’go).
 
2145
  sent2 = BEFORE + AFTERSIGN + AFTER
2146
  The coordinator (IN_COMPOUND_CONSTITUENT) is discarded.
2147
 
2148
+ Returns [] unless both sides of the coordinator are non-empty.
 
2149
  """
2150
  try:
2151
  def get_text(elem):
 
2154
  parts = []
2155
  for w in elem.findall("W"):
2156
  if w.text:
2157
+ t = re.sub(r"[\[\]]+", "", str(w.text).strip())
 
2158
  if t:
2159
  parts.append(t)
2160
  return " ".join(parts).strip()
2161
 
2162
+ before_text = get_text(xml_sent.find(".//BEFORE_COMPOUND_CONSTITUENT"))
2163
+ beforesign_text = get_text(xml_sent.find(".//IN_COMPOUND_CONSTITUENT_BEFORESIGN"))
2164
+ aftersign_text = get_text(xml_sent.find(".//IN_COMPOUND_CONSTITUENT_AFTERSIGN"))
2165
+ after_text = get_text(xml_sent.find(".//AFTER_COMPOUND_CONSTITUENT"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2166
 
2167
+ # both conjuncts required β€” do not invent one from a comma
2168
+ if not beforesign_text or not aftersign_text:
2169
+ print("CompoundSimplify: empty conjunct, skipping")
2170
+ return []
2171
 
2172
  def tidy(s):
2173
  if not s:
2174
  return ""
2175
+ s = re.sub(r"\s+", " ", str(s)).strip()
 
2176
  s = re.sub(r"\s+([.,!?;:])", r"\1", s)
2177
  s = re.sub(r"([.,!?;:])\s+", r"\1 ", s)
 
2178
  s = re.sub(r"(\d)\s*\.\s*(\d)", r"\1.\2", s)
 
2179
  s = re.sub(r"[,;:]+\s*\.", ".", s)
2180
  s = re.sub(r"[,;:]+\s*$", "", s)
2181
  s = s.strip()
 
2185
  s = s[0].upper() + s[1:]
2186
  return s
2187
 
2188
+ sent1 = tidy(" ".join(x for x in [before_text, beforesign_text, after_text] if x))
2189
+ sent2 = tidy(" ".join(x for x in [before_text, aftersign_text, after_text] if x))
 
 
2190
 
2191
  result = []
2192
  if sent1:
 
2199
 
2200
  except Exception as e:
2201
  print(f"CompoundSimplify error: {e}")
2202
+ return []
 
2203
  ###############################################################################
2204
 
2205
 
 
2544
  ###############################################################################
2545
 
2546
  ###############################################################################
2547
+ COORDINATORS = {"and", "or", "but"}
2548
+ SSCCV_SIGNS = {"that", "which", "who", "whom", "whose", "when", "where", "while"}
2549
+ RELATIVE_PRONOUNS = {"who", "which", "whom", "whose"}
2550
+
2551
  def build_single_sign_bracketed(tagged_list, mode="ccv", skip_indices=None):
2552
  """
2553
+ mode = "ccv" β†’ leftmost and/or/but tagged M:Y_CCV
2554
+ mode = "cmv1" β†’ leftmost and/or/but tagged M:Y_CMV1
2555
+ mode = "cmn1" β†’ leftmost and/or/but tagged M:Y_CMN1
2556
+ mode = "cln" β†’ leftmost and/or/but tagged M:Y_CLN
2557
+ mode = "ssccv" β†’ one relative SSCCV trigger (never a bare colon/comma)
2558
+ mode = "ssmn" β†’ leftmost SSMN sign
2559
+
2560
+ skip_indices: token offsets already tried and rejected.
 
 
 
 
 
2561
  Returns None if nothing remains to bracket for this mode.
2562
  """
 
2563
  skip_indices = set(skip_indices or [])
2564
 
2565
  def surface_of(item):
2566
+ s = str(item)
2567
+ if s.startswith("<M:Y_") and ">" in s:
2568
+ start = s.find(">") + 1
2569
+ end = s.rfind("<")
2570
  if end > start:
2571
+ return s[start:end]
2572
+ return s
2573
+
2574
+ def is_coordinator(i):
2575
+ return surface_of(tagged_list[i]).strip().lower() in COORDINATORS
2576
 
2577
  ccv_indices = []
2578
  cmv1_indices = []
 
2586
  if i in skip_indices:
2587
  continue
2588
  s = str(item)
2589
+ surf = surface_of(item).strip().lower()
2590
+
2591
+ if ("<M:Y_CCV>" in s or "M:Y_CCV" in s) and surf in COORDINATORS:
2592
  ccv_indices.append(i)
2593
+ if ("<M:Y_CMV1>" in s or "M:Y_CMV1" in s) and surf in COORDINATORS:
2594
  cmv1_indices.append(i)
2595
+ if ("<M:Y_CMN1>" in s or "M:Y_CMN1" in s) and surf in COORDINATORS:
2596
  cmn1_indices.append(i)
2597
+ if ("<M:Y_CLN>" in s or "M:Y_CLN" in s) and surf in COORDINATORS:
2598
  cln_indices.append(i)
2599
  if "M:Y_SSCCV" in s:
2600
  ssccv_indices.append(i)
 
2607
 
2608
  target = set()
2609
 
2610
+ # leftmost real coordinator β€” never a comma or colon
2611
  if mode == "ccv" and ccv_indices:
2612
+ target.add(ccv_indices[0])
 
2613
  elif mode == "cmv1" and cmv1_indices:
2614
+ target.add(cmv1_indices[0])
 
2615
  elif mode == "cmn1" and cmn1_indices:
2616
+ target.add(cmn1_indices[0])
 
2617
  elif mode == "cln" and cln_indices:
2618
+ target.add(cln_indices[0])
 
2619
  elif mode == "ssmn" and ssmn_indices:
2620
  target.add(ssmn_indices[0])
2621
 
2622
  elif mode == "ssccv" and ssccv_indices:
 
2623
  preferred = []
 
2624
  for i in ssccv_indices:
2625
  surface = surface_of(tagged_list[i]).strip().lower()
2626
 
2627
+ if surface in RELATIVE_PRONOUNS or surface == "that":
2628
  preferred.append(i)
2629
  continue
2630
 
2631
+ # punctuation only if it introduces a relative
2632
  if surface in {",", ";", ":"} and i + 1 < len(tagged_list):
2633
  nxt = surface_of(tagged_list[i + 1]).strip().lower()
2634
+ if nxt in RELATIVE_PRONOUNS or nxt == "that":
2635
  preferred.append(i)
 
2636
 
2637
  if preferred:
2638
  nested = False
 
2658
  if not nested and not coordinated:
2659
  target.add(preferred[0])
2660
  else:
2661
+ # fallback: leftmost real SSCCV sign, never bare punctuation
2662
+ pure = [
2663
  i
2664
+ for i in ssccv_indices
2665
+ if surface_of(tagged_list[i]).strip().lower() in SSCCV_SIGNS
2666
  ]
2667
+ if pure:
2668
+ target.add(pure[0])
 
2669
 
2670
  if not target:
2671
  return None
2672
 
2673
  parts = []
2674
  for i, item in enumerate(tagged_list):
2675
+ s = str(item)
2676
+ if s.startswith("<M:Y_") and s.endswith(">"):
2677
+ start = s.find(">") + 1
2678
+ end = s.rfind("<")
2679
+ surface = s[start:end] if end > start else s
2680
  if i in target:
2681
+ # spaces stop "[:" fusing into one XX token
2682
+ parts.append(f"[ {surface} ]")
2683
  else:
2684
  parts.append(surface)
2685
  else:
2686
+ parts.append(s)
2687
 
2688
  text = " ".join(parts)
 
 
 
 
 
2689
  text = re.sub(r"\s+", " ", text).strip()
2690
 
2691
+ # keep "[ , which ]" style for a punct+relative pair
2692
  text = re.sub(
2693
+ r"\[\s*([,;:])\s*\]\s+(who|which|that|whom|whose)\b",
2694
+ r"[ \1 \2 ]",
2695
  text,
2696
  flags=re.I,
2697
  )