Search in sources :

Example 1 with DetectEntitiesRequest

use of com.amazonaws.services.comprehend.model.DetectEntitiesRequest in project knime-cloud by knime.

the class EntityTagger method tagEntities.

@Override
protected List<TaggedEntity> tagEntities(final Sentence sentence) {
    final String textValue = sentence.getTextWithWsSuffix();
    final DetectEntitiesRequest request = new DetectEntitiesRequest().withText(textValue).withLanguageCode(m_languageCode);
    final DetectEntitiesResult result = m_client.detectEntities(request);
    return // 
    result.getEntities().stream().map(// 
    entity -> new TaggedEntity(entity.getText(), entity.getType())).collect(Collectors.toList());
}
Also used : AmazonEntityTag(org.knime.cloud.aws.mlservices.utils.tagsets.AmazonEntityTag) Arrays(java.util.Arrays) List(java.util.List) AbstractDocumentTagger(org.knime.ext.textprocessing.nodes.tagging.AbstractDocumentTagger) Sentence(org.knime.ext.textprocessing.data.Sentence) Document(org.knime.ext.textprocessing.data.Document) Tag(org.knime.ext.textprocessing.data.Tag) AmazonComprehend(com.amazonaws.services.comprehend.AmazonComprehend) TaggedEntity(org.knime.ext.textprocessing.nodes.tagging.TaggedEntity) DetectEntitiesRequest(com.amazonaws.services.comprehend.model.DetectEntitiesRequest) DetectEntitiesResult(com.amazonaws.services.comprehend.model.DetectEntitiesResult) Collectors(java.util.stream.Collectors) TaggedEntity(org.knime.ext.textprocessing.nodes.tagging.TaggedEntity) DetectEntitiesResult(com.amazonaws.services.comprehend.model.DetectEntitiesResult) DetectEntitiesRequest(com.amazonaws.services.comprehend.model.DetectEntitiesRequest)

Example 2 with DetectEntitiesRequest

use of com.amazonaws.services.comprehend.model.DetectEntitiesRequest 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)

Aggregations

DetectEntitiesRequest (com.amazonaws.services.comprehend.model.DetectEntitiesRequest)2 DetectEntitiesResult (com.amazonaws.services.comprehend.model.DetectEntitiesResult)2 AmazonClientException (com.amazonaws.AmazonClientException)1 AmazonComprehend (com.amazonaws.services.comprehend.AmazonComprehend)1 PredictionsException (com.amplifyframework.predictions.PredictionsException)1 Entity (com.amplifyframework.predictions.models.Entity)1 EntityType (com.amplifyframework.predictions.models.EntityType)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 AmazonEntityTag (org.knime.cloud.aws.mlservices.utils.tagsets.AmazonEntityTag)1 Document (org.knime.ext.textprocessing.data.Document)1 Sentence (org.knime.ext.textprocessing.data.Sentence)1 Tag (org.knime.ext.textprocessing.data.Tag)1 AbstractDocumentTagger (org.knime.ext.textprocessing.nodes.tagging.AbstractDocumentTagger)1 TaggedEntity (org.knime.ext.textprocessing.nodes.tagging.TaggedEntity)1