|
| 1 | +"""Symbols that carry meaning survive normalization (issue #125). |
| 2 | +
|
| 3 | +Normalization used to replace all punctuation with spaces, including the |
| 4 | +comparison operators, the plus-minus sign and the minus sign of a number, |
| 5 | +so a quote that reversed a comparison or the sign of a value still passed. |
| 6 | +These tests pin those cases, and 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 | + ("difference was significant (p < 0.05)", "the difference was significant (p > 0.05)"), |
| 27 | + ("significant (p > 0.05)", "the difference was significant (p < 0.05)"), |
| 28 | + ("p ≤ 0.01", "with p ≥ 0.01 throughout"), |
| 29 | + ("p <= 0.01", "with p >= 0.01 throughout"), |
| 30 | + ("levels decreased by -2.5 fold", "levels decreased by 2.5 fold"), |
| 31 | + ("levels decreased by 2.5 fold", "levels decreased by -2.5 fold"), |
| 32 | + ("stored at 80 °C", "samples were stored at −80 °C"), |
| 33 | + ("2.5 fold change", "a -2.5 fold change"), |
| 34 | + ("a mean of 5 2 mM", "a mean of 5 ± 2 mM"), |
| 35 | + ("10^5 M", "a dose of 10^-5 M"), |
| 36 | + ("about 2 fold", "about ~-2 fold"), |
| 37 | + ("approximately ≈ 2", "approximately ≈−2"), |
| 38 | + ("a value of 2.5", "a value of “-2.5”"), |
| 39 | + ("a value of 2.5", "a value of ‘−2.5’"), |
| 40 | + ], |
| 41 | +) |
| 42 | +def test_reversed_comparison_or_sign_fails(quote, content): |
| 43 | + """A quote that changes a comparison, a sign, or drops a ± is not found.""" |
| 44 | + assert not found(quote, content) |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parametrize( |
| 48 | + "quote,content", |
| 49 | + [ |
| 50 | + ("significant (p < 0.05)", "the difference was significant (p<0.05) overall"), |
| 51 | + ("p ≤ 0.01", "with p ≤ 0.01 throughout"), |
| 52 | + ("p <= 0.01", "with p ≤ 0.01 throughout"), |
| 53 | + ("p >= 0.01", "with p ≥ 0.01 throughout"), |
| 54 | + ("decreased by -2.5 fold", "levels decreased by −2.5 fold"), |
| 55 | + ("stored at −80 °C", "samples were stored at -80 °C"), |
| 56 | + ("a mean of 5 ± 2 mM", "a mean of 5 +/- 2 mM"), |
| 57 | + ("5 ± 2 mM", "a mean of 5±2 mM"), |
| 58 | + ("a mean of 5 ± 2 mM", "a mean of 5 +/− 2 mM"), |
| 59 | + ("a mean of 5 ± 2 mM", "a mean of 5 +− 2 mM"), |
| 60 | + ("mean ± SD", "values are mean +/-SD of three runs"), |
| 61 | + ("mean ± SEM", "values are mean ±SEM of three runs"), |
| 62 | + ("a change of -2.5", "a change of (−2.5) overall"), |
| 63 | + ("x = -5", "solved for x=-5 here"), |
| 64 | + ], |
| 65 | +) |
| 66 | +def test_same_meaning_in_other_spellings_matches(quote, content): |
| 67 | + """ASCII and Unicode spellings of the same symbol are equivalent.""" |
| 68 | + assert found(quote, content) |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.parametrize( |
| 72 | + "quote,content", |
| 73 | + [ |
| 74 | + ("IL-6 levels", "serum IL-6 levels rose"), |
| 75 | + ("IL 6 levels", "serum IL-6 levels rose"), |
| 76 | + ("COVID 19 patients", "COVID-19 patients were enrolled"), |
| 77 | + ("days 3 5", "on days 3-5 after infection"), |
| 78 | + ("pH 7.4-8.0", "a pH 7.4-8.0 buffer"), |
| 79 | + ("3'-5' exonuclease", "a 3′–5′ exonuclease activity"), |
| 80 | + ("Ca2+ dependent", "a Ca2+-dependent kinase"), |
| 81 | + ("reduced by 10-20%", "reduced by 10%-20% overall"), |
| 82 | + ('a 5-10" tilt', 'a 5"-10" tilt'), |
| 83 | + ("tilted 5-10°", "tilted 5°-10° at rest"), |
| 84 | + ("A B transition", "the A → B transition"), |
| 85 | + ("A B transition", "the A -> B transition"), |
| 86 | + ], |
| 87 | +) |
| 88 | +def test_hyphens_and_arrows_are_not_signs_or_comparisons(quote, content): |
| 89 | + """A hyphen after a word or digit, and an arrow, still normalize to a space.""" |
| 90 | + assert found(quote, content) |
| 91 | + |
| 92 | + |
| 93 | +def test_normalize_text_spells_out_meaning_symbols(): |
| 94 | + """The tokens are ordinary words, so whole-word matching handles them.""" |
| 95 | + normalize = SupportingTextValidator.normalize_text |
| 96 | + assert normalize("p<0.05") == "p lt 0.05" |
| 97 | + assert normalize("p ≥ 0.01") == normalize("p >= 0.01") == "p ge 0.01" |
| 98 | + assert normalize("5 ± 2") == normalize("5 +/- 2") == normalize("5 +- 2") == "5 pm 2" |
| 99 | + assert normalize("by −2.5 fold") == normalize("by -2.5 fold") == "by neg2.5 fold" |
| 100 | + assert normalize("IL-6 and COVID-19") == "il 6 and covid 19" |
| 101 | + assert normalize("10%-20%") == normalize("10-20%") == "10 20" |
| 102 | + assert normalize("x=-5, [-3]") == "x neg5 neg3" |
| 103 | + |
| 104 | + |
| 105 | +def test_plus_minus_repair_still_needs_the_symbol_as_written(): |
| 106 | + """``+-`` -> ``±`` is still confirmed literally, so ``Ca2+-`` is left alone.""" |
| 107 | + repairer = SupportingTextRepairer(ReferenceValidationConfig()) |
| 108 | + reference = ReferenceContent( |
| 109 | + reference_id="PMID:1", |
| 110 | + content="Ca2+-dependent CO₂ fixation was measured in HEK293 cells.", |
| 111 | + ) |
| 112 | + result = repairer.attempt_repair("Ca2+-dependent CO2 fixation", "PMID:1", reference) |
| 113 | + assert result.is_repaired |
| 114 | + assert result.repaired_text == "Ca2+-dependent CO₂ fixation" |
| 115 | + |
| 116 | + |
| 117 | +def test_titles_compare_meaning_symbols_too(): |
| 118 | + """Title validation compares normalize_text output, so a reversed comparison differs.""" |
| 119 | + normalize = SupportingTextValidator.normalize_text |
| 120 | + assert normalize("Survival with dose < 5 mg") == normalize("Survival with dose <5 mg") |
| 121 | + assert normalize("Survival with dose < 5 mg") != normalize("Survival with dose > 5 mg") |
0 commit comments