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);
}
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;
}
Aggregations