in app/src/useComprehension.js [21:89]
export default function useComprehension(transcriptChunks, clientParams) {
const [result, setResult] = useState([]);
useEffect(() => {
const addResult = (chunk, entities, cacheKey) => {
const prev = resultMap.get(chunk);
let next = [...prev];
entities.forEach((e) => {
const matching = prev.find((x) => {
return (
x.BeginOffset === e.BeginOffset &&
x.EndOffset === e.EndOffset &&
x.Type === e.Type &&
x.Category === e.Category
);
});
// If there's already an entity with these exact properties, extend it.
// Specifically, for ICD10CM and RxNorm, there's usually a pre-existing
// entity returned by the general comprehend response, and we're attaching
// the concepts to it.
if (matching) {
next = next.filter((y) => y !== matching);
next.push({ ...matching, ...e });
} else {
e.id = uuid();
next.push(e);
}
});
cache[cacheKey] = next;
resultMap.set(chunk, next);
setResult(transcriptChunks.map((chunk) => resultMap.get(chunk) ?? []));
};
for (const chunk of transcriptChunks) {
const existing = resultMap.get(chunk);
if (!existing) {
resultMap.set(chunk, []);
const cacheKey = md5(chunk.text);
// Only use the cached version if the offline flag is set
if (cache[cacheKey] && useComprehension.__offline) {
addResult(chunk, cache[cacheKey], cacheKey);
return;
}
detectEntities(chunk.text, clientParams).then((entities) => {
addResult(chunk, entities, cacheKey);
});
inferRxNorm(chunk.text, clientParams).then((rawEntities) => {
const entities = addSelectedConceptCodeAndSortConcepts(rawEntities, 'RxNormConcepts');
addResult(chunk, entities, cacheKey);
});
inferICD10CM(chunk.text, clientParams).then((rawEntities) => {
const entities = addSelectedConceptCodeAndSortConcepts(rawEntities, 'ICD10CMConcepts');
addResult(chunk, entities, cacheKey);
});
}
}
}, [transcriptChunks, clientParams]);
return [result, setResult];
}