Search in sources :

Example 1 with ComprehendException

use of software.amazon.awssdk.services.comprehend.model.ComprehendException in project aws-doc-sdk-examples by awsdocs.

the class DetectSentiment method detectSentiments.

// snippet-start:[comprehend.java2.detect_sentiment.main]
public static void detectSentiments(ComprehendClient comClient, String text) {
    try {
        DetectSentimentRequest detectSentimentRequest = DetectSentimentRequest.builder().text(text).languageCode("en").build();
        DetectSentimentResponse detectSentimentResult = comClient.detectSentiment(detectSentimentRequest);
        System.out.println("The Neutral value is " + detectSentimentResult.sentimentScore().neutral());
    } catch (ComprehendException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : ComprehendException(software.amazon.awssdk.services.comprehend.model.ComprehendException) DetectSentimentRequest(software.amazon.awssdk.services.comprehend.model.DetectSentimentRequest) DetectSentimentResponse(software.amazon.awssdk.services.comprehend.model.DetectSentimentResponse)

Example 2 with ComprehendException

use of software.amazon.awssdk.services.comprehend.model.ComprehendException in project aws-doc-sdk-examples by awsdocs.

the class DocumentClassifierDemo method createDocumentClassifier.

// snippet-start:[comprehend.java2.classifier.main]
public static void createDocumentClassifier(ComprehendClient comClient, String dataAccessRoleArn, String s3Uri, String documentClassifierName) {
    try {
        DocumentClassifierInputDataConfig config = DocumentClassifierInputDataConfig.builder().s3Uri(s3Uri).build();
        CreateDocumentClassifierRequest createDocumentClassifierRequest = CreateDocumentClassifierRequest.builder().documentClassifierName(documentClassifierName).dataAccessRoleArn(dataAccessRoleArn).languageCode("en").inputDataConfig(config).build();
        CreateDocumentClassifierResponse createDocumentClassifierResult = comClient.createDocumentClassifier(createDocumentClassifierRequest);
        String documentClassifierArn = createDocumentClassifierResult.documentClassifierArn();
        System.out.println("Document Classifier ARN: " + documentClassifierArn);
    } catch (ComprehendException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : ComprehendException(software.amazon.awssdk.services.comprehend.model.ComprehendException) CreateDocumentClassifierRequest(software.amazon.awssdk.services.comprehend.model.CreateDocumentClassifierRequest) CreateDocumentClassifierResponse(software.amazon.awssdk.services.comprehend.model.CreateDocumentClassifierResponse) DocumentClassifierInputDataConfig(software.amazon.awssdk.services.comprehend.model.DocumentClassifierInputDataConfig)

Example 3 with ComprehendException

use of software.amazon.awssdk.services.comprehend.model.ComprehendException in project aws-doc-sdk-examples by awsdocs.

the class DetectEntities method detectAllEntities.

// snippet-start:[comprehend.java2.detect_entities.main]
public static void detectAllEntities(ComprehendClient comClient, String text) {
    try {
        DetectEntitiesRequest detectEntitiesRequest = DetectEntitiesRequest.builder().text(text).languageCode("en").build();
        DetectEntitiesResponse detectEntitiesResult = comClient.detectEntities(detectEntitiesRequest);
        List<Entity> entList = detectEntitiesResult.entities();
        Iterator<Entity> lanIterator = entList.iterator();
        while (lanIterator.hasNext()) {
            Entity entity = lanIterator.next();
            System.out.println("Entity text is " + entity.text());
        }
    } catch (ComprehendException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : ComprehendException(software.amazon.awssdk.services.comprehend.model.ComprehendException) Entity(software.amazon.awssdk.services.comprehend.model.Entity) DetectEntitiesRequest(software.amazon.awssdk.services.comprehend.model.DetectEntitiesRequest) DetectEntitiesResponse(software.amazon.awssdk.services.comprehend.model.DetectEntitiesResponse)

Example 4 with ComprehendException

use of software.amazon.awssdk.services.comprehend.model.ComprehendException in project aws-doc-sdk-examples by awsdocs.

the class DetectSyntax method detectAllSyntax.

// snippet-start:[comprehend.java2.detect_syntax.main]
public static void detectAllSyntax(ComprehendClient comClient, String text) {
    try {
        DetectSyntaxRequest detectSyntaxRequest = DetectSyntaxRequest.builder().text(text).languageCode("en").build();
        DetectSyntaxResponse detectSyntaxResult = comClient.detectSyntax(detectSyntaxRequest);
        List<SyntaxToken> syntaxTokens = detectSyntaxResult.syntaxTokens();
        Iterator<SyntaxToken> syntaxIterator = syntaxTokens.iterator();
        while (syntaxIterator.hasNext()) {
            SyntaxToken token = syntaxIterator.next();
            System.out.println("Language is " + token.text());
            System.out.println("Part of speech is " + token.partOfSpeech().tagAsString());
        }
    } catch (ComprehendException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : ComprehendException(software.amazon.awssdk.services.comprehend.model.ComprehendException) SyntaxToken(software.amazon.awssdk.services.comprehend.model.SyntaxToken) DetectSyntaxRequest(software.amazon.awssdk.services.comprehend.model.DetectSyntaxRequest) DetectSyntaxResponse(software.amazon.awssdk.services.comprehend.model.DetectSyntaxResponse)

Example 5 with ComprehendException

use of software.amazon.awssdk.services.comprehend.model.ComprehendException in project aws-doc-sdk-examples by awsdocs.

the class LexService method DetectLanguage.

private String DetectLanguage(String text) {
    Region region = Region.US_EAST_1;
    ComprehendClient comClient = ComprehendClient.builder().region(region).build();
    try {
        String lanCode = "";
        DetectDominantLanguageRequest request = DetectDominantLanguageRequest.builder().text(text).build();
        DetectDominantLanguageResponse resp = comClient.detectDominantLanguage(request);
        List<DominantLanguage> allLanList = resp.languages();
        Iterator<DominantLanguage> lanIterator = allLanList.iterator();
        while (lanIterator.hasNext()) {
            DominantLanguage lang = lanIterator.next();
            lanCode = lang.languageCode();
        }
        return lanCode;
    } catch (ComprehendException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return "";
}
Also used : ComprehendException(software.amazon.awssdk.services.comprehend.model.ComprehendException) DominantLanguage(software.amazon.awssdk.services.comprehend.model.DominantLanguage) DetectDominantLanguageResponse(software.amazon.awssdk.services.comprehend.model.DetectDominantLanguageResponse) ComprehendClient(software.amazon.awssdk.services.comprehend.ComprehendClient) DetectDominantLanguageRequest(software.amazon.awssdk.services.comprehend.model.DetectDominantLanguageRequest) Region(software.amazon.awssdk.regions.Region)

Aggregations

ComprehendException (software.amazon.awssdk.services.comprehend.model.ComprehendException)7 DetectDominantLanguageRequest (software.amazon.awssdk.services.comprehend.model.DetectDominantLanguageRequest)2 DetectDominantLanguageResponse (software.amazon.awssdk.services.comprehend.model.DetectDominantLanguageResponse)2 DominantLanguage (software.amazon.awssdk.services.comprehend.model.DominantLanguage)2 Region (software.amazon.awssdk.regions.Region)1 ComprehendClient (software.amazon.awssdk.services.comprehend.ComprehendClient)1 CreateDocumentClassifierRequest (software.amazon.awssdk.services.comprehend.model.CreateDocumentClassifierRequest)1 CreateDocumentClassifierResponse (software.amazon.awssdk.services.comprehend.model.CreateDocumentClassifierResponse)1 DetectEntitiesRequest (software.amazon.awssdk.services.comprehend.model.DetectEntitiesRequest)1 DetectEntitiesResponse (software.amazon.awssdk.services.comprehend.model.DetectEntitiesResponse)1 DetectKeyPhrasesRequest (software.amazon.awssdk.services.comprehend.model.DetectKeyPhrasesRequest)1 DetectKeyPhrasesResponse (software.amazon.awssdk.services.comprehend.model.DetectKeyPhrasesResponse)1 DetectSentimentRequest (software.amazon.awssdk.services.comprehend.model.DetectSentimentRequest)1 DetectSentimentResponse (software.amazon.awssdk.services.comprehend.model.DetectSentimentResponse)1 DetectSyntaxRequest (software.amazon.awssdk.services.comprehend.model.DetectSyntaxRequest)1 DetectSyntaxResponse (software.amazon.awssdk.services.comprehend.model.DetectSyntaxResponse)1 DocumentClassifierInputDataConfig (software.amazon.awssdk.services.comprehend.model.DocumentClassifierInputDataConfig)1 Entity (software.amazon.awssdk.services.comprehend.model.Entity)1 KeyPhrase (software.amazon.awssdk.services.comprehend.model.KeyPhrase)1 SyntaxToken (software.amazon.awssdk.services.comprehend.model.SyntaxToken)1