Search in sources :

Example 1 with SpeechType

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

the class AWSPredictionsInterpretTest method testSyntaxDetection.

/**
 * Assert that interpret correctly labels syntax.
 * @throws Exception if prediction fails
 */
@Test
public void testSyntaxDetection() throws Exception {
    final String sampleText = "I am inevitable.";
    // Interpret sample text and assert non-null result
    InterpretResult result = predictions.interpret(sampleText);
    assertNotNull(result);
    // Assert syntax detection
    List<Syntax> actual = result.getSyntax();
    List<SpeechType> expected = Arrays.asList(// I
    SpeechType.PRONOUN, // am
    SpeechType.VERB, // inevitable
    SpeechType.ADJECTIVE, // .
    SpeechType.PUNCTUATION);
    FeatureAssert.assertMatches(expected, actual);
}
Also used : SpeechType(com.amplifyframework.predictions.models.SpeechType) InterpretResult(com.amplifyframework.predictions.result.InterpretResult) Syntax(com.amplifyframework.predictions.models.Syntax) Test(org.junit.Test)

Example 2 with SpeechType

use of com.amplifyframework.predictions.models.SpeechType 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

SpeechType (com.amplifyframework.predictions.models.SpeechType)2 Syntax (com.amplifyframework.predictions.models.Syntax)2 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 InterpretResult (com.amplifyframework.predictions.result.InterpretResult)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1