pfs.cite (Cite + builders; rule_label moved from llm.lineage and re-exported), pfs.citecheck (sentence split, number normalization, check_prose; plus grounding_problems: words, codes and CMS-vs-ours attribution of model prose), pfs.dossier (facts with resolved cites from every Task 1-12 table, timeline, stakeholder panels with names from pfs.commenter, stance accuracy beside every count, computed coverage gaps, ledgered model prose with template fallback, pfs.dossier table), llm.search item_keys filter, `stack pfs dossier`. Lineage rows reach the dossier only when a final rule's own plain sentence states them. HOME-VISITS live: 558 facts, 28 timeline events, check passed, prose model:qwen2.5:14b+template (8 of 29 units on templates).
215 lines
6.3 KiB
Python
215 lines
6.3 KiB
Python
"""pfs.citecheck — the sentence-level citation check (dossier task 13)."""
|
||
|
||
from __future__ import annotations
|
||
|
||
import pytest
|
||
|
||
from pfs.cite import Cite
|
||
from pfs.citecheck import (
|
||
Problem,
|
||
check_prose,
|
||
grounding_problems,
|
||
number_tokens,
|
||
split_sentences,
|
||
)
|
||
from pfs.dossier import Fact
|
||
|
||
|
||
def fact(text, values=None, fid="F1"):
|
||
return Fact(
|
||
id=fid,
|
||
kind="round",
|
||
year=2023,
|
||
codes=(),
|
||
text=text,
|
||
values=values or {},
|
||
cite=Cite(kind="fr_para", label="x", url="u"),
|
||
)
|
||
|
||
|
||
F = {
|
||
"F1": fact(
|
||
"For CY2023, the RUC recommended a work RVU of 1.00 for 99341 and CMS "
|
||
"finalized 1.00 (accept).",
|
||
{"final": 1.0},
|
||
),
|
||
"F2": fact(
|
||
"For CY2023, the RUC recommended a work RVU of 1.65 for 99342 and CMS "
|
||
"finalized 1.65 (accept).",
|
||
{"final": 1.65},
|
||
"F2",
|
||
),
|
||
"F3": fact(
|
||
"In CY2023 the payment for 99350 was $131.17 and the total changed −16.5%.",
|
||
{},
|
||
"F3",
|
||
),
|
||
}
|
||
|
||
|
||
def test_uncited_sentence_rejected():
|
||
assert check_prose("CMS accepted the RUC values.", F)[0].kind == "uncited"
|
||
|
||
|
||
def test_number_from_uncited_fact_rejected():
|
||
[p] = check_prose("99341 was finalized at 1.65 [F1].", F)
|
||
assert p.kind == "number_mismatch"
|
||
assert "1.65" in p.detail
|
||
|
||
|
||
def test_equivalent_number_formats_accepted():
|
||
assert check_prose("In CY2023 99341 was set at 1.0 work RVUs [F1].", F) == []
|
||
|
||
|
||
def test_unknown_fact_rejected():
|
||
[p] = check_prose("CMS finalized 1.00 for 99341 [F9].", F)
|
||
assert p.kind == "unknown_fact"
|
||
assert "F9" in p.detail
|
||
|
||
|
||
def test_abbreviations_do_not_split():
|
||
text = (
|
||
"The U.S. statute, e.g. section 1833, i.e. the Act, No. 5 and p. 49 "
|
||
"set 1.00 for 99341 [F1]."
|
||
)
|
||
assert split_sentences(text) == [text]
|
||
assert len(split_sentences("A one [F1]. B two [F2].")) == 2
|
||
|
||
|
||
def test_tags_after_the_period_stay_with_their_sentence():
|
||
assert split_sentences("CMS finalized 1.00 for 99341. [F1] Next [F2].") == [
|
||
"CMS finalized 1.00 for 99341. [F1]",
|
||
"Next [F2].",
|
||
]
|
||
assert check_prose("CMS finalized 1.00 for 99341 in CY2023. [F1]", F) == []
|
||
|
||
|
||
def test_money_and_percent_normalized():
|
||
assert number_tokens("$131.17") == {"131.17"}
|
||
assert number_tokens("−16.5%") == number_tokens("-16.5%") == {"-16.5%"}
|
||
assert number_tokens("–16.5%") == {"-16.5%"}
|
||
assert check_prose("99350 paid 131.17 in CY2023 and moved -16.5% [F3].", F) == []
|
||
|
||
|
||
def test_number_tokens_normalization():
|
||
assert number_tokens("1.00 3.60 +2 1,234 25th 2.0") == {
|
||
"1",
|
||
"3.6",
|
||
"2",
|
||
"1234",
|
||
"25th",
|
||
}
|
||
# bracketed segments (fact tags, labels) carry no numbers
|
||
assert number_tokens("x [F12] [87 FR 69608 ¶1672] y") == set()
|
||
# a range is two codes, never a negative number
|
||
assert number_tokens("99324–99328") == {"99324", "99328"}
|
||
# CY years and docket ids
|
||
assert number_tokens("CY2023") == {"2023"}
|
||
assert number_tokens("CMS-2022-0113") == {"2022", "0113"}
|
||
# HCPCS letters codes are whole tokens; modifiers carry no number
|
||
assert number_tokens("G0318 and B1") == {"G0318"}
|
||
|
||
|
||
def test_values_count_as_supported_numbers():
|
||
facts = {"F1": fact("CMS finalized the value for 99341 (accept).", {"final": 1.0})}
|
||
assert check_prose("99341 was finalized at 1.00 [F1].", facts) == []
|
||
facts = {"F1": fact("x", {"share": "12.5%", "label": "n/a"})}
|
||
assert check_prose("It was 12.5% [F1].", facts) == []
|
||
|
||
|
||
def test_several_tags_in_one_bracket():
|
||
assert check_prose("99341 and 99342 got 1.00 and 1.65 [F1, F2].", F) == []
|
||
|
||
|
||
def test_problem_carries_the_sentence():
|
||
p = check_prose("Uncited one. Fine 99341 1.00 [F1].", F)
|
||
assert p == [Problem("Uncited one.", "uncited", "no [F#] tag")]
|
||
|
||
|
||
@pytest.mark.parametrize("text", ["", " "])
|
||
def test_empty_prose_has_no_problems(text):
|
||
assert check_prose(text, F) == []
|
||
|
||
|
||
def test_zero_and_empty_values():
|
||
assert number_tokens("-0.00 and +0") == {"0"}
|
||
facts = {"F1": fact("x", {"none": None, "flag": True, "n": 0})}
|
||
assert check_prose("It was 0 [F1].", facts) == []
|
||
|
||
|
||
def _kind(text, kind="round", fid="F1", codes=()):
|
||
return Fact(fid, kind, 2023, codes, text, {}, Cite("fr_para", "x", "u"))
|
||
|
||
|
||
G = {
|
||
"F1": _kind(
|
||
"For CY2008, the RUC recommended a work RVU of 2.27 for 99326 and CMS finalized 2.63 (refine).",
|
||
codes=("99326",),
|
||
),
|
||
"F2": _kind(
|
||
"For CY2008, CMS finalized a work RVU of 3.28 for 99350.",
|
||
fid="F2",
|
||
codes=("99350",),
|
||
),
|
||
"F3": _kind(
|
||
"CMS reported that commenters supported the proposal (CY2005 final rule).",
|
||
"cms_summary",
|
||
"F3",
|
||
),
|
||
"F4": _kind(
|
||
"Of 3 CY2023 commenters we classified on the work RVU valuation, 1 supported.",
|
||
"stance_split",
|
||
"F4",
|
||
),
|
||
}
|
||
|
||
|
||
def test_grounded_restatement_passes():
|
||
assert (
|
||
grounding_problems(
|
||
"For CY2008, CMS finalized work RVUs for 99326 and 99350 [F1][F2].", G
|
||
)
|
||
== []
|
||
)
|
||
assert grounding_problems("No tag here.", G) == [] # left to check_prose
|
||
|
||
|
||
def test_invented_words_are_ungrounded():
|
||
[p] = grounding_problems(
|
||
"The CPT 2019 codebook introduced new codes for office visits [F1].", G
|
||
)
|
||
assert p.kind == "ungrounded" and "introduc" in p.detail
|
||
|
||
|
||
def test_a_range_spanning_uncited_codes_is_rejected():
|
||
[p] = grounding_problems(
|
||
"CMS finalized work RVUs for 99326 through 99350 [F1][F2].", G
|
||
)
|
||
assert p.kind == "code_mismatch" and "99341" in p.detail
|
||
|
||
|
||
def test_cms_summary_stays_attributed_and_apart():
|
||
[p] = grounding_problems(
|
||
"Commenters supported the proposal (CY2005 final rule) [F3].", G
|
||
)
|
||
assert p.kind == "unattributed"
|
||
assert (
|
||
grounding_problems(
|
||
"CMS reported that commenters supported the proposal [F3].", G
|
||
)
|
||
== []
|
||
)
|
||
[m] = grounding_problems(
|
||
"CMS reported that commenters supported; we classified 3 [F3][F4].", G
|
||
)
|
||
assert m.kind == "merged"
|
||
[u] = grounding_problems(
|
||
"Of 3 CY2023 commenters, 1 supported the work RVU valuation [F4].", G
|
||
)
|
||
assert u.kind == "unattributed" and "classified" in u.detail
|
||
|
||
|
||
def test_a_range_the_fact_writes_may_be_restated():
|
||
facts = {"F1": _kind("The Panel deleted codes 99324-99328.", "formation")}
|
||
assert grounding_problems("The Panel deleted codes 99324-99328 [F1].", facts) == []
|