Search in sources :

Example 1 with Entity

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

the class AWSComprehendService method fetchEntities.

private List<Entity> fetchEntities(String text, LanguageType language) throws PredictionsException {
    // Skip if configuration specifies NOT entities
    if (!isResourceConfigured(InterpretTextConfiguration.InterpretType.ENTITIES)) {
        return null;
    }
    DetectEntitiesRequest request = new DetectEntitiesRequest().withText(text).withLanguageCode(language.getLanguageCode());
    // Detect entities from given text via AWS Comprehend
    final DetectEntitiesResult result;
    try {
        result = comprehend.detectEntities(request);
    } catch (AmazonClientException serviceException) {
        throw new PredictionsException("AWS Comprehend encountered an error while detecting entities.", serviceException, "See attached service exception for more details.");
    }
    // Convert AWS Comprehend's detection result to Amplify-compatible format
    List<Entity> entities = new ArrayList<>();
    for (com.amazonaws.services.comprehend.model.Entity comprehendEntity : result.getEntities()) {
        EntityType entityType = EntityTypeAdapter.fromComprehend(comprehendEntity.getType());
        Entity amplifyEntity = Entity.builder().value(entityType).confidence(comprehendEntity.getScore() * PERCENT).targetText(comprehendEntity.getText()).startIndex(comprehendEntity.getBeginOffset()).build();
        entities.add(amplifyEntity);
    }
    return entities;
}
Also used : EntityType(com.amplifyframework.predictions.models.EntityType) Entity(com.amplifyframework.predictions.models.Entity) AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) DetectEntitiesResult(com.amazonaws.services.comprehend.model.DetectEntitiesResult) DetectEntitiesRequest(com.amazonaws.services.comprehend.model.DetectEntitiesRequest) PredictionsException(com.amplifyframework.predictions.PredictionsException)

Example 2 with Entity

use of com.amplifyframework.predictions.models.Entity 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 3 with Entity

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

the class AWSPredictionsInterpretTest method testEntityDetection.

/**
 * Assert that entities are detected.
 * @throws Exception if prediction fails
 */
@Test
public void testEntityDetection() throws Exception {
    final String sampleText = "Toto, I've a feeling we're not in Kansas anymore.";
    // Interpret sample text and assert non-null result
    InterpretResult result = predictions.interpret(sampleText);
    assertNotNull(result);
    // Assert entities detection
    List<Entity> actual = result.getEntities();
    List<EntityType> expected = Arrays.asList(// Toto (it's a dog, but close enough)
    EntityType.PERSON, // Kansas
    EntityType.LOCATION);
    FeatureAssert.assertMatches(expected, actual);
}
Also used : EntityType(com.amplifyframework.predictions.models.EntityType) Entity(com.amplifyframework.predictions.models.Entity) InterpretResult(com.amplifyframework.predictions.result.InterpretResult) Test(org.junit.Test)

Aggregations

Entity (com.amplifyframework.predictions.models.Entity)3 PredictionsException (com.amplifyframework.predictions.PredictionsException)2 EntityType (com.amplifyframework.predictions.models.EntityType)2 AmazonClientException (com.amazonaws.AmazonClientException)1 DetectEntitiesRequest (com.amazonaws.services.comprehend.model.DetectEntitiesRequest)1 DetectEntitiesResult (com.amazonaws.services.comprehend.model.DetectEntitiesResult)1 DominantLanguage (com.amazonaws.services.comprehend.model.DominantLanguage)1 KeyPhrase (com.amplifyframework.predictions.models.KeyPhrase)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