use of com.amplifyframework.predictions.PredictionsException in project amplify-android by aws-amplify.
the class AWSComprehendService method fetchKeyPhrases.
private List<KeyPhrase> fetchKeyPhrases(String text, LanguageType language) throws PredictionsException {
// Skip if configuration specifies NOT key phrase
if (!isResourceConfigured(InterpretTextConfiguration.InterpretType.KEY_PHRASES)) {
return null;
}
DetectKeyPhrasesRequest request = new DetectKeyPhrasesRequest().withText(text).withLanguageCode(language.getLanguageCode());
// Detect key phrases from given text via AWS Comprehend
final DetectKeyPhrasesResult result;
try {
result = comprehend.detectKeyPhrases(request);
} catch (AmazonClientException serviceException) {
throw new PredictionsException("AWS Comprehend encountered an error while detecting key phrases.", serviceException, "See attached service exception for more details.");
}
// Convert AWS Comprehend's detection result to Amplify-compatible format
List<KeyPhrase> keyPhrases = new ArrayList<>();
for (com.amazonaws.services.comprehend.model.KeyPhrase comprehendKeyPhrase : result.getKeyPhrases()) {
KeyPhrase amplifyKeyPhrase = KeyPhrase.builder().value(comprehendKeyPhrase.getText()).confidence(comprehendKeyPhrase.getScore() * PERCENT).targetText(comprehendKeyPhrase.getText()).startIndex(comprehendKeyPhrase.getBeginOffset()).build();
keyPhrases.add(amplifyKeyPhrase);
}
return keyPhrases;
}
use of com.amplifyframework.predictions.PredictionsException 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;
}
use of com.amplifyframework.predictions.PredictionsException in project amplify-android by aws-amplify.
the class AWSPollyService method synthesizeSpeech.
private InputStream synthesizeSpeech(String text, AWSVoiceType voiceType) throws PredictionsException {
final String languageCode;
final String voiceId;
if (AWSVoiceType.UNKNOWN.equals(voiceType)) {
// Obtain voice + language from plugin configuration by default
SpeechGeneratorConfiguration config = pluginConfiguration.getSpeechGeneratorConfiguration();
languageCode = config.getLanguage();
voiceId = config.getVoice();
} else {
// Override configuration defaults if explicitly specified in the options
languageCode = voiceType.getLanguageCode();
voiceId = voiceType.getName();
}
SynthesizeSpeechRequest request = new SynthesizeSpeechRequest().withText(text).withTextType(TextType.Text).withLanguageCode(languageCode).withVoiceId(voiceId).withOutputFormat(OutputFormat.Mp3).withSampleRate(Integer.toString(MP3_SAMPLE_RATE));
// Synthesize speech from given text via Amazon Polly
final SynthesizeSpeechResult result;
try {
result = polly.synthesizeSpeech(request);
} catch (AmazonClientException serviceException) {
throw new PredictionsException("AWS Polly encountered an error while synthesizing speech.", serviceException, "See attached service exception for more details.");
}
return result.getAudioStream();
}
use of com.amplifyframework.predictions.PredictionsException in project amplify-android by aws-amplify.
the class AWSPredictionsService method detectLabels.
/**
* Delegate to {@link AWSRekognitionService} to detect labels.
* @param type the type of labels to detect
* @param imageData the image data
* @param onSuccess triggered upon successful result
* @param onError triggered upon encountering error
*/
public void detectLabels(@NonNull IdentifyAction type, @NonNull ByteBuffer imageData, @NonNull Consumer<IdentifyResult> onSuccess, @NonNull Consumer<PredictionsException> onError) {
final LabelType labelType;
try {
labelType = getLabelType(type);
} catch (PredictionsException error) {
onError.accept(error);
return;
}
rekognitionService.detectLabels(labelType, imageData, onSuccess, onError);
}
use of com.amplifyframework.predictions.PredictionsException in project amplify-android by aws-amplify.
the class AWSRekognitionService method detectPlainText.
private IdentifyTextResult detectPlainText(ByteBuffer imageData) throws PredictionsException {
DetectTextRequest request = new DetectTextRequest().withImage(new Image().withBytes(imageData));
// Read text in the given image via Amazon Rekognition
final DetectTextResult result;
try {
result = rekognition.detectText(request);
} catch (AmazonClientException serviceException) {
throw new PredictionsException("Amazon Rekognition encountered an error while detecting text.", serviceException, "See attached service exception for more details.");
}
StringBuilder fullTextBuilder = new StringBuilder();
List<String> rawLineText = new ArrayList<>();
List<IdentifiedText> words = new ArrayList<>();
List<IdentifiedText> lines = new ArrayList<>();
for (TextDetection detection : result.getTextDetections()) {
TextTypes type = TextTypes.fromValue(detection.getType());
switch(type) {
case LINE:
rawLineText.add(detection.getDetectedText());
lines.add(RekognitionResultTransformers.fromTextDetection(detection));
continue;
case WORD:
fullTextBuilder.append(detection.getDetectedText()).append(" ");
words.add(RekognitionResultTransformers.fromTextDetection(detection));
continue;
default:
}
}
return IdentifyTextResult.builder().fullText(fullTextBuilder.toString().trim()).rawLineText(rawLineText).lines(lines).words(words).build();
}
Aggregations