|
| 1 | +"""A quote must match whole words, not begin or end inside one (issue #124). |
| 2 | +
|
| 3 | +Normalized parts used to be tested as bare substrings of the normalized |
| 4 | +reference, so "activates" matched inside "inactivates" and a quote stating |
| 5 | +the opposite of the reference passed. These tests pin the failing cases and |
| 6 | +the matches that must keep working. |
| 7 | +""" |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from linkml_reference_validator.models import ReferenceContent, ReferenceValidationConfig |
| 12 | +from linkml_reference_validator.validation.repairer import SupportingTextRepairer |
| 13 | +from linkml_reference_validator.validation.supporting_text_validator import SupportingTextValidator |
| 14 | + |
| 15 | + |
| 16 | +def found(quote: str, content: str) -> bool: |
| 17 | + """Whether ``quote`` validates against ``content``.""" |
| 18 | + validator = SupportingTextValidator(ReferenceValidationConfig()) |
| 19 | + reference = ReferenceContent(reference_id="PMID:1", content=content) |
| 20 | + return validator.find_text_in_reference(quote, reference).found |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize( |
| 24 | + "quote,content", |
| 25 | + [ |
| 26 | + ("activates AKT signaling", "Loss of PTEN inactivates AKT signaling in mice."), |
| 27 | + ("toxic to hepatocytes", "the compound was nontoxic to hepatocytes"), |
| 28 | + ("5 mM H2O2 induced apoptosis", "10.5 mM H2O2 induced apoptosis"), |
| 29 | + ("stable in vivo", "the variant was unstable in vivo"), |
| 30 | + ("regulated in tumors", "PTEN was downregulated in tumors"), |
| 31 | + ("PTEN is not", "PTEN is nothing like AKT"), |
| 32 | + ("the protein binds DNA", "the protein binds DNAse"), |
| 33 | + ], |
| 34 | +) |
| 35 | +def test_quote_starting_or_ending_mid_word_fails(quote, content): |
| 36 | + """A quote that begins or ends inside a word of the reference is not found.""" |
| 37 | + assert not found(quote, content) |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.parametrize( |
| 41 | + "quote,content", |
| 42 | + [ |
| 43 | + ("activates AKT signaling", "Loss of PTEN activates AKT signaling in mice."), |
| 44 | + ("PTEN", "PTEN"), |
| 45 | + ("protein functions", "The protein functions in cells."), |
| 46 | + ("binds DNA", "it binds DNA."), |
| 47 | + ("T cell receptor", "the T-cell receptor was"), |
| 48 | + ("alpha catenin", "α-catenin binds"), |
| 49 | + ("10.5 mM H2O2", "with 10.5 mM H2O2 induced"), |
| 50 | + ("loss of PTEN ... AKT signaling", "Loss of PTEN activates AKT signaling."), |
| 51 | + ], |
| 52 | +) |
| 53 | +def test_whole_word_quotes_still_match(quote, content): |
| 54 | + """Quotes that start and end on word boundaries, at any position, still match.""" |
| 55 | + assert found(quote, content) |
| 56 | + |
| 57 | + |
| 58 | +def test_each_ellipsis_part_is_held_to_word_boundaries(): |
| 59 | + """The boundary rule applies to every part of a split quote, not just the whole.""" |
| 60 | + assert not found("Loss of PTEN ... activates AKT", "Loss of PTEN inactivates AKT signaling.") |
| 61 | + |
| 62 | + |
| 63 | +def test_figure_labels_are_matched_on_word_boundaries(): |
| 64 | + """Each searchable text (an SVG figure label) is held to word boundaries too.""" |
| 65 | + validator = SupportingTextValidator(ReferenceValidationConfig()) |
| 66 | + match = validator._substring_match( |
| 67 | + ["activates AKT"], "", searchable=["PTEN inactivates AKT", "mTOR"] |
| 68 | + ) |
| 69 | + assert not match.found |
| 70 | + |
| 71 | + |
| 72 | +def test_repair_does_not_confirm_a_mid_word_match(): |
| 73 | + """Character normalization is not offered as a fix when it only matches mid-word.""" |
| 74 | + reference = ReferenceContent( |
| 75 | + reference_id="PMID:1", content="Treatment with nonCO₂ gas activates AKT." |
| 76 | + ) |
| 77 | + result = SupportingTextRepairer(ReferenceValidationConfig()).attempt_repair( |
| 78 | + "CO2 gas activates AKT", "PMID:1", reference |
| 79 | + ) |
| 80 | + assert not result.is_repaired |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.parametrize( |
| 84 | + "quote,content", |
| 85 | + [ |
| 86 | + ("PTEN is a tumor suppressor", "PTEN is a tumor suppressor12 in mice."), |
| 87 | + ("PTEN is a tumor suppressor", "PTEN is a tumor suppressor1,2."), |
| 88 | + ("PTEN is a tumor suppressor", "PTEN is a tumor suppressor1-3 in mice."), |
| 89 | + ], |
| 90 | +) |
| 91 | +def test_citation_digits_welded_to_the_last_word_still_match(quote, content): |
| 92 | + """Extraction welds superscript citations onto a word; a quote ending there still matches. |
| 93 | +
|
| 94 | + JATS ``suppressor<sup><xref>12</xref></sup>``, PMC HTML and PDF text all |
| 95 | + render the citation as ``suppressor12``, so a quote of the sentence must |
| 96 | + not fail on its last word. |
| 97 | + """ |
| 98 | + assert found(quote, content) |
| 99 | + |
| 100 | + |
| 101 | +def test_citation_allowance_does_not_extend_a_number(): |
| 102 | + """Trailing digits are allowed only after a letter: ``10`` must not grow into ``105``.""" |
| 103 | + assert not found("a dose of 10", "a dose of 105 mg") |
| 104 | + |
| 105 | + |
| 106 | +def test_digit_separators_are_part_of_the_number(): |
| 107 | + """A point or comma between digits is kept, so differently punctuated numbers differ.""" |
| 108 | + assert not found("on days 3, 5 and 7", "on days 3,5 and 7") |
| 109 | + assert found("on days 3,5 and 7", "measured on days 3,5 and 7") |
| 110 | + assert found("p < 0.05", "significant (p<0.05)") |
| 111 | + |
| 112 | + |
| 113 | +def test_unmapped_glyph_still_splits_the_word(): |
| 114 | + """U+FFFD splits a damaged word, so a quote of its second half still matches.""" |
| 115 | + assert found("ammation of the gut", "in�ammation of the gut") |
| 116 | + |
| 117 | + |
| 118 | +def test_mid_word_miss_says_why_and_suggests_nothing_that_fails_again(): |
| 119 | + """A quote found only inside longer words gets a clear error, not itself as a fix.""" |
| 120 | + validator = SupportingTextValidator(ReferenceValidationConfig()) |
| 121 | + quote = "activates AKT signaling" |
| 122 | + match = validator.find_text_in_reference( |
| 123 | + quote, ReferenceContent(reference_id="PMID:1", content="Loss of PTEN inactivates AKT signaling.") |
| 124 | + ) |
| 125 | + assert not match.found |
| 126 | + assert "whole words" in (match.error_message or "") |
| 127 | + assert f'"{quote}"' not in (match.suggested_fix or "") |
| 128 | + |
| 129 | + |
| 130 | +@pytest.mark.parametrize("quote", ["activates AKT signaling", "activates AKT signaling in mice"]) |
| 131 | +def test_repair_never_returns_the_failing_quote_as_its_fix(quote): |
| 132 | + """Fuzzy repair must not 'fix' a mid-word miss by returning the same quote.""" |
| 133 | + reference = ReferenceContent( |
| 134 | + reference_id="PMID:1", |
| 135 | + content="Loss of PTEN inactivates AKT signaling in mice. Other text about things.", |
| 136 | + ) |
| 137 | + result = SupportingTextRepairer(ReferenceValidationConfig()).attempt_repair( |
| 138 | + quote, "PMID:1", reference |
| 139 | + ) |
| 140 | + assert not (result.is_repaired and result.repaired_text == quote) |
| 141 | + |
| 142 | + |
| 143 | +def test_fuzzy_exact_stage_requires_whole_words(): |
| 144 | + """The fuzzy matcher's exact shortcut does not accept a mid-word hit.""" |
| 145 | + from linkml_reference_validator.validation.fuzzy_text_utils import find_fuzzy_match_in_text |
| 146 | + |
| 147 | + found_, score, _ = find_fuzzy_match_in_text("activates AKT", "PTEN inactivates AKT.") |
| 148 | + assert score < 100 |
| 149 | + assert find_fuzzy_match_in_text("activates AKT", "PTEN activates AKT.") == ( |
| 150 | + True, 100.0, "activates AKT" |
| 151 | + ) |
0 commit comments