Search in sources :

Example 1 with KeyPhrase

use of com.amplifyframework.predictions.models.KeyPhrase in project amplify-android by aws-amplify.

the class AWSComprehendService method comprehend.

void comprehend(@NonNull String text, @NonNull Consumer<InterpretResult> onSuccess, @NonNull Consumer<PredictionsException> onError) {
    try {
        // First obtain the dominant language to begin analysis
        final Language dominantLanguage = fetchPredominantLanguage(text);
        final LanguageType language = dominantLanguage.getValue();
        // Actually analyze text in the context of dominant language
        final Sentiment sentiment = fetchSentiment(text, language);
        final List<KeyPhrase> keyPhrases = fetchKeyPhrases(text, language);
        final List<Entity> entities = fetchEntities(text, language);
        final List<Syntax> syntax = fetchSyntax(text, language);
        onSuccess.accept(InterpretResult.builder().language(dominantLanguage).sentiment(sentiment).keyPhrases(keyPhrases).entities(entities).syntax(syntax).build());
    } catch (PredictionsException exception) {
        onError.accept(exception);
    }
}
Also used : Entity(com.amplifyframework.predictions.models.Entity) DominantLanguage(com.amazonaws.services.comprehend.model.DominantLanguage) Language(com.amplifyframework.predictions.models.Language) PredictionsException(com.amplifyframework.predictions.PredictionsException) Syntax(com.amplifyframework.predictions.models.Syntax) LanguageType(com.amplifyframework.predictions.models.LanguageType) Sentiment(com.amplifyframework.predictions.models.Sentiment) KeyPhrase(com.amplifyframework.predictions.models.KeyPhrase)

Example 2 with KeyPhrase

use of com.amplifyframework.predictions.models.KeyPhrase 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)

Example 3 with KeyPhrase

use of com.amplifyframework.predictions.models.KeyPhrase in project amplify-android by aws-amplify.

the class AWSPredictionsInterpretTest method testKeyPhraseDetection.

/**
 * Assert that key phrases are detected.
 * @throws Exception if prediction fails
 */
@Test
public void testKeyPhraseDetection() throws Exception {
    final String sampleText = "My mama always said life was like a box of chocolates.";
    // Interpret sample text and assert non-null result
    InterpretResult result = predictions.interpret(sampleText);
    assertNotNull(result);
    // Assert key phrase detection
    List<KeyPhrase> actual = result.getKeyPhrases();
    List<String> expected = Arrays.asList("My mama", "life", "a box", "chocolates");
    FeatureAssert.assertMatches(expected, actual);
}
Also used : InterpretResult(com.amplifyframework.predictions.result.InterpretResult) KeyPhrase(com.amplifyframework.predictions.models.KeyPhrase) Test(org.junit.Test)

Aggregations

KeyPhrase (com.amplifyframework.predictions.models.KeyPhrase)3 PredictionsException (com.amplifyframework.predictions.PredictionsException)2 AmazonClientException (com.amazonaws.AmazonClientException)1 DetectKeyPhrasesRequest (com.amazonaws.services.comprehend.model.DetectKeyPhrasesRequest)1 DetectKeyPhrasesResult (com.amazonaws.services.comprehend.model.DetectKeyPhrasesResult)1 DominantLanguage (com.amazonaws.services.comprehend.model.DominantLanguage)1 Entity (com.amplifyframework.predictions.models.Entity)1 Language (com.amplifyframework.predictions.models.Language)1 LanguageType (com.amplifyframework.predictions.models.LanguageType)1 Sentiment (com.amplifyframework.predictions.models.Sentiment)1 Syntax (com.amplifyframework.predictions.models.Syntax)1 InterpretResult (com.amplifyframework.predictions.result.InterpretResult)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1