File size: 19,316 Bytes
32f6a89 70b2a71 32f6a89 70b2a71 32f6a89 70b2a71 32f6a89 70b2a71 32f6a89 70b2a71 32f6a89 70b2a71 32f6a89 70b2a71 32f6a89 70b2a71 32f6a89 70b2a71 32f6a89 70b2a71 32f6a89 70b2a71 32f6a89 70b2a71 32f6a89 70b2a71 32f6a89 70b2a71 32f6a89 70b2a71 32f6a89 70b2a71 32f6a89 70b2a71 32f6a89 70b2a71 32f6a89 70b2a71 32f6a89 70b2a71 32f6a89 70b2a71 32f6a89 70b2a71 32f6a89 70b2a71 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 | #@title 5A. CANDIDATE REALIZATION RELIABILITY PATCH v3.4
# Complete replacement for Cell 5A v3.3.
# Run immediately after Cell 5 v4.1 and before Cell 6 v5.
#
# Purpose:
# - reject malformed candidates at both generation stages;
# - preserve valid relative clauses, appositives, lists, and terminology;
# - remain rerun-safe after Generator is rebuilt by Cell 5;
# - add no transformation families and change no semantic policy;
# - expose precise rejection evidence to Cell 7, Cell 7B, and Cell 9.
import re
from collections import Counter
# =====================================================================
# 0. PREREQUISITE AND VERSION CHECKS
# =====================================================================
_REQUIRED_5A = [
"RULES", "rule", "Candidate", "Generator", "clean", "K",
"lower_initial", "surface_key", "nlp",
]
_missing_5a = [name for name in _REQUIRED_5A if name not in globals()]
if _missing_5a:
raise RuntimeError(
"Cell 5A cannot run. Missing: " + ", ".join(_missing_5a)
+ ". Run the complete Cell 5 v4.1 first."
)
EXPECTED_5A_RULE_COUNT = 30
if len(RULES) != EXPECTED_5A_RULE_COUNT:
raise RuntimeError(
f"Cell 5A expected {EXPECTED_5A_RULE_COUNT} Cell 5 v4.1 rules, "
f"but found {len(RULES)}. Rerun Cell 5 v4.1 first."
)
# =====================================================================
# 1. HIGH-CONFIDENCE REALIZATION CHECKS
# =====================================================================
_SUBJECT_PRONOUNS = {"i", "we", "he", "she", "they", "who"}
_OBJECT_PRONOUNS = {"me", "us", "him", "her", "them", "whom"}
_FINITE_TAGS = {"VBD", "VBP", "VBZ", "MD"}
_DECLARATIVE_STARTERS = {
"i", "we", "you", "he", "she", "it", "they",
"this", "that", "these", "those", "the", "a", "an",
}
_RELATIVE_MARKERS = {"that", "which", "who", "whose", "where"}
def _r34_split_sentences(text):
"""Return bounded sentence units without modifying source content."""
value = clean(text)
try:
document = nlp()(value)
units = [sent.text.strip() for sent in document.sents if sent.text.strip()]
if units:
return units
except Exception:
pass
return [
part.strip()
for part in re.split(r"(?<=[.!?])\s+", value)
if part.strip()
]
def _r34_parse(text):
try:
document = nlp()(str(text or ""))
return document if len(document) else None
except Exception:
return None
def _r34_has_finite_predicate(document):
if document is None:
return False
for token in document:
if token.tag_ in _FINITE_TAGS:
return True
if token.pos_ in {"VERB", "AUX"} and any(
child.dep_ in {"aux", "auxpass"} and child.tag_ in _FINITE_TAGS
for child in token.children
):
return True
return False
def _r34_relative_clause_is_finite(document):
"""Validate finite predicates after explicit relative markers."""
if document is None:
return True
for token in document:
if token.lower_ not in _RELATIVE_MARKERS:
continue
following = document[token.i + 1:]
if not following:
return False
if not any(
item.tag_ in _FINITE_TAGS
or item.pos_ == "AUX"
for item in following
):
return False
return True
def _r34_orphan_participial_fragment(sentence):
"""
Detect noun/preposition fragments such as:
'Protocol to small cybersecurity learners spanning ...'
while allowing a valid finite relative clause:
'learners that span ...'
"""
low = sentence.lower().strip()
if re.search(r"\b(?:that|which|who)\s+(?:span|spans|cover|covers|include|includes|contain|contains|involve|involves)\b", low):
return False
document = _r34_parse(sentence)
if document is not None and document.has_annotation("DEP"):
root = next((token for token in document if token.dep_ == "ROOT"), None)
if root is not None:
if root.pos_ in {"VERB", "AUX"} and _r34_has_finite_predicate(document):
return False
participles = [
token for token in document
if token.text.lower() in {
"spanning", "covering", "including", "containing", "involving"
}
]
if participles and not _r34_has_finite_predicate(document):
return True
return bool(re.match(
r"(?is)^(?:[A-Z][\w-]*|The|A|An)\b.+\s+"
r"(?:spanning|covering|including|containing|involving)\s+.+$",
sentence,
)) and not bool(re.search(
r"\b(?:is|are|was|were|has|have|had|do|does|did|can|could|may|might|"
r"must|should|will|would|span|spans|cover|covers|include|includes|"
r"contain|contains|involve|involves)\b",
low,
))
def _r34_clause_issues(text):
issues = []
for index, sentence in enumerate(_r34_split_sentences(text), start=1):
words = re.findall(r"[A-Za-z]+(?:[-'][A-Za-z]+)*", sentence)
if not words:
issues.append(f"EMPTY_SENTENCE_{index}")
continue
first = words[0].lower()
document = _r34_parse(sentence)
if first in _DECLARATIVE_STARTERS and not _r34_has_finite_predicate(document):
issues.append(f"NONFINITE_DECLARATIVE_FRAGMENT_{index}")
if document is not None:
root = next((token for token in document if token.dep_ == "ROOT"), None)
if (
first in _DECLARATIVE_STARTERS
and root is not None
and root.pos_ not in {"VERB", "AUX"}
and not _r34_has_finite_predicate(document)
):
issues.append(f"NOMINAL_ROOT_FRAGMENT_{index}")
if not _r34_relative_clause_is_finite(document):
issues.append(f"NONFINITE_RELATIVE_CLAUSE_{index}")
else:
if re.match(
r"(?is)^(?:I|We|You|He|She|It|They|The|A|An|This|That|These|Those)\s+"
r"(?:[A-Z][\w-]*\s+){1,8}"
r"(?:Framework|System|Model|Protocol|Method|Procedure|Process|Team|Study)\b",
sentence,
):
issues.append(f"LIKELY_NOMINAL_FRAGMENT_{index}")
if _r34_orphan_participial_fragment(sentence):
issues.append(f"ORPHAN_PARTICIPIAL_FRAGMENT_{index}")
return issues
def _r34_balanced_delimiters(text):
pairs = {"(": ")", "[": "]", "{": "}"}
stack = []
for char in str(text or ""):
if char in pairs:
stack.append(pairs[char])
elif char in pairs.values():
if not stack or stack.pop() != char:
return False
return not stack
def realization_issues(text):
"""Return only high-confidence grammatical/realization defects."""
value = clean(text)
low = value.lower()
issues = []
if not value:
return ["EMPTY_OUTPUT"]
if not _r34_balanced_delimiters(value):
issues.append("UNBALANCED_DELIMITERS")
if re.search(
r"\b(?:for|to|with|by|from|of|about)\s+(?:i|we|he|she|they|who)\b",
low,
):
issues.append("SUBJECT_PRONOUN_AFTER_PREPOSITION")
if re.match(
r"^(?:me|us|him|her|them|whom)\s+"
r"(?:am|is|are|was|were|must|should|can|could|may|might|will|would)\b",
low,
):
issues.append("OBJECT_PRONOUN_AS_SUBJECT")
if re.match(
r"^(?:inspect|distinguish|evaluate|examine|analyze|compare|apply|use|"
r"test|measure|determine|identify|review|assess|implement|investigate)"
r"\b.+\s+is\s+the\s+(?:next|first|second|third|final|primary|main)\s+"
r"(?:step|task|action|objective|goal)\b",
low,
):
issues.append("BARE_ACTION_AS_NOMINAL_SUBJECT")
if re.search(r"\b(?:is|are|was|were)\s+(?:is|are|was|were)\b", low):
issues.append("DUPLICATED_COPULA")
if re.search(r"\b(?:has|have|had)\s+been\s+been\b", low):
issues.append("DUPLICATED_AUXILIARY")
if re.search(r"\b(?:a|an)\s+(?:are|were|have|do)\b", low):
issues.append("DETERMINER_BEFORE_FINITE_VERB")
if re.search(
r"\b(?:of|for|with|by|from|to|in|on|under|within|across)\s+"
r"(?:The|A|An)\s+\w+",
value,
):
issues.append("INTERNAL_CAPITALIZED_DETERMINER")
if re.search(r"\s+[,.!?;:]", value):
issues.append("SPACE_BEFORE_PUNCTUATION")
if re.search(r"[.!?]{2,}", value):
issues.append("DUPLICATED_TERMINAL_PUNCTUATION")
if len(re.findall(r"\b\w+\b", value)) < 3:
issues.append("TOO_SHORT")
issues.extend(_r34_clause_issues(value))
return list(dict.fromkeys(issues))
# =====================================================================
# 2. RELIABLE SAME-NAME RULE OVERRIDES
# =====================================================================
@rule(
"copular_action_frame",
{"Balanced", "Structural"},
{"COPULAR_ACTION_FRAME"},
.96,
)
def tr_copular_action_reliable(analysis, profile, user_terms):
match = re.match(
r"(?is)^The\s+(?P<order>next|first|second|third|final|primary|main)\s+"
r"(?P<label>step|task|action|objective|goal)\s+is\s+to\s+"
r"(?P<action>.+?)[.]?$",
analysis.text,
)
if not match:
return []
action = match["action"].strip()
first_match = re.match(r"(?is)^(?P<verb>[A-Za-z-]+)(?P<rest>.*)$", action)
if not first_match:
return []
verb = first_match["verb"]
rest = first_match["rest"]
low = verb.lower()
irregular = {
"be": "being", "have": "having", "begin": "beginning",
"run": "running", "set": "setting", "get": "getting",
}
if low in irregular:
gerund = irregular[low]
elif low.endswith("ie"):
gerund = verb[:-2] + "ying"
elif low.endswith("e") and not low.endswith(("ee", "ye", "oe")):
gerund = verb[:-1] + "ing"
else:
gerund = verb + "ing"
candidate = K(
f"The {match['order']} {match['label']} involves {gerund}{rest}",
"Balanced",
"COPULAR_ACTION_FRAME",
"Converted a copular action frame into a grammatical event frame.",
.96,
)
return [] if realization_issues(candidate.text) else [candidate]
@rule(
"need_to_action",
{"Balanced", "Structural"},
{"NEED_TO_ACTION"},
.94,
)
def tr_need_to_reliable(analysis, profile, user_terms):
match = re.match(
r"(?is)^(?P<subject>.+?)\s+(?P<need>need|needs|needed)\s+to\s+"
r"(?P<action>.+?)[.]?$",
analysis.text,
)
if not match:
return []
modal = "had to" if match["need"].lower() == "needed" else "must"
candidate = K(
f"{match['subject'].strip()} {modal} {match['action'].strip()}",
"Balanced",
"NEED_TO_ACTION",
"Recast a need-to construction as a subject-preserving necessity frame.",
.94,
)
return [] if realization_issues(candidate.text) else [candidate]
@rule(
"modal_copular_enumeration",
{"Balanced", "Structural"},
{"MODAL_COPULAR_ENUMERATION", "COORDINATED_OBJECTS_OR_ENUMERATION", "ENUMERATION"},
.95,
)
def tr_modal_copular_enumeration_reliable(analysis, profile, user_terms):
match = re.match(
r"(?is)^(?P<subject>.+?)\s+(?P<modal>may|might|could|can)\s+be\s+in\s+"
r"(?P<items>.+?,\s*.+?\s*,?\s*(?:or|and)\s+.+?)[.]?$",
analysis.text,
)
if not match:
return []
candidate = K(
f"Possible sources of {lower_initial(match['subject'])} "
f"{match['modal'].lower()} include {match['items']}",
"Balanced",
"MODAL_COPULAR_ENUMERATION",
"Recast a modal copular list while preserving uncertainty and every member.",
.95,
)
return [] if realization_issues(candidate.text) else [candidate]
@rule(
"passive_using_nominal",
{"Balanced", "Structural"},
{"PASSIVE_USING"},
.96,
)
def tr_passive_using_reliable(analysis, profile, user_terms):
match = re.match(
r"(?is)^(?:(?P<marker>However|Therefore|Consequently|Thus|Moreover|"
r"Furthermore|Nevertheless|Accordingly),\s+)?"
r"(?P<subject>.+?)\s+"
r"(?:is|are|was|were|has been|have been|had been)\s+"
r"(?P<adverb>\w+ly\s+)?evaluated\s+using\s+"
r"(?P<object>.+?)[.]?$",
analysis.text,
)
if not match:
return []
marker = (match.group("marker") or "").strip()
adverb = (match.group("adverb") or "").strip()
prefix = f"{marker}, " if marker else ""
adverb_text = f"{adverb} " if adverb else ""
candidate = K(
f"{prefix}Evaluations of {lower_initial(match['subject'])} "
f"{adverb_text}use {match['object']}",
"Balanced",
"PASSIVE_USING",
"Recast a passive evaluation as an evaluation event without inventing an actor.",
.96,
)
return [] if realization_issues(candidate.text) else [candidate]
# =====================================================================
# 3. FIRST-STAGE FILTER — RERUN SAFE
# =====================================================================
if not hasattr(Generator, "_nll_reliability_base_lane"):
Generator._nll_reliability_base_lane = Generator.lane
else:
Generator.lane = Generator._nll_reliability_base_lane
def _lane_with_realization_filter(self, analysis, profile, user_terms, lane, trace):
candidates = Generator._nll_reliability_base_lane(
self, analysis, profile, user_terms, lane, trace
)
accepted = []
for candidate in candidates:
issues = realization_issues(candidate.text)
if issues:
trace.setdefault("realization_rejections", []).append({
"stage": "lane",
"rule": getattr(candidate, "family", ""),
"lane": getattr(candidate, "lane", lane),
"text": getattr(candidate, "text", ""),
"issues": issues,
})
continue
accepted.append(candidate)
return accepted
Generator.lane = _lane_with_realization_filter
# =====================================================================
# 4. FINAL/COMPOSED FILTER — RERUN SAFE
# =====================================================================
if not hasattr(Generator, "_nll_reliability_base_generate"):
Generator._nll_reliability_base_generate = Generator.generate
else:
Generator.generate = Generator._nll_reliability_base_generate
def _generate_with_final_realization_filter(
self, analysis, profile, user_terms, mode
):
candidates, trace = Generator._nll_reliability_base_generate(
self, analysis, profile, user_terms, mode
)
accepted = []
seen = set()
for candidate in candidates:
text = getattr(candidate, "text", "")
issues = realization_issues(text)
if issues:
trace.setdefault("realization_rejections", []).append({
"stage": "final_pool",
"rule": getattr(candidate, "family", ""),
"lane": getattr(candidate, "lane", mode),
"text": text,
"issues": issues,
})
continue
key = surface_key(text)
if not key or key in seen:
continue
seen.add(key)
accepted.append(candidate)
trace["generated_before_final_filter"] = len(candidates)
trace["generated_after_final_filter"] = len(accepted)
trace["generated"] = len(accepted)
return accepted, trace
Generator.generate = _generate_with_final_realization_filter
# =====================================================================
# 5. SELF-TESTS
# =====================================================================
_BAD_REALIZATIONS = [
"Inspect how the handler works is the next step.",
"Distinguish the possibilities is necessary for we.",
"For they, the review is necessary.",
"The system is is evaluated.",
"Evaluations of The service use latency measurements.",
"We propose. We Adaptive Learning and Competency Evaluation Framework.",
"Protocol to small cybersecurity learners spanning classification, drafting, and planning.",
"The learners that spanning classification, drafting, and planning.",
"The learners that classification, drafting, and planning.",
]
_GOOD_REALIZATIONS = [
"The next step involves inspecting how the handler works.",
"We must distinguish the possibilities using the existing source.",
"Possible sources of the problem could include lookup, initialization, or reporting logic.",
"Evaluations of the service use latency, throughput, and failure-rate measurements.",
"We propose the Adaptive Learning and Competency Evaluation Framework.",
"The experimental testbed applies this protocol to small cybersecurity learners that span classification, generative drafting, and structured diagram planning.",
"ALCEF-RSF is a domain-independent orchestration protocol consisting of native model contracts, protected examinations, and auditable promotion proposed by us.",
]
for bad_text in _BAD_REALIZATIONS:
if not realization_issues(bad_text):
raise RuntimeError(
"Cell 5A v3.4 failed to reject malformed text: " + bad_text
)
for good_text in _GOOD_REALIZATIONS:
detected = realization_issues(good_text)
if detected:
raise RuntimeError(
"Cell 5A v3.4 incorrectly rejected valid text: "
+ good_text + " -> " + str(detected)
)
_rule_names = [registered.name for registered in RULES]
_duplicates = sorted(
name for name, count in Counter(_rule_names).items() if count > 1
)
if _duplicates:
raise RuntimeError(
"Cell 5A v3.4 duplicate rule registration: " + ", ".join(_duplicates)
)
if len(RULES) != EXPECTED_5A_RULE_COUNT:
raise RuntimeError(
f"Cell 5A v3.4 expected {EXPECTED_5A_RULE_COUNT} rules but found {len(RULES)}."
)
if getattr(Generator.lane, "__name__", "") != "_lane_with_realization_filter":
raise RuntimeError("Cell 5A v3.4 lane wrapper was not installed.")
if getattr(Generator.generate, "__name__", "") != "_generate_with_final_realization_filter":
raise RuntimeError("Cell 5A v3.4 generate wrapper was not installed.")
print("=" * 78)
print("CELL 5A CANDIDATE RELIABILITY PATCH v3.4 LOADED")
print("=" * 78)
print("Aligned Cell 5 version: v4.1")
print("Finite declarative-clause checks: active")
print("Finite relative-clause checks: active")
print("Orphan participial-fragment checks: active")
print("Delimiter-balance checks: active")
print("Pronoun-case checks: active")
print("Internal capitalization checks: active")
print("First-stage filtering: active")
print("Final/composed filtering: active")
print("Rerun-safe Generator.lane wrapping: active")
print("Rerun-safe Generator.generate wrapping: active")
print("Malformed candidate self-tests: PASSED")
print("Valid candidate self-tests: PASSED")
print("Duplicate rule registrations: none")
print("Registered transformation rules:", len(RULES))
print("Expected rule count:", EXPECTED_5A_RULE_COUNT)
print("=" * 78)
|