2929full URIs before matching.
3030"""
3131
32+ import re
3233from dataclasses import dataclass
3334from typing import TYPE_CHECKING , Callable , Optional , Protocol
3435
3536if 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 )
0 commit comments