RefSeg-CA / source /code /r2_repair.py
nmsofficial's picture
Add verified public availability and human-evaluation guide
9126e0d verified
Raw
History Blame Contribute Delete
3.11 kB
"""Scope-filtered anchor projection. Frozen before R2 predictions.
The new primary rule removes the unhelpful counterfactual branch and refuses
clauses whose scope/cardinality cannot be represented by the original parser.
No test labels, images, or masks enter the syntax guard.
"""
import re
import phase1_repair as p1
FORBIDDEN=re.compile(r'\b(that|which|who|whose|with|wearing|holding|carrying|standing|sitting|looking|and|or|except|without|behind|between|nearest|closest|not|no)\b')
EXACT_COUNT=re.compile(r'\b(two|three|four|five|six|seven|eight|nine|ten|[2-9])\b')
def scope_safe(text):
q=p1.parse_query(text)
if q.negate:return True,'explicit_action'
if not q.supported:return False,'outside_grammar'
if EXACT_COUNT.search(q.original):return False,'exact_cardinality_unsupported'
clauses=[q.target]+([q.anchor] if q.anchor else [])
if any(FORBIDDEN.search(c) or len(c.split())>5 for c in clauses):return False,'nested_or_modified_clause'
if any(re.search(r'\b(left|right|above|below)\b',c) for c in clauses):return False,'nested_relation'
return True,'accepted'
def repair(text,maps,threshold):
preds,flags=p1.repaired_maps(text,maps,threshold)
safe,reason=scope_safe(text)
preds['safe_anchor']=preds['anchor_gate'] if safe else preds['frozen']
flags.update(scope_safe=safe,scope_reason=reason,cf_vs_anchor_changed=bool((preds['cacp']!=preds['anchor_gate']).any()),nogate_cf_changed=bool((preds['anchor_nogate']!=preds['counterfactual_nogate']).any()),safe_vs_frozen_changed=bool((preds['safe_anchor']!=preds['frozen']).any()))
return preds,flags
def primary_plan(text):
q=p1.parse_query(text);safe,_=scope_safe(text)
return list(dict.fromkeys([q.original]+([q.target,q.anchor] if safe and q.anchor else [q.target] if safe and not q.negate else [])))
def primary_mask(text,maps,threshold):
"""The primary rule alone, without computing any ablated branches."""
q=p1.parse_query(text);p=maps[q.original];base=p1.native(p,threshold)
if q.negate:return base & False
safe,_=scope_safe(text)
if not safe:return base
targets=p1.components(maps.get(q.target,p),threshold)
if not targets:return base
if q.relation.endswith('most'):return p1.global_component(targets,q.relation)['mask']
if not q.anchor:
if q.plural:return p1.union(targets,p.shape)
margin=targets[0]['confidence']-(targets[1]['confidence'] if len(targets)>1 else 0)
return base if margin<.05 else targets[0]['mask']
anchors=p1.components(maps[q.anchor],threshold)
if not anchors:return base
ac=anchors[0];margin=ac['confidence']-(anchors[1]['confidence'] if len(anchors)>1 else 0)
if margin<.05:return base
key='x' if q.relation in ('left','right') else 'y';sign=1 if q.relation in ('right','below') else -1
allowed=[c for c in targets if sign*(c[key]-ac[key])>=.03]
if not allowed:return base
margin=allowed[0]['confidence']-(allowed[1]['confidence'] if len(allowed)>1 else 0)
if not q.plural and margin<.05:return base
return p1.union(allowed if q.plural else [allowed[0]],p.shape)