| """Dependency + template sentence restructure — thin re-export of engine stages. | |
| Prefer ``app.engine`` for new code. Kept for scripts that import restructure APIs. | |
| """ | |
| from __future__ import annotations | |
| from app.engine.classify import classify_sentence | |
| from app.engine.models import RewritePlan as ReorderPlan | |
| from app.engine.models import SentenceSlots | |
| from app.engine.parse import extract_slots | |
| from app.engine.plan import build_plan | |
| from app.engine.rewrite import generate_from_plan, reorder_quality_ok | |
| from app.engine.templates import ( | |
| fill_template as _fill_template, | |
| rank_templates, | |
| try_because_front, | |
| try_discourse_front, | |
| ) | |
| class RestructureResult: | |
| def __init__( | |
| self, | |
| text: str, | |
| template_id: str, | |
| confidence: float, | |
| plan: ReorderPlan | None = None, | |
| ) -> None: | |
| self.text = text | |
| self.template_id = template_id | |
| self.confidence = confidence | |
| self.plan = plan | |
| def plan_reorder(slots: SentenceSlots) -> ReorderPlan | None: | |
| cands = rank_templates(slots) | |
| if not cands: | |
| return None | |
| return ReorderPlan( | |
| safe=True, | |
| slots=slots, | |
| template_id=cands[0].template_id, | |
| candidates=cands, | |
| confidence=cands[0].confidence, | |
| ) | |
| def fill_template(plan: ReorderPlan): | |
| """Back-compat: accept ReorderPlan like the old API.""" | |
| if plan is None or not plan.slots: | |
| return None | |
| tid = plan.template_id or (plan.candidates[0].template_id if plan.candidates else "") | |
| if not tid: | |
| return None | |
| return _fill_template(tid, plan.slots) | |
| def restructure_sentence(text: str, *, min_confidence: float = 0.55) -> RestructureResult | None: | |
| plan = build_plan(text, min_confidence=min_confidence) | |
| if not plan.safe: | |
| return None | |
| filled = generate_from_plan(plan) | |
| if not filled: | |
| return None | |
| return RestructureResult( | |
| text=filled, | |
| template_id=plan.template_id, | |
| confidence=plan.confidence, | |
| plan=plan, | |
| ) | |
| _reorder_quality_ok = reorder_quality_ok | |
| __all__ = [ | |
| "SentenceSlots", | |
| "ReorderPlan", | |
| "RestructureResult", | |
| "classify_sentence", | |
| "extract_slots", | |
| "plan_reorder", | |
| "fill_template", | |
| "restructure_sentence", | |
| "try_because_front", | |
| "try_discourse_front", | |
| "_reorder_quality_ok", | |
| ] | |