All files / src/utils comprehend.ts

0% Statements 0/26
0% Branches 0/1
0% Functions 0/1
0% Lines 0/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35                                                                     
import {Amplify, fetchAuthSession} from "@aws-amplify/core";
import {Comprehend, type KeyPhrase, LanguageCode} from "@aws-sdk/client-comprehend";
import type {ComprehendClientConfig} from "@aws-sdk/client-comprehend/dist-types/ComprehendClient";
 
/**
 * Detect key phrases in the given text using Lambda function
 * @param text - Input text to analyze
 * @param languageCode - Language code for the text
 * @returns Promise resolving to an array of key phrases
 */
export async function comprehend(text: string, languageCode: string): Promise<KeyPhrase[]> {
    if (text.trim().length === 0
        || Object.values(LanguageCode).indexOf(languageCode as LanguageCode) === -1
    ) {
        return [];
    }
    const region = Amplify.getConfig()
        .Predictions!
        .interpret!
        .interpretText!
        .region!;
    const authSession = await fetchAuthSession();
    const credentials = authSession.credentials!;
    const config: ComprehendClientConfig = {
        region,
        credentials,
    };
    const comprehend = new Comprehend(config);
    const response = await comprehend.detectKeyPhrases({
        Text: text,
        LanguageCode: languageCode as LanguageCode,
    });
    return response.KeyPhrases ?? [];
}