export default function useComprehension()

in apps/televisit-demo/frontend/src/containers/transcriptions/useComprehension.js [15:85]


export default function useComprehension(transcriptChunks) {
  const [result, setResult] = useState([]);
  const clientParams = {
    region: 'us-east-1',
    accessKeyId: AWS.config.credentials.accessKeyId,
    secretAccessKey: AWS.config.credentials.secretAccessKey,
    sessionToken: AWS.config.credentials.sessionToken,
  };

  useEffect(() => {
    const addResult = (chunk, entities) => {
      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);
        }
      });

      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, []);

        detectEntities(chunk.text, clientParams).then((entities) => {
          addResult(chunk, entities);
        });

        inferRxNorm(chunk.text, clientParams).then((rawEntities) => {
          const entities = addSelectedConceptCodeAndSortConcepts(
            rawEntities,
            'RxNormConcepts'
          );
          addResult(chunk, entities);
        });

        inferICD10CM(chunk.text, clientParams).then((rawEntities) => {
          const entities = addSelectedConceptCodeAndSortConcepts(
            rawEntities,
            'ICD10CMConcepts'
          );
          addResult(chunk, entities);
        });
      }
    }
  }, [transcriptChunks, clientParams]);

  return [result, setResult];
}