_inflect_predicate applied base_form — a SINGLE-VERB function — to whole
humanized predicate phrases, stripping the final word's last character
class, and its plural branch never consulted `copular`. Nine of the 26 seed
predicates came out wrong and every multi-word one did:
is defined as -> "is defined a" want "are defined as"
has the following ... -> "...step" want "have the following steps"
belongs to -> "belongs to" want "belong to"
causes -> "caus" want "cause"
Root causes, both single-point as §1.4 predicted:
1. morphology.agree_plural_phrase inflects the HEAD (the finite verb is the
first token of every humanized predicate) and carries the rest through.
base_form stays single-verb and is PINNED as still wrong on a phrase, so
nobody "fixes" the symptom in the wrong place.
2. The plural branch now agrees the phrase. The plural NEGATED branch had the
same defect ("do not is defined a") and is fixed with it: a plural copula
takes a bare "not", everything else takes do-support.
Two further defects found while writing the eval cases, both fixed here:
base_form("causes") -> "caus". The -es sibilant rule fired on a stem
ending in a single "s"; it must require a doubled "ss" ("passes"->"pass"),
because a single "s" is nearly always a stem ending in "e" that took a
plain -s ("causes"->"cause").
pluralize("proof") -> "prooves". f/fe -> ves is NOT productive in English
(proof->proofs, chief->chiefs, roof->roofs); it is a closed set. Now
derived as lexicon.VES_PLURAL_SINGULARS from the ves-rows of
IRREGULAR_SINGULARS, so the rule cannot claim a word the table does not
know. Phase 2B had put pluralize on the SERVING path, so this one was live
— and the surface-hashing pin from #133 confirms no served surface moved,
which is that guard's first real use.
New construction C14 quantified_copular, 13 cases: the combination that was
broken was the one never tested. Measured before: 152 corpus cases carry a
quantifier and ZERO combine it with a copular predicate.
MY FIRST DRAFT OF THOSE CASES COULD NOT FAIL. must_contain/word_order listed
quantifier, subject and object but NOT THE VERB, so "all molecules is
defined a compound" passed and reverting the fix left 47/47 green. Rewritten
with the agreed verb in both constraints plus reject_surfaces carrying the
ACTUAL pre-fix output, computed by running the reverted code rather than
guessed. Mutation now:
baseline 13/13 C14
revert phrase-head agreement 4/13
revert -es stem rule 12/13
revert closed ves set 11/13
The 4 survivors of the first mutation are the mass-noun controls, which is
correct — "all evidence is grounded in truth" must NOT pluralize.
Same lesson as the lane pins in #133: a pin that cannot fail guards nothing,
and the only way to know is to break the thing on purpose.
grammar_roundtrip's graph corpus is harvested from the committed case files,
so it grew 280 -> 293. Updated exactly rather than loosened to an inequality:
a corpus that grows or shrinks should require a deliberate edit.
[Verification]: in-worktree on CPython 3.12.13, uv sync --locked —
agreement 26/26 (from 17/26); english_fluency_ood 117/117 + 39/39 + dev
13/13 unchanged; grammatical_coverage v1 49/49; smoke 621 unchanged;
deductive 405; scripts/verify_lane_shas.py 11/11, no pin edited.
179 lines
6.7 KiB
Python
179 lines
6.7 KiB
Python
"""Deterministic surface templates for rhetorical moves.
|
|
|
|
Each template is a format string keyed by RhetoricalMove. Slots:
|
|
{subject} — primary subject from the articulation step
|
|
{predicate} — semantic predicate (e.g. "is_defined_as", "contrasts_with")
|
|
{obj} — object slot from the graph node (may be "<pending>")
|
|
|
|
Templates are intentionally simple. The goal is structural correctness,
|
|
not fluency — fluency comes in a later phase when the generation stream
|
|
consumes these as constraints rather than final output.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from generate.lexicon import (
|
|
IRREGULAR_PLURALS,
|
|
PLURAL_QUANTIFIERS,
|
|
PREDICATE_DISPLAY,
|
|
)
|
|
from generate.articulation_legality import (
|
|
ArticulationLegality,
|
|
validate_finite_predicate_legality,
|
|
)
|
|
from generate.graph_planner import RhetoricalMove
|
|
from generate.morphology import (
|
|
agree_plural_phrase,
|
|
base_form,
|
|
is_mass_noun,
|
|
past_participle,
|
|
past_tense,
|
|
pluralize,
|
|
present_participle,
|
|
)
|
|
|
|
|
|
# Noun pluralisation — used under quantifiers (all/some/many/few/most).
|
|
# Closes english_fluency_ood gaps.md G2 (plural agreement).
|
|
#
|
|
# Phase 2B: the rules moved to generate/morphology.py, which now owns number
|
|
# in both directions. Re-exported here because this module's public surface
|
|
# is consumed by the eval runners.
|
|
_IRREGULAR_PLURALS: dict[str, str] = IRREGULAR_PLURALS
|
|
|
|
|
|
# Quantifiers that demand plural agreement on the subject + verb.
|
|
# "the" / "a" stay singular; "every" / "each" are singular by English
|
|
# rule even though semantically universal.
|
|
_PLURAL_QUANTIFIERS: frozenset[str] = PLURAL_QUANTIFIERS
|
|
|
|
_PREDICATE_DISPLAY: dict[str, str] = PREDICATE_DISPLAY
|
|
|
|
|
|
def _humanize_predicate(predicate: str) -> str:
|
|
return _PREDICATE_DISPLAY.get(predicate, predicate.replace("_", " "))
|
|
|
|
|
|
_MOVE_TEMPLATES: dict[RhetoricalMove, str] = {
|
|
RhetoricalMove.ASSERT: "{subject} {predicate_h} {obj}",
|
|
RhetoricalMove.ELABORATE: "furthermore, {subject} {predicate_h} {obj}",
|
|
RhetoricalMove.CONTRAST: "in contrast, {subject} {predicate_h} {obj}",
|
|
RhetoricalMove.SEQUENCE: "next, {subject} {predicate_h} {obj}",
|
|
RhetoricalMove.CORRECT: "correction: {subject} {predicate_h} {obj}",
|
|
}
|
|
|
|
|
|
def _inflect_predicate(
|
|
predicate_h: str,
|
|
*,
|
|
negated: bool = False,
|
|
tense: str | None = None,
|
|
aspect: str | None = None,
|
|
plural_subject: bool = False,
|
|
) -> str:
|
|
"""Apply tense/aspect/negation to a humanized predicate.
|
|
|
|
When ``plural_subject`` is true, the conjugation uses plural
|
|
agreement (do not / have / are / bare-base verb in present) so
|
|
surfaces like "all molecules bind enzyme" come out correctly
|
|
instead of "all molecule binds enzyme" (english_fluency_ood G2).
|
|
"""
|
|
verb = predicate_h
|
|
copular = any(
|
|
predicate_h.startswith(prefix)
|
|
for prefix in ("is ", "are ", "has ", "have ", "belongs ")
|
|
)
|
|
base = base_form(verb)
|
|
|
|
match (aspect, tense, negated, plural_subject):
|
|
case ("perfective", _, _, True):
|
|
return f"have {past_participle(verb)}"
|
|
case ("perfective", _, _, False):
|
|
return f"has {past_participle(verb)}"
|
|
case ("imperfective", _, _, True):
|
|
return f"are {present_participle(verb)}"
|
|
case ("imperfective", _, _, False):
|
|
return f"is {present_participle(verb)}"
|
|
case (_, "past", True, _):
|
|
return f"did not {base}"
|
|
case (_, "past", False, _):
|
|
return past_tense(verb)
|
|
case (_, "future", True, _):
|
|
return f"will not {base}"
|
|
case (_, "future", False, _):
|
|
return f"will {base}"
|
|
case (_, _, True, True):
|
|
# Plural + negated. Agree the head first, then negate around it:
|
|
# a plural copula takes a bare "not" ("are not defined as"), while
|
|
# any other verb needs do-support ("do not have the following
|
|
# steps", "do not belong to"). Previously this was
|
|
# ``f"do not {base}"`` over the whole phrase, which produced
|
|
# "do not is defined a".
|
|
agreed = agree_plural_phrase(predicate_h)
|
|
head, sep, rest = agreed.partition(" ")
|
|
if head in ("are", "were"):
|
|
return f"{head} not{sep}{rest}" if rest else f"{head} not"
|
|
return f"do not {agreed}"
|
|
case (_, _, True, False) if copular:
|
|
if predicate_h.startswith("is "):
|
|
return "is not " + predicate_h[3:]
|
|
if predicate_h.startswith("are "):
|
|
return "are not " + predicate_h[4:]
|
|
if predicate_h.startswith("has "):
|
|
return "has not " + predicate_h[4:]
|
|
if predicate_h.startswith("have "):
|
|
return "have not " + predicate_h[5:]
|
|
if predicate_h.startswith("belongs "):
|
|
return "does not belong " + predicate_h[8:]
|
|
return f"is not {base}"
|
|
case (_, _, True, False):
|
|
return f"does not {base}"
|
|
case (_, _, False, True):
|
|
# Plural agreement on the whole phrase, not base_form() of it.
|
|
# This is the branch the 9-of-26 defect lived in: it returned the
|
|
# bare base and never consulted ``copular``, so "is defined as"
|
|
# came back "is defined a" instead of "are defined as".
|
|
return agree_plural_phrase(predicate_h)
|
|
case _:
|
|
return verb
|
|
|
|
|
|
def render_step(
|
|
move: RhetoricalMove,
|
|
subject: str,
|
|
predicate: str,
|
|
obj: str,
|
|
*,
|
|
negated: bool = False,
|
|
quantifier: str | None = None,
|
|
tense: str | None = None,
|
|
aspect: str | None = None,
|
|
) -> str:
|
|
"""Render a single articulation step into a surface fragment."""
|
|
template = _MOVE_TEMPLATES[move]
|
|
# Mass nouns under a quantifier stay singular ("all evidence
|
|
# supports", not "all evidences support"). Count nouns
|
|
# pluralise and the verb de-conjugates ("all molecules bind").
|
|
plural_q = quantifier is not None and quantifier.lower() in _PLURAL_QUANTIFIERS
|
|
is_mass = is_mass_noun(subject)
|
|
plural = plural_q and not is_mass
|
|
predicate_h = _humanize_predicate(predicate)
|
|
legality = validate_finite_predicate_legality(
|
|
predicate_humanized=predicate_h,
|
|
negated=negated,
|
|
)
|
|
if legality.legality is ArticulationLegality.ILLEGAL_NON_VERB_FINITE_PREDICATE:
|
|
return "I cannot realize that proposition coherently yet."
|
|
predicate_h = _inflect_predicate(
|
|
predicate_h,
|
|
negated=negated, tense=tense, aspect=aspect,
|
|
plural_subject=plural,
|
|
)
|
|
obj_display = obj if obj != "<pending>" else "..."
|
|
subject_form = pluralize(subject) if plural else subject
|
|
subject_display = f"{quantifier} {subject_form}" if quantifier else subject_form
|
|
return template.format(
|
|
subject=subject_display,
|
|
predicate_h=predicate_h,
|
|
obj=obj_display,
|
|
)
|