Skip to content

Commit 61560d3

Browse files
authored
Merge pull request #47 from linkml/worktree-fix-reference-substring
Match the generic "reference" term as a token, not a bare substring
2 parents 3e79833 + 235cce7 commit 61560d3

3 files changed

Lines changed: 92 additions & 2 deletions

File tree

‎src/linkml_reference_validator/field_detection.py‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,34 @@
2929
full URIs before matching.
3030
"""
3131

32+
import re
3233
from dataclasses import dataclass
3334
from typing import TYPE_CHECKING, Callable, Optional, Protocol
3435

3536
if TYPE_CHECKING:
3637
from curies import Converter
3738

3839

40+
def _uri_tokens(uri: str) -> set[str]:
41+
"""Split a URI/CURIE into lowercased word tokens.
42+
43+
Splits on camelCase boundaries and any run of non-alphanumeric characters,
44+
so a generic term can be matched as a whole word rather than as a bare
45+
substring (which would flag ``user_preference`` or ``dereference`` as
46+
containing "reference").
47+
48+
Examples:
49+
>>> sorted(_uri_tokens("http://example.org/myReferenceField"))
50+
['example', 'field', 'http', 'my', 'org', 'reference']
51+
>>> sorted(_uri_tokens("test:user_preference"))
52+
['preference', 'test', 'user']
53+
>>> sorted(_uri_tokens("dcterms:references"))
54+
['dcterms', 'references']
55+
"""
56+
spaced = re.sub(r"(?<=[a-z0-9])(?=[A-Z])", " ", uri)
57+
return {tok.lower() for tok in re.split(r"[^A-Za-z0-9]+", spaced) if tok}
58+
59+
3960
# =============================================================================
4061
# URI Constants
4162
# =============================================================================
@@ -126,20 +147,32 @@ class ReferenceURIs:
126147
LEGACY_LINKML: str = "https://w3id.org/linkml/authoritative_reference"
127148
LEGACY_LINKML_PREFIXED: str = "linkml:authoritative_reference"
128149

129-
# Additional patterns to match (substrings)
150+
# Unambiguous patterns matched as substrings (e.g. dcterms:source has no
151+
# generic single-word form to confuse).
130152
MATCH_PATTERNS: tuple[str, ...] = (
131153
"dcterms:references",
132154
"dc/terms/references",
133155
"dcterms:source",
134156
"dc/terms/source",
135157
"authoritative_reference",
158+
)
159+
160+
# Generic terms matched as whole word tokens, NOT bare substrings, so that
161+
# "user_preference" / "dereference" are not mistaken for references.
162+
TOKEN_TERMS: tuple[str, ...] = (
136163
"reference",
164+
"references",
137165
)
138166

139167
@classmethod
140168
def is_reference_uri(cls, uri: str) -> bool:
141169
"""Check if a URI identifies a reference field.
142170
171+
Specific Dublin Core / legacy URIs are matched as substrings. The
172+
generic word "reference(s)" is matched only as a whole token, so URIs
173+
that merely contain those letters (``user_preference``, ``dereference``)
174+
are not misclassified.
175+
143176
Args:
144177
uri: URI string to check (can be full or prefixed)
145178
@@ -155,11 +188,19 @@ def is_reference_uri(cls, uri: str) -> bool:
155188
True
156189
>>> ReferenceURIs.is_reference_uri("https://w3id.org/linkml/authoritative_reference")
157190
True
191+
>>> ReferenceURIs.is_reference_uri("http://example.org/myReferenceField")
192+
True
158193
>>> ReferenceURIs.is_reference_uri("oa:exact")
159194
False
195+
>>> ReferenceURIs.is_reference_uri("test:user_preference")
196+
False
197+
>>> ReferenceURIs.is_reference_uri("ex:dereference")
198+
False
160199
"""
161200
uri_lower = uri.lower()
162-
return any(pattern in uri_lower for pattern in cls.MATCH_PATTERNS)
201+
if any(pattern in uri_lower for pattern in cls.MATCH_PATTERNS):
202+
return True
203+
return bool(_uri_tokens(uri) & set(cls.TOKEN_TERMS))
163204

164205

165206
@dataclass(frozen=True)

‎tests/test_field_detection.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,53 @@ def test_multiple_implements(self):
166166
assert is_excerpt_slot(slot) is True
167167

168168

169+
class TestReferenceURITokenBoundaries:
170+
"""The generic 'reference' term must match as a token, not a bare substring.
171+
172+
A bare substring match flags unrelated URIs like ``user_preference`` or
173+
``dereference`` (both contain the letters 'reference'), which would cause
174+
the plugin to treat those slots as authoritative references. Matching the
175+
word as a camelCase / separator-delimited token avoids the false positives
176+
while still recognising real reference fields.
177+
"""
178+
179+
@pytest.mark.parametrize(
180+
"uri",
181+
[
182+
"test:user_preference",
183+
"ex:dereference",
184+
"http://example.org/userPreference",
185+
"schema:preferenceOrder",
186+
],
187+
)
188+
def test_non_reference_terms_rejected(self, uri: str):
189+
"""URIs where 'reference' is only a substring of another word."""
190+
assert ReferenceURIs.is_reference_uri(uri) is False
191+
192+
@pytest.mark.parametrize(
193+
"uri",
194+
[
195+
# Specific canonical / legacy forms must keep matching
196+
"dcterms:references",
197+
"http://purl.org/dc/terms/references",
198+
"dcterms:source",
199+
"linkml:authoritative_reference",
200+
# Generic 'reference' token in various shapes
201+
"http://example.org/myReferenceField",
202+
"test:reference",
203+
"ex:cross_reference",
204+
],
205+
)
206+
def test_real_reference_terms_accepted(self, uri: str):
207+
"""Genuine reference fields must still be detected."""
208+
assert ReferenceURIs.is_reference_uri(uri) is True
209+
210+
def test_slot_with_preference_uri_not_a_reference(self):
211+
"""A slot whose URI merely contains 'preference' is not a reference."""
212+
slot = Mock(implements=["test:user_preference"], slot_uri=None)
213+
assert is_reference_slot(slot) is False
214+
215+
169216
class TestIsReferenceSlot:
170217
"""Tests for is_reference_slot function."""
171218

‎tests/test_title_validation.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,12 @@ def test_validate_with_title_field(self, plugin, mocker):
318318
# Setup schema view
319319
mock_slot_ref = mocker.MagicMock()
320320
mock_slot_ref.implements = ["linkml:authoritative_reference"]
321+
mock_slot_ref.slot_uri = None
321322
mock_slot_ref.range = None
322323

323324
mock_slot_excerpt = mocker.MagicMock()
324325
mock_slot_excerpt.implements = ["linkml:excerpt"]
326+
mock_slot_excerpt.slot_uri = None
325327
mock_slot_excerpt.range = None
326328

327329
mock_slot_title = mocker.MagicMock()

0 commit comments

Comments
 (0)