Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1208,6 +1208,54 @@ def SSCCVsimplify(xml_sent):
|
|
| 1208 |
|
| 1209 |
###############################################################################
|
| 1210 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1211 |
|
| 1212 |
###############################################################################
|
| 1213 |
def CompoundSimplify(xml_sent):
|
|
@@ -1874,6 +1922,25 @@ def process_text(input_text):
|
|
| 1874 |
)
|
| 1875 |
simps = simplifier(xml_sent)
|
| 1876 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1877 |
if not simps:
|
| 1878 |
print(
|
| 1879 |
"Simplifier returned []; forcing final (no re-queue)"
|
|
|
|
| 1208 |
|
| 1209 |
###############################################################################
|
| 1210 |
|
| 1211 |
+
###############################################################################
|
| 1212 |
+
def extract_coordinator_from_xml(xml_sent):
|
| 1213 |
+
"""Surface of IN_COMPOUND_CONSTITUENT (and / or / but / ; / , and …)."""
|
| 1214 |
+
elem = xml_sent.find(".//IN_COMPOUND_CONSTITUENT")
|
| 1215 |
+
if elem is None:
|
| 1216 |
+
return None
|
| 1217 |
+
parts = []
|
| 1218 |
+
for w in elem.findall("W"):
|
| 1219 |
+
if w.text and str(w.text).strip():
|
| 1220 |
+
parts.append(str(w.text).strip())
|
| 1221 |
+
if not parts:
|
| 1222 |
+
return None
|
| 1223 |
+
raw = " ".join(parts).strip().lower()
|
| 1224 |
+
raw = re.sub(r"\s+", " ", raw)
|
| 1225 |
+
# ", and" / "; and" → and
|
| 1226 |
+
for word in ("and", "or", "but"):
|
| 1227 |
+
if re.search(rf"\b{word}\b", raw):
|
| 1228 |
+
return word
|
| 1229 |
+
if raw in {";", ","} or raw.endswith(";") or raw.endswith(","):
|
| 1230 |
+
return "and" # list punctuation → treat as "and" for residuals
|
| 1231 |
+
return None
|
| 1232 |
+
###############################################################################
|
| 1233 |
+
|
| 1234 |
+
###############################################################################
|
| 1235 |
+
def rewrite_residual_list_comma(sent, coordinator="and"):
|
| 1236 |
+
"""
|
| 1237 |
+
Last list-separator comma → same conjunction as the split sign,
|
| 1238 |
+
so the next pass sees CMN1/CMV1/CCV more reliably (not SSMN).
|
| 1239 |
+
Skips comma before a bare number (e.g. Smith, 43).
|
| 1240 |
+
"""
|
| 1241 |
+
if not sent or not coordinator:
|
| 1242 |
+
return sent
|
| 1243 |
+
coord = coordinator.strip().lower()
|
| 1244 |
+
if coord not in {"and", "or", "but"}:
|
| 1245 |
+
return sent
|
| 1246 |
+
|
| 1247 |
+
s = sent.strip()
|
| 1248 |
+
m = re.search(r",\s+(?!\d\b)([^,]+?)(\s*[.!?])?\s*$", s)
|
| 1249 |
+
if not m:
|
| 1250 |
+
return s
|
| 1251 |
+
|
| 1252 |
+
head = s[: m.start()].rstrip()
|
| 1253 |
+
tail = m.group(1).strip()
|
| 1254 |
+
punct = (m.group(2) or "").strip() or "."
|
| 1255 |
+
out = f"{head} {coord} {tail}{punct}"
|
| 1256 |
+
out = re.sub(r"\s+", " ", out).strip()
|
| 1257 |
+
return out
|
| 1258 |
+
###############################################################################
|
| 1259 |
|
| 1260 |
###############################################################################
|
| 1261 |
def CompoundSimplify(xml_sent):
|
|
|
|
| 1922 |
)
|
| 1923 |
simps = simplifier(xml_sent)
|
| 1924 |
|
| 1925 |
+
# Residual comma → same conjunction (CCV / CMV1 / CMN1 only)
|
| 1926 |
+
if (
|
| 1927 |
+
simps
|
| 1928 |
+
and simplifier is CompoundSimplify
|
| 1929 |
+
and xml_tag in ("M:Y_CCV", "M:Y_CMV1", "M:Y_CMN1")
|
| 1930 |
+
):
|
| 1931 |
+
coord = extract_coordinator_from_xml(xml_sent)
|
| 1932 |
+
if coord:
|
| 1933 |
+
rewritten = []
|
| 1934 |
+
for s in simps:
|
| 1935 |
+
s2 = rewrite_residual_list_comma(s, coord)
|
| 1936 |
+
if s2 != s:
|
| 1937 |
+
print(
|
| 1938 |
+
f" Residual list rewrite ({coord}): "
|
| 1939 |
+
f"{s!r} → {s2!r}"
|
| 1940 |
+
)
|
| 1941 |
+
rewritten.append(s2)
|
| 1942 |
+
simps = rewritten
|
| 1943 |
+
|
| 1944 |
if not simps:
|
| 1945 |
print(
|
| 1946 |
"Simplifier returned []; forcing final (no re-queue)"
|