diff --git a/src/did/+did2/+schema/termIndex.m b/src/did/+did2/+schema/termIndex.m index 8a564588..acc3b9d5 100644 --- a/src/did/+did2/+schema/termIndex.m +++ b/src/did/+did2/+schema/termIndex.m @@ -9,7 +9,7 @@ % main): nothing can be checked against it % count the number of registered terms % byNode curieKey(node) -> the registered term {node, name, status, -% number_unit, equivalent} (number_unit: [] or the {node, name} +% number_unit, equivalent, synonyms} (number_unit: [] or the {node, name} % of the unit the term's number takes; equivalent: a cellstr of % the ids in other vocabularies that mean the same thing, {} when % the row states none or the registry predates the column -- @@ -17,6 +17,19 @@ % byName did2.schema.termNameKey(name) -> the registered term, for every % term (case, spaces, underscores and hyphens ignored: [term % names match ignoring spacing], Jess Haley, 2026-10-29) +% byEquivalent curieKey(id) -> the registered term, for each id in a +% row's `equivalent` list (a node a document written before the +% row's node was chosen may still hold, e.g. NCIm:CL1445492 for +% CHEBI:757169 carbogen) +% bySynonym termNameKey(synonym) -> the registered term, for each +% entry in a row's `synonyms` list (spacing ignored, as byName) +% Each registered term also carries `synonyms` (a cellstr, {} when none). +% byEquivalent and bySynonym are for a READER resolving what a user typed +% ([entity search resolves a term through the registry], Jess Haley, +% 2026-11-30; VH-Lab/NDI-matlab#1332). termRegistered and registeredTerm +% do not read them: an equivalent node or a synonym is not the registered +% term, so a writer still refuses or rewrites it as before. A key already +% taken in byNode or byName is not added to byEquivalent or bySynonym. % % REGISTRY ([] = the shared schema cache's) is the struct jsondecode makes of % the file. With REGISTRY empty the index is built once per shared cache. @@ -56,7 +69,9 @@ ix = struct('present', false, 'count', 0, ... 'byNode', containers.Map('KeyType', 'char', 'ValueType', 'any'), ... - 'byName', containers.Map('KeyType', 'char', 'ValueType', 'any')); + 'byName', containers.Map('KeyType', 'char', 'ValueType', 'any'), ... + 'byEquivalent', containers.Map('KeyType', 'char', 'ValueType', 'any'), ... + 'bySynonym', containers.Map('KeyType', 'char', 'ValueType', 'any')); if ~isstruct(registry) || ~isscalar(registry) || ~isfield(registry, 'terms') return; end @@ -65,6 +80,7 @@ terms = num2cell(terms); % one row per cell: rows may differ in keys end ix.present = true; +rows = {}; for k = 1:numel(terms) t = terms{k}; if ~isstruct(t) || ~isscalar(t) @@ -72,24 +88,46 @@ end e = struct('node', textOf(t, 'node'), 'name', textOf(t, 'name'), ... 'status', textOf(t, 'status'), 'number_unit', unitOf(t), ... - 'equivalent', {equivalentOf(t)}); + 'equivalent', {listOf(t, 'equivalent')}, 'synonyms', {listOf(t, 'synonyms')}); if ~isempty(e.node) ix.byNode(did2.schema.curieKey(e.node)) = e; end if ~isempty(e.name) ix.byName(did2.schema.termNameKey(e.name)) = e; end + rows{end+1} = e; %#ok +end +% the equivalents and synonyms after every row's own node and name, so a row's +% own node or name always wins over another row's equivalent or synonym +for k = 1:numel(rows) + e = rows{k}; + for j = 1:numel(e.equivalent) + key = did2.schema.curieKey(e.equivalent{j}); + if ~isempty(key) && ~isKey(ix.byNode, key) && ~isKey(ix.byEquivalent, key) + ix.byEquivalent(key) = e; + end + end + for j = 1:numel(e.synonyms) + key = did2.schema.termNameKey(e.synonyms{j}); + if ~isempty(key) && ~isKey(ix.byName, key) && ~isKey(ix.bySynonym, key) + ix.bySynonym(key) = e; + end + end end ix.count = numel(terms); end -function c = equivalentOf(s) -% The row's `equivalent` list as a cellstr ({} when none). +function c = listOf(s, f) +% The row's list F (`equivalent`, `synonyms`) as a cellstr ({} when none). +arguments + s struct + f {mustBeText} +end c = {}; -if ~isfield(s, 'equivalent') || isempty(s.equivalent) +if ~isfield(s, f) || isempty(s.(f)) return; end -v = s.equivalent; +v = s.(f); if ischar(v) || isstring(v) c = cellstr(v); elseif iscell(v) diff --git a/tests/+did2/+unittest/testBuildTermRegistry.m b/tests/+did2/+unittest/testBuildTermRegistry.m index d19a0dfa..f60d49ce 100644 --- a/tests/+did2/+unittest/testBuildTermRegistry.m +++ b/tests/+did2/+unittest/testBuildTermRegistry.m @@ -56,6 +56,37 @@ function testANameIsFoundIgnoringCaseSpacesUnderscoresAndHyphens(testCase) verifyFalse(testCase, did2.schema.termRegistered(term('', 'mid point'), ix)); end +function testEquivalentsAndSynonymsAreIndexedForAReader(testCase) +% [entity search resolves a term through the registry] (Jess Haley, +% 2026-11-30; VH-Lab/NDI-matlab#1332): an `equivalent` node and a `synonyms` +% entry find their registered row, for a reader. A writer's rule is unchanged: +% an equivalent node or a synonym is not the registered term. +ix = did2.schema.termIndex(resolveRegistry()); +verifyEqual(testCase, ix.count, 3); +c = ix.byNode(did2.schema.curieKey('CHEBI:757169')); +verifyEqual(testCase, c.equivalent, {'NCIm:CL1445492'}); +verifyEqual(testCase, c.synonyms, {'95% O2 / 5% CO2'}); +% by its equivalent, prefix by key and case as for a node +verifyTrue(testCase, isKey(ix.byEquivalent, did2.schema.curieKey('ncim:CL1445492'))); +verifyEqual(testCase, ix.byEquivalent(did2.schema.curieKey('NCIm:CL1445492')).node, 'CHEBI:757169'); +% by its synonym, spacing ignored as for a name +verifyEqual(testCase, ix.bySynonym(did2.schema.termNameKey('95% O2 / 5% co2')).name, 'carbogen'); +% control: the equivalent and the synonym are not in byNode or byName +verifyFalse(testCase, isKey(ix.byNode, did2.schema.curieKey('NCIm:CL1445492'))); +verifyFalse(testCase, isKey(ix.byName, did2.schema.termNameKey('95% O2 / 5% CO2'))); +% control: a row's own node or name wins over another row's equivalent or synonym +verifyEqual(testCase, ix.byNode(did2.schema.curieKey('CHEBI:15379')).name, 'dioxygen'); +verifyFalse(testCase, isKey(ix.byEquivalent, did2.schema.curieKey('CHEBI:15379'))); +verifyFalse(testCase, isKey(ix.bySynonym, did2.schema.termNameKey('dioxygen'))); +% a row whose lists are empty has empty ones (dioxygen; body midpoint's are not) +verifyEqual(testCase, ix.byNode(did2.schema.curieKey('CHEBI:15379')).equivalent, {}); +verifyEqual(testCase, ix.byNode(did2.schema.curieKey('CHEBI:15379')).synonyms, {}); +verifyEqual(testCase, ix.byName(did2.schema.termNameKey('body midpoint')).equivalent, {'CHEBI:15379'}); +% the writer's rule: an equivalent node is not the registered term +verifyFalse(testCase, did2.schema.termRegistered(term('NCIm:CL1445492', 'carbogen'), ix)); +verifyTrue(testCase, did2.schema.termRegistered(term('CHEBI:757169', 'carbogen'), ix)); +end + function testNoRegistryRegistersNothing(testCase) ix = did2.schema.termIndex(struct('title', 'not a term registry')); verifyFalse(testCase, ix.present); @@ -325,6 +356,18 @@ function testTheCensusDoesNotListAFitsVariableSymbols(testCase) '{"node": "", "name": "body midpoint", "status": "staged"}]}']); end +function r = resolveRegistry() +% carbogen with an equivalent and a synonym; dioxygen, whose node another row +% lists as an equivalent and whose name another row lists as a synonym +r = jsondecode(['{"terms": [' ... + '{"node": "CHEBI:757169", "name": "carbogen", "status": "decided", ' ... + '"equivalent": ["NCIm:CL1445492"], "synonyms": ["95% O2 / 5% CO2"]},' ... + '{"node": "CHEBI:15379", "name": "dioxygen", "status": "decided", ' ... + '"equivalent": [], "synonyms": []},' ... + '{"node": "", "name": "body midpoint", "status": "staged", ' ... + '"equivalent": ["CHEBI:15379"], "synonyms": ["dioxygen"]}]}']); +end + function t = symbol(name) % a coefficient's symbol as a label ({node '', name}), as the migrators write it arguments