Files
quality_analyse/verification/test_native_subject_references.py

66 lines
4.1 KiB
Python
Raw Permalink 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.
"""Rank/set references identify signed members before inspecting quantities."""
import copy,sys,unittest
from pathlib import Path
sys.path[:0]=[str(Path(__file__).resolve().parents[1]/'scripts'),str(Path(__file__).parent)]
import nao_native_answer as n
import test_quantity_comparison_bounds as quantities
class SignedSubjectReferenceTests(unittest.TestCase):
def facts(self):
facts=quantities.QuantityComparisonPipelineTests().history_report()['facts']
for fact,raw,display in zip(facts,('0.9500708964391122','0.8245282495352184','0.9582524149985584'),('95.01%','82.45%','95.83%')):
fact['text']+='综合合格率'+display+'。'
fact['verifiedDisplayValues'].append({'metricCode':'compositeRate','rawValue':raw,'rawUnit':'ratio','unit':'percent','displayValue':display})
return facts
def gate(self,text,*,facts=None,blocks=None):
facts=facts or self.facts();tokens=n._binding_inventory(facts);diagnostics={}
parsed={'blocks':blocks or [{'role':'core','text':text,'factIds':[fact['id'] for fact in facts]}]}
parsed=n._complete_identifier_fact_bindings(parsed,facts,tokens,diagnostics)
parsed=n._bind_comparison_bounds(parsed,facts,tokens,diagnostics)
parsed=n._bind_exact_identifiers(parsed,tokens,diagnostics)
parsed=n._bind_exact_periods(parsed,tokens,diagnostics)
parsed=n._bind_literal_measures(parsed,tokens,diagnostics,facts=facts)
result,_=n._validate_blocks(parsed,facts,tokens,diagnostics=diagnostics)
return result,diagnostics
def test_rank_reference_uses_formal_rank_for_both_stage_values(self):
text='### 2026年8月 / 全公司 Top3\n\n首位规格为亮片708.6324 kg、黑片446.851 kg。'
result,_=self.gate(text)
self.assertIn('亮片708.6324 kg、黑片446.851 kg',result[0]['text'])
result,_=self.gate(text.replace('首位','第1位'))
self.assertIn('第1位规格',result[0]['text'])
def test_complement_excludes_only_the_unambiguous_preceding_subject(self):
text=('### 2026年8月 / 全公司 Top3\n\n规格乙综合合格率82.45%,'
'且其黑片报废重量659.65 kg高于亮片446.4221 kg;另两项综合合格率均在95%以上。')
result,diagnostics=self.gate(text)
self.assertIn('另两项综合合格率均在95%以上',result[0]['text'])
self.assertEqual(diagnostics['comparisonBoundBindings'][0]['owners'],['F1','F3'])
changed=text+'后续规格乙另行核查。'
_,again=self.gate(changed)
self.assertEqual(again['comparisonBoundBindings'][0]['owners'],['F1','F3'])
def test_wrong_rank_stage_scope_and_explicit_name_are_not_rescued(self):
for text in ('第4位规格为亮片708.6324 kg。','首位规格为亮片446.851 kg。',
'首位规格规格乙为亮片446.4221 kg。','9月首位规格为亮片708.6324 kg。'):
with self.subTest(text=text),self.assertRaises(n.NativeCompositionError):self.gate(text)
def test_complement_needs_a_complete_unique_antecedent_and_matching_count(self):
for text in ('另两项综合合格率均在95%以上。',
'规格甲综合合格率95.01%,规格乙综合合格率82.45%;另两项综合合格率均在95%以上。',
'规格乙综合合格率82.45%;另三项综合合格率均在95%以上。'):
with self.subTest(text=text),self.assertRaises(n.NativeCompositionError):self.gate(text)
def test_first_feedback_aggregates_literal_and_reference_gaps(self):
facts=self.facts();ids=[fact['id'] for fact in facts]
blocks=[{'role':'core','text':'另两项综合合格率均在95%以上。','factIds':ids},
{'role':'key_evidence','text':'第4位规格为亮片708.6324 kg。','factIds':ids}]
with self.assertRaises(n.NativeCompositionError) as caught:self.gate('',facts=facts,blocks=blocks)
self.assertIn('blocks[0]',str(caught.exception));self.assertIn('blocks[1]',str(caught.exception))
self.assertIn('rank_reference',str(caught.exception))
if __name__=='__main__':unittest.main()