Search in sources :

Example 1 with PartOfSpeechTag

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

AmazonClientException (com.amazonaws.AmazonClientException)1 DetectSyntaxRequest (com.amazonaws.services.comprehend.model.DetectSyntaxRequest)1 DetectSyntaxResult (com.amazonaws.services.comprehend.model.DetectSyntaxResult)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