Skip to content

Commit 2324933

Browse files
authored
Merge pull request #151 from linkml/ccr-65a5480d-pb91sk
Keep comparison, plus-minus and minus signs through normalization
2 parents bc666f1 + bc944d9 commit 2324933

5 files changed

Lines changed: 164 additions & 8 deletions

File tree

‎docs/concepts/how-it-works.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ Before matching, both the supporting text and reference content are normalized:
1515
- **Lowercased**: `"MUC1"` → `"muc1"`
1616
- **Punctuation removed**: `"c-Abl"` → `"c abl"` (a point or comma between
1717
digits is kept, so `"10.5"` stays one number)
18+
- **Meaning-bearing symbols kept as words**: `<` `>` `≤` `≥` become `lt` `gt`
19+
`le` `ge`, `±` (or `+/-`) becomes `pm`, and a minus sign before a number stays
20+
on it, so `"p < 0.05"` does not match `"p > 0.05"` and `"2.5 fold"` does not
21+
match `"-2.5 fold"`. ASCII and Unicode spellings are equivalent (`<=` and `≤`,
22+
`-` and `−`). A hyphen after a word or digit (`IL-6`, `3-5`) is still
23+
punctuation.
1824
- **Whitespace collapsed**: Multiple spaces become single space
1925
- **Editorial notes removed**: `"[mucin 1]"` → `""`
2026

‎docs/how-to/repair-validation-errors.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,11 @@ A mapping that starts and ends with punctuation, such as `+/-`, applies
119119
anywhere, so `5+/-2` becomes `5±2`. All mappings are applied in one pass,
120120
longest first.
121121

122-
Validation ignores punctuation, spells out Greek letters and folds ligatures,
123-
so it cannot confirm a mapping that changes only those, such as `+-` → `±` or
124-
a custom `alpha` → `α`. Such a mapping is kept only when the reference
125-
contains the repaired text exactly as written, apart from case and whitespace.
122+
Validation reads `+-` (except before a letter), `+/-` and `±` all as the same
123+
word, ignores other punctuation, spells out Greek letters and folds ligatures,
124+
so it cannot confirm a mapping that changes only those, such as `+-` → `±` or a
125+
custom `alpha` → `α`. Such a mapping is kept only when the reference contains
126+
the repaired text exactly as written, apart from case and whitespace.
126127
That is what keeps `Ca2+-dependent` from becoming `Ca2±dependent`.
127128

128129
Quotes with `...` or `[editorial brackets]` are checked part by part, the same

‎src/linkml_reference_validator/validation/repairer.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def _all_parts_found(
235235
>>> repairer._all_parts_found("CO₂ fixation ... was [then] measured", searchable)
236236
True
237237
>>> repairer._all_parts_found("Ca2±dependent CO₂", searchable)
238-
True
238+
False
239239
>>> repairer._all_parts_found("Ca2±dependent CO₂", literal, literal=True)
240240
False
241241
>>> repairer._all_parts_found("[note]", searchable)

‎src/linkml_reference_validator/validation/supporting_text_validator.py‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -727,8 +727,12 @@ def normalize_text(text: str) -> str:
727727
Folds Latin compatibility ligatures, spells out Greek letters, removes
728728
punctuation and extra whitespace, and lowercases. A point or comma
729729
between two digits is kept, so a number such as ``10.5`` stays one
730-
word. The same normalization applies to reference content and queries
731-
(including split query parts).
730+
word. Symbols that carry meaning become words: ``<`` ``>`` ``≤`` ``≥``
731+
(and ``<=`` ``>=``) become ``lt`` ``gt`` ``le`` ``ge``, ``±`` and
732+
``+/-`` become ``pm``, and a minus sign before a number stays on it as
733+
``neg``. Arrows such as ``->`` are punctuation. The same normalization
734+
applies to reference content and queries (including split query
735+
parts).
732736
733737
IJ (IJ/ij) is folded because Unicode defines its compatibility decomposition
734738
as I + J. Distinct letters Æ/æ and Œ/œ are preserved: equating them with
@@ -758,7 +762,11 @@ def normalize_text(text: str) -> str:
758762
>>> SupportingTextValidator.normalize_text("ff fi fl ffi ffl ſt st IJ ij")
759763
'ff fi fl ffi ffl st st ij ij'
760764
>>> SupportingTextValidator.normalize_text("10.5 mM, 3,4-dihydroxy, p<0.05.")
761-
'10.5 mm 3,4 dihydroxy p 0.05'
765+
'10.5 mm 3,4 dihydroxy p lt 0.05'
766+
>>> SupportingTextValidator.normalize_text("p<0.05, 5 ± 2, by −2.5 fold")
767+
'p lt 0.05 5 pm 2 by neg2.5 fold'
768+
>>> SupportingTextValidator.normalize_text("IL-6, Ca2+-dependent, A -> B")
769+
'il 6 ca2 dependent a b'
762770
>>> SupportingTextValidator.normalize_text("Æ Œ æ œ H₂O 10⁶ µ")
763771
'æ œ æ œ h₂o 10⁶ µ'
764772
"""
@@ -802,6 +810,26 @@ def normalize_text(text: str) -> str:
802810
text = text.replace(greek, spelled)
803811

804812
text = text.lower()
813+
814+
# Symbols that carry meaning become words before punctuation is
815+
# stripped, so a quote that reverses a comparison or drops a sign
816+
# fails (issue #125). Arrows go first, or "->" would read as ">"; a
817+
# "<-" before a digit is "<" then a negative number, not an arrow.
818+
text = re.sub(r"<[-=]+>|[-=]+>|<-+(?!\d)", " ", text)
819+
text = re.sub(r"<=|[≤⩽]", " le ", text)
820+
text = re.sub(r">=|[≥⩾]", " ge ", text)
821+
text = text.replace("<", " lt ").replace(">", " gt ")
822+
# A bare "+-" before a letter is a charge then a hyphen
823+
# ("Ca2+-dependent"); "+/-" is plus-minus even before one ("+/-SD").
824+
text = re.sub(r"±|\+/[-−]|\+[-−](?![^\W\d_])", " pm ", text)
825+
# A minus sign stays on its number, so "2.5" does not match "-2.5".
826+
# It is a sign only at the start, after a space, an opening bracket or
827+
# curly quote, "=", ":", "~", "≈" or an exponent's "^" (10^-5 is not
828+
# 10^5); anywhere else it is a hyphen or a range dash: IL-6, 3-5,
829+
# 3'-5', 10%-20%, 5°-10°. Straight quotes are left out because they
830+
# double as primes and inch marks (5"-10").
831+
text = re.sub(r"(?:^|(?<=[\s(\[{=:~≈^“‘«]))[-−](?=\d)", "neg", text)
832+
805833
# A point or comma between digits is part of the number: splitting
806834
# "10.5" into "10 5" would let a quote of "5 mM" match "10.5 mM".
807835
text = re.sub(r"(?!(?<=\d)[.,](?=\d))[^\w\s]", " ", text)

‎tests/test_meaning_symbols.py‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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

Comments
 (0)