Search in sources :

Example 1 with DetectKeyPhrasesResult

use of com.amazonaws.services.comprehend.model.DetectKeyPhrasesResult in project amplify-android by aws-amplify.

the class AWSComprehendService method fetchKeyPhrases.

private List<KeyPhrase> fetchKeyPhrases(String text, LanguageType language) throws PredictionsException {
    // Skip if configuration specifies NOT key phrase
    if (!isResourceConfigured(InterpretTextConfiguration.InterpretType.KEY_PHRASES)) {
        return null;
    }
    DetectKeyPhrasesRequest request = new DetectKeyPhrasesRequest().withText(text).withLanguageCode(language.getLanguageCode());
    // Detect key phrases from given text via AWS Comprehend
    final DetectKeyPhrasesResult result;
    try {
        result = comprehend.detectKeyPhrases(request);
    } catch (AmazonClientException serviceException) {
        throw new PredictionsException("AWS Comprehend encountered an error while detecting key phrases.", serviceException, "See attached service exception for more details.");
    }
    // Convert AWS Comprehend's detection result to Amplify-compatible format
    List<KeyPhrase> keyPhrases = new ArrayList<>();
    for (com.amazonaws.services.comprehend.model.KeyPhrase comprehendKeyPhrase : result.getKeyPhrases()) {
        KeyPhrase amplifyKeyPhrase = KeyPhrase.builder().value(comprehendKeyPhrase.getText()).confidence(comprehendKeyPhrase.getScore() * PERCENT).targetText(comprehendKeyPhrase.getText()).startIndex(comprehendKeyPhrase.getBeginOffset()).build();
        keyPhrases.add(amplifyKeyPhrase);
    }
    return keyPhrases;
}
Also used : DetectKeyPhrasesRequest(com.amazonaws.services.comprehend.model.DetectKeyPhrasesRequest) DetectKeyPhrasesResult(com.amazonaws.services.comprehend.model.DetectKeyPhrasesResult) AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) PredictionsException(com.amplifyframework.predictions.PredictionsException) KeyPhrase(com.amplifyframework.predictions.models.KeyPhrase)

Aggregations

AmazonClientException (com.amazonaws.AmazonClientException)1 DetectKeyPhrasesRequest (com.amazonaws.services.comprehend.model.DetectKeyPhrasesRequest)1 DetectKeyPhrasesResult (com.amazonaws.services.comprehend.model.DetectKeyPhrasesResult)1 PredictionsException (com.amplifyframework.predictions.PredictionsException)1 KeyPhrase (com.amplifyframework.predictions.models.KeyPhrase)1 ArrayList (java.util.ArrayList)1