"""A preceding measure may dominate examples, never a token's own field.""" import sys,unittest from pathlib import Path sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'scripts')) from nao_native_contract import native_span_structure class ExamplePredicateStructureTests(unittest.TestCase): def locate(self,text,token): start=text.index(token);return native_span_structure(text).locate(start,start+len(token)) def test_actual_repair_example_inherits_only_the_measure(self): text=('- 9月Top名单各规格亮片报废重量均高于黑片,例如 {{I13}} 为亮片 {{N115}} kg、黑片 {{N114}} kg,' '{{I8}} 为亮片 {{N65}} kg、黑片 {{N64}} kg;8月Top名单中 {{I11}} 则以黑片 {{N82}} kg 高于亮片 {{N83}} kg。') for token,stage in [('N115','bright'),('N114','black'),('N65','bright'),('N64','black'),('N82','black'),('N83','bright')]: location=self.locate(text,'{{'+token+'}}') self.assertEqual(location['metricFamily'],'scrap') self.assertEqual(location['ownerStage'],stage) self.assertEqual(location['fieldAssociation']['kind'],'governing_predicate') self.assertEqual(location['fieldAssociation']['label'],'报废重量') self.assertNotIn('9月',location['fieldAssociation']['label']) self.assertNotIn('亮片',location['fieldAssociation']['label']) self.assertIn('8月',self.locate(text,'{{N82}}')['prefix']) def test_actual_initial_sort_frame_governs_the_following_observation(self): text='**2026年9月 / 消费电子产业部 / 消费电子-3C** 按规格合计报废重量排序,{{I1}} 以 {{N9}} kg 居首,综合合格率 {{N10}}。' location=self.locate(text,'{{N9}}') self.assertEqual(location['metricFamily'],'scrap') self.assertEqual(location['fieldAssociation']['label'],'报废重量') self.assertNotIn('消费电子',location['fieldAssociation']['label']) self.assertIsNone(location['ownerStage']) self.assertEqual(self.locate(text,'{{N10}}')['metricFamily'],'rate') def test_explicit_period_and_stage_do_not_inherit_from_field_frame(self): text='9月亮片报废重量较高,8月{{I1}}黑片{{N1}} kg。' location=self.locate(text,'{{N1}}') self.assertEqual(location['metricFamily'],'scrap') self.assertEqual(location['ownerStage'],'black') self.assertNotIn('9月',location['predicateContext']) self.assertNotIn('亮片',location['predicateContext']) def test_local_nonmass_fields_bar_the_prior_mass_frame(self): for label,family in [('报废率','scrap_rate'),('报废重量占比','share'),('记录数','count')]: text='报废重量排序,'+label+'另述,{{I1}}以{{N1}} kg居首。' self.assertEqual(self.locate(text,'{{N1}}')['metricFamily'],family) def test_rank_table_exposes_original_cells_and_zero_based_column(self): text='| 排名 | 规格 | 综合报废重量 | 综合合格率 |\n|---|---|---|---|\n| 1 | {{I1}} | {{N9}} kg | {{N10}} |' location=self.locate(text,'{{N9}}') self.assertEqual(location['columnIndex'],2) self.assertEqual(location['rowLabel'],'1') self.assertEqual([cell['text'].strip() for cell in location['rowCells']],['1','{{I1}}','{{N9}} kg','{{N10}}']) for cell in location['rowCells']: self.assertEqual(cell['text'],text[cell['start']:cell['end']]) self.assertEqual(location['metricFamily'],'scrap') def test_cells_expose_ambiguity_without_inventing_a_subject_or_field(self): text='| 排名 | 规格 | 其他规格 | 综合合格率 |\n|---|---|---|---|\n| 1 | {{I1}} | {{I2}} | {{N1}} |' location=self.locate(text,'{{N1}}') self.assertEqual(location['columnIndex'],3) self.assertEqual(location['rowLabel'],'1') self.assertEqual(location['metricFamily'],'rate') self.assertEqual(location['ownerStage'],'overall') self.assertEqual([cell['text'].strip() for cell in location['rowCells']][1:3],['{{I1}}','{{I2}}']) def test_explicit_new_field_overrides_and_future_text_cannot_authorize(self): text='报废重量较高,例如{{I1}}为黑片{{N1}} kg,投入重量较低,例如{{I2}}为亮片{{N2}} kg。' self.assertEqual(self.locate(text,'{{N1}}')['metricFamily'],'scrap') self.assertEqual(self.locate(text,'{{N2}}')['metricFamily'],'input') future='例如{{I1}}为黑片{{N1}} kg,后面另述报废重量较高。' self.assertIsNone(self.locate(future,'{{N1}}')['metricFamily']) stopped='报废重量较高。另段例如{{I1}}为黑片{{N1}} kg。' self.assertIsNone(self.locate(stopped,'{{N1}}')['metricFamily']) def test_rate_share_and_new_list_are_not_mass_antecedents(self): for label in ('报废率','报废重量占比','报废份额','记录数'): text=label+'较高,例如{{I1}}为黑片{{N1}} kg。' self.assertNotEqual(self.locate(text,'{{N1}}')['metricFamily'],'scrap') text='- 报废重量较高,例如{{I1}}为黑片{{N1}} kg。\n- 另述例如{{I2}}为亮片{{N2}} kg。' self.assertIsNone(self.locate(text,'{{N2}}')['metricFamily']) if __name__=='__main__':unittest.main()