Search in sources :

Example 1 with DetectSyntaxRequest

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

the class SyntaxTagger method tagEntities.

@Override
protected List<TaggedEntity> tagEntities(final Sentence sentence) {
    final String textValue = sentence.getTextWithWsSuffix();
    // Create the delete syntax request
    final DetectSyntaxRequest request = new DetectSyntaxRequest().withText(textValue).withLanguageCode(m_languageCode);
    final DetectSyntaxResult result = m_client.detectSyntax(request);
    return // 
    result.getSyntaxTokens().stream().map(// 
    token -> new TaggedEntity(token.getText(), token.getPartOfSpeech().getTag())).collect(Collectors.toList());
}
Also used : UniversalDependenciesPOSTagSet(org.knime.ext.textprocessing.data.UniversalDependenciesPOSTagSet) DetectSyntaxRequest(com.amazonaws.services.comprehend.model.DetectSyntaxRequest) 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) DetectSyntaxResult(com.amazonaws.services.comprehend.model.DetectSyntaxResult) Tag(org.knime.ext.textprocessing.data.Tag) AmazonComprehend(com.amazonaws.services.comprehend.AmazonComprehend) TaggedEntity(org.knime.ext.textprocessing.nodes.tagging.TaggedEntity) Collectors(java.util.stream.Collectors) TaggedEntity(org.knime.ext.textprocessing.nodes.tagging.TaggedEntity) DetectSyntaxRequest(com.amazonaws.services.comprehend.model.DetectSyntaxRequest) DetectSyntaxResult(com.amazonaws.services.comprehend.model.DetectSyntaxResult)

Example 2 with DetectSyntaxRequest

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

the class AWSComprehendService method fetchSyntax.

private List<Syntax> fetchSyntax(String text, LanguageType language) throws PredictionsException {
    // Skip if configuration specifies NOT syntax
    if (!isResourceConfigured(InterpretTextConfiguration.InterpretType.SYNTAX)) {
        return null;
    }
    DetectSyntaxRequest request = new DetectSyntaxRequest().withText(text).withLanguageCode(language.getLanguageCode());
    // Detect syntax from given text via AWS Comprehend
    final DetectSyntaxResult result;
    try {
        result = comprehend.detectSyntax(request);
    } catch (AmazonClientException serviceException) {
        throw new PredictionsException("AWS Comprehend encountered an error while detecting syntax.", serviceException, "See attached service exception for more details.");
    }
    // Convert AWS Comprehend's detection result to Amplify-compatible format
    List<Syntax> syntaxTokens = new ArrayList<>();
    for (com.amazonaws.services.comprehend.model.SyntaxToken comprehendSyntax : result.getSyntaxTokens()) {
        PartOfSpeechTag comprehendPartOfSpeech = comprehendSyntax.getPartOfSpeech();
        SpeechType partOfSpeech = SpeechTypeAdapter.fromComprehend(comprehendPartOfSpeech.getTag());
        Syntax amplifySyntax = Syntax.builder().id(comprehendSyntax.getTokenId().toString()).value(partOfSpeech).confidence(comprehendPartOfSpeech.getScore() * PERCENT).targetText(comprehendSyntax.getText()).startIndex(comprehendSyntax.getBeginOffset()).build();
        syntaxTokens.add(amplifySyntax);
    }
    return syntaxTokens;
}
Also used : PartOfSpeechTag(com.amazonaws.services.comprehend.model.PartOfSpeechTag) SpeechType(com.amplifyframework.predictions.models.SpeechType) DetectSyntaxRequest(com.amazonaws.services.comprehend.model.DetectSyntaxRequest) DetectSyntaxResult(com.amazonaws.services.comprehend.model.DetectSyntaxResult) AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) PredictionsException(com.amplifyframework.predictions.PredictionsException) Syntax(com.amplifyframework.predictions.models.Syntax)

Aggregations

DetectSyntaxRequest (com.amazonaws.services.comprehend.model.DetectSyntaxRequest)2 DetectSyntaxResult (com.amazonaws.services.comprehend.model.DetectSyntaxResult)2 AmazonClientException (com.amazonaws.AmazonClientException)1 AmazonComprehend (com.amazonaws.services.comprehend.AmazonComprehend)1 PartOfSpeechTag (com.amazonaws.services.comprehend.model.PartOfSpeechTag)1 PredictionsException (com.amplifyframework.predictions.PredictionsException)1 SpeechType (com.amplifyframework.predictions.models.SpeechType)1 Syntax (com.amplifyframework.predictions.models.Syntax)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)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 UniversalDependenciesPOSTagSet (org.knime.ext.textprocessing.data.UniversalDependenciesPOSTagSet)1 AbstractDocumentTagger (org.knime.ext.textprocessing.nodes.tagging.AbstractDocumentTagger)1 TaggedEntity (org.knime.ext.textprocessing.nodes.tagging.TaggedEntity)1