Files
quality_analyse/verification/test_native_structure.py
T

147 lines
11 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Structural invariants, independently of numerical equality or model choice."""
import sys,unittest
from pathlib import Path
sys.path[:0]=[str(Path(__file__).resolve().parents[1]/'scripts'),str(Path(__file__).parent)]
from nao_native_contract import native_span_structure,native_document_ancestors,rebase_native_bindings,heading_subject
from nao_native_answer import (_native_span_slots,_native_draft_candidate,NativeCompositionError,
build_native_report,_prepare_native_draft,_native_present_sources,_native_atomic_inventory)
from test_native_grounded_contract import GroundedNativeContractTests
class NativeStructureTests(unittest.TestCase):
def locate(self,text,span):
start=text.index(span);return native_span_structure(text).locate(start,start+len(span))
def test_future_siblings_never_change_earlier_owner_or_metric(self):
before='黑片阶段(对综合率贡献-0.30pp,亮片约-0.15pp)'
after=before+',组织集中在新能源汽车磁钢、汽车交通-国内。'
for value in ('-0.30','-0.15'):
first=self.locate(before,value);second=self.locate(after,value)
for key in ('predicate','prefix','subjectText','inheritedText','periodRole'):
self.assertEqual(first[key],second[key])
self.assertNotIn('汽车交通',second['subjectText'])
def test_list_subject_target_and_change_parts_are_distinct_relations(self):
text='- 黑片:率97%→96%(自身-1pp,对部门综合率贡献-0.8pp);投入10→20 kg(+10 kg),报废1→3 kg(+2 kg);其中责废+1.5 kg、料废+0.5 kg。'
contribution=self.locate(text,'-0.8')
self.assertEqual(contribution['ownerStage'],'black');self.assertEqual(contribution['metricTarget'],'部门综合率')
for value in ('20','+2','+1.5','+0.5'):
location=self.locate(text,value);self.assertEqual(location['ownerStage'],'black')
self.assertEqual(self.locate(text,'+2')['fieldAssociation']['kind'],'change_of_pair')
for value in ('+1.5','+0.5'):self.assertEqual(self.locate(text,value)['fieldAssociation']['kind'],'part_of_change')
no_change=text.replace('(+2 kg)','')
self.assertNotEqual((self.locate(no_change,'+1.5')['fieldAssociation'] or {}).get('kind'),'part_of_change')
self.assertEqual(heading_subject('三、下钻:部门甲内部构成'),'部门甲')
self.assertNotEqual(heading_subject('组织维度:下降集中在部门甲'),'部门甲')
def test_parent_predicates_and_enumerations_do_not_use_token_semantics(self):
text='且其报废以黑片为主(黑片{{N82}}、亮片{{N83}}),投入规模为黑片{{N84}}、亮片{{N85}}'
for token,family,stage in [('N82','scrap','black'),('N83','scrap','bright'),('N84','input','black'),('N85','input','bright')]:
location=self.locate(text,'{{'+token+'}}')
self.assertEqual((location['metricFamily'],location['ownerStage']),(family,stage))
wrong=text.replace('投入规模为','报废重量为')
self.assertEqual(self.locate(wrong,'{{N84}}')['metricFamily'],'scrap')
self.assertEqual(self.locate(wrong,'{{N85}}')['metricFamily'],'scrap')
def test_ordered_header_and_from_to_bind_roles_without_using_value(self):
table='| 部门 | 综合率(9月/8月) |\n|---|---|\n| 甲 | 95.31% / 95.90% |'
self.assertEqual(self.locate(table,'95.31%')['periodText'],'9月')
self.assertEqual(self.locate(table,'95.90%')['periodText'],'8月')
reversed_header=table.replace('9月/8月','8月/9月')
self.assertEqual(self.locate(reversed_header,'95.31%')['periodText'],'8月')
for text in ('黑片投入从100 kg扩到200 kg。','黑片投入由100 kg降至200 kg。','黑片投入100 kg→200 kg。'):
self.assertEqual(self.locate(text,'100')['periodRole'],'comparison')
self.assertEqual(self.locate(text,'200')['periodRole'],'current')
ambiguous=table.replace('95.90%','95.90% / 94.00%')
self.assertIsNone(self.locate(ambiguous,'95.31%')['periodText'])
def test_only_structural_ordinals_are_masked(self):
text='### 2. 对比2026年9月\n1. 规格127.985质量\n合格率95.2%。'
masked=native_span_structure(text).mask_ordinals()
self.assertNotIn('2. 对比',masked);self.assertNotIn('1. 规格',masked)
for literal in ('2026年9月','127.985','95.2%'):self.assertIn(literal,masked)
def test_nearest_prior_subject_is_available_but_not_old_list(self):
text='规格甲报废增量10 kg,另有线索:规格乙投入从100 kg到200 kg;规格丙率下降3 pp,投入50 kg。'
located=self.locate(text,'100');self.assertNotIn('规格甲',located['inheritedText'])
located=self.locate(text,'50');self.assertTrue(any('规格丙' in value for value in located['precedingPredicates']))
self.assertFalse(any('规格乙' in value for value in located['precedingPredicates']))
enumerated='规格甲(+10 kg)、规格乙(+20 kg)、规格丙(+30 kg)。'
for name,value in [('甲','10'),('乙','20'),('丙','30')]:
location=self.locate(enumerated,value)
self.assertIn('规格'+name,location['inheritedText'])
self.assertEqual(sum('规格'+other in location['inheritedText'] for other in '甲乙丙'),1)
def test_versions_and_explicit_ancestors_are_separate(self):
blocks=[{'sourceId':'D1','text':'## 产业部:部门甲\n\n分组比较:'},
{'sourceId':'D2','text':'| 分组 | 本期 |\n|---|---|\n| 分组乙 | 10 |'}]
labels=native_document_ancestors(blocks)
self.assertIn('产业部:部门甲',labels['D2']);self.assertIn('分组比较:',labels['D2'])
first=native_span_structure('重量1吨。');second=native_span_structure('重量1,000.00 kg。')
self.assertIsNot(first,second);self.assertEqual(second.locate(2,10)['prefix'],'重量')
def test_conflicting_authorized_parent_is_rejected_without_value_lookup(self):
text='分组乙报废10 kg。';tokens={'N1':{'ownerScope':{'department':'部门乙','group':'分组乙'}}}
context={'_agentTask':{'filters':{'department':'部门甲'},'period':{'year':2026,'month':9}},
'_nativeScopeCatalog':[{'department':'部门乙','group':'分组乙'}]}
slots=_native_span_slots(text,5,7,context,tokens,'kg')
self.assertTrue(slots['scopeConflict'])
def test_deleted_bindings_are_removed_only_if_wholly_contained(self):
text='本期95.16%。设备99.0异常。';removed=[(9,len(text))];remaining=text[:9]
bindings=[{'span':'95.16%','tokenId':'N1'},{'span':'99.0','tokenId':'N2'}]
self.assertEqual(rebase_native_bindings(text,remaining,bindings,removed),[{**bindings[0],'occurrence':1}])
self.assertIsNone(rebase_native_bindings(text,remaining,[{'span':'不存在','tokenId':'N3'}],removed))
self.assertIsNone(rebase_native_bindings(text,remaining,[{'span':'%。设备','tokenId':'N3'}],removed))
source={'sourceId':'D1','text':text,'clauseCatalog':[]}
row={'sourceId':'D1','role':'core','factIds':['F1'],'supported':True,'statisticalClaimsSupported':True,
'unsupportedClauses':['设备99.0异常。'],'remainingClaimsSupported':True,'remainingStatisticalClaimsSupported':True,
'bindings':[{'span':'99.0','tokenId':'N999'}]}
with self.assertRaises(NativeCompositionError):_native_draft_candidate({'reviews':[row]},[source],{},tokens={'N1':{'nativeCatalogIssued':True}})
repeated='数值10 kg。再述10 kg。'
removed=[(0,8)];remaining=repeated[8:]
self.assertIsNone(rebase_native_bindings(repeated,remaining,[{'span':'10','tokenId':'N1'}],removed))
self.assertEqual(rebase_native_bindings(repeated,remaining,[{'span':'10','tokenId':'N1','occurrence':2}],removed),
[{'span':'10','tokenId':'N1','occurrence':1}])
class NativeStructurePipelineTests(unittest.TestCase):
def test_list_metrics_obey_owner_period_and_field_relationships(self):
invoke=GroundedNativeContractTests().invoke
draft='### 下钻:部门甲内部构成\n\n- 黑片:报废重量684.474 kg→615.249 kg(-69.225 kg);其中外废-69.225 kg。'
docs,context=GroundedNativeContractTests().fixture();context['_agentTask']['filters']={'department':'部门甲'}
docs[0]['result']['filterBinding']={'department':'部门甲'}
good,calls=invoke(draft,fixture=(docs,context));self.assertTrue(good['evidenceSufficient'],good['compositionDiagnostics']['issues'])
self.assertEqual(len(calls),1)
for wrong in (draft.replace('- 黑片:','- 亮片:'),draft.replace('684.474 kg→615.249','615.249 kg→684.474')):
bad,_=invoke(wrong,fixture=(docs,context));self.assertFalse(bad['evidenceSufficient'])
def test_protected_bindings_remap_after_pool_reordering_by_fact_and_measure(self):
docs,context=GroundedNativeContractTests().fixture();report=build_native_report(docs,context)
sources,meta=_prepare_native_draft('9月黑片外废报废重量615.2 kg。',report['facts'],context)
self.assertGreater(meta['protectedMetricSpans'],0)
reordered=list(reversed(report['facts']));tokens=_native_atomic_inventory(reordered)
presented=_native_present_sources(sources,reordered)
for source in presented:
self.assertEqual(len(source['protectedBindings']),len(source['_protected']) if '_protected' in source else meta['protectedMetricSpans'])
for binding in source['protectedBindings']:
token=tokens[binding['tokenId']]
self.assertEqual(token['factId'],binding['factId'])
self.assertEqual(token['metricField'],'categoryWeight');self.assertEqual(token['rawValue'],'615.249')
def test_ordered_table_and_prose_levels_keep_period_guards(self):
invoke=GroundedNativeContractTests().invoke
table='9月黑片外废报废。\n\n| 阶段 | 报废重量(9月/8月,kg) |\n|---|---|\n| 黑片外废 | 615.2 / 684.5 |'
good,calls=invoke(table);self.assertTrue(good['evidenceSufficient'],good['compositionDiagnostics']['issues'])
self.assertEqual(len(calls),1)
bad,_=invoke(table.replace('9月/8月','8月/9月'));self.assertFalse(bad['evidenceSufficient'])
good,_=invoke('9月黑片外废报废重量从684.474 kg降至615.249 kg。')
self.assertTrue(good['evidenceSufficient'],good['compositionDiagnostics']['issues'])
bad,_=invoke('9月黑片外废报废重量从615.249 kg降至684.474 kg。');self.assertFalse(bad['evidenceSufficient'])
bad,_=invoke('8月黑片外废报废重量从684.474 kg降至615.249 kg。');self.assertFalse(bad['evidenceSufficient'])
def test_heading_ordinal_stays_visible_without_becoming_fact(self):
final,calls=GroundedNativeContractTests().invoke('### 1. 关键报废\n\n9月黑片外废报废重量615.2 kg。')
self.assertTrue(final['evidenceSufficient'],final['compositionDiagnostics']['issues']);self.assertEqual(len(calls),1)
self.assertIn('### 1. 关键报废',final['answer'])
if __name__=='__main__':unittest.main()