use of com.amplifyframework.predictions.models.LanguageType in project amplify-android by aws-amplify.
the class AWSComprehendService method fetchPredominantLanguage.
private Language fetchPredominantLanguage(String text) throws PredictionsException {
// Language is a required field for other detections.
// Always fetch language regardless of what configuration says.
isResourceConfigured(InterpretTextConfiguration.InterpretType.LANGUAGE);
DetectDominantLanguageRequest request = new DetectDominantLanguageRequest().withText(text);
// Detect dominant language from given text via AWS Comprehend
final DetectDominantLanguageResult result;
try {
result = comprehend.detectDominantLanguage(request);
} catch (AmazonClientException serviceException) {
throw new PredictionsException("AWS Comprehend encountered an error while detecting dominant language.", serviceException, "See attached service exception for more details.");
}
// Find the most dominant language from the list
DominantLanguage dominantLanguage = null;
for (DominantLanguage language : result.getLanguages()) {
if (dominantLanguage == null || language.getScore() > dominantLanguage.getScore()) {
dominantLanguage = language;
}
}
// Confirm that there was at least one detected language
if (dominantLanguage == null) {
throw new PredictionsException("AWS Comprehend did not detect any dominant language.", "Please verify the integrity of text being analyzed.");
}
String languageCode = dominantLanguage.getLanguageCode();
LanguageType language = LanguageType.from(languageCode);
Float score = dominantLanguage.getScore();
return Language.builder().value(language).confidence(score * PERCENT).build();
}
use of com.amplifyframework.predictions.models.LanguageType in project amplify-android by aws-amplify.
the class TranslateTextConfiguration method fromJson.
/**
* Construct an instance of {@link TranslateTextConfiguration} from
* plugin configuration JSON object.
* @param configurationJson the plugin configuration
* @return the configuration for text translation
* @throws JSONException if translate configuration is malformed
*/
@Nullable
public static TranslateTextConfiguration fromJson(@NonNull JSONObject configurationJson) throws JSONException {
if (!configurationJson.has(CONFIG_NAME)) {
return null;
}
JSONObject translateTextJson = configurationJson.getJSONObject(CONFIG_NAME);
String sourceLangCode = translateTextJson.getString("sourceLang");
String targetLangCode = translateTextJson.getString("targetLang");
String networkPolicyString = translateTextJson.getString("defaultNetworkPolicy");
final LanguageType sourceLanguage = LanguageType.from(sourceLangCode);
final LanguageType targetLanguage = LanguageType.from(targetLangCode);
final NetworkPolicy networkPolicy = NetworkPolicy.fromKey(networkPolicyString);
return new TranslateTextConfiguration(sourceLanguage, targetLanguage, networkPolicy);
}
use of com.amplifyframework.predictions.models.LanguageType in project amplify-android by aws-amplify.
the class AWSComprehendService method comprehend.
void comprehend(@NonNull String text, @NonNull Consumer<InterpretResult> onSuccess, @NonNull Consumer<PredictionsException> onError) {
try {
// First obtain the dominant language to begin analysis
final Language dominantLanguage = fetchPredominantLanguage(text);
final LanguageType language = dominantLanguage.getValue();
// Actually analyze text in the context of dominant language
final Sentiment sentiment = fetchSentiment(text, language);
final List<KeyPhrase> keyPhrases = fetchKeyPhrases(text, language);
final List<Entity> entities = fetchEntities(text, language);
final List<Syntax> syntax = fetchSyntax(text, language);
onSuccess.accept(InterpretResult.builder().language(dominantLanguage).sentiment(sentiment).keyPhrases(keyPhrases).entities(entities).syntax(syntax).build());
} catch (PredictionsException exception) {
onError.accept(exception);
}
}
use of com.amplifyframework.predictions.models.LanguageType in project amplify-android by aws-amplify.
the class AWSTranslateService method fetchTranslation.
private TranslateTextResult fetchTranslation(String text, LanguageType sourceLanguage, LanguageType targetLanguage) throws PredictionsException {
// Throw if default language is not configured
LanguageType source = !LanguageType.UNKNOWN.equals(sourceLanguage) ? sourceLanguage : pluginConfiguration.getTranslateTextConfiguration().getSourceLanguage();
LanguageType target = !LanguageType.UNKNOWN.equals(targetLanguage) ? targetLanguage : pluginConfiguration.getTranslateTextConfiguration().getTargetLanguage();
TranslateTextRequest request = new TranslateTextRequest().withText(text).withSourceLanguageCode(source.getLanguageCode()).withTargetLanguageCode(target.getLanguageCode());
// Translate given text via AWS Translate
final com.amazonaws.services.translate.model.TranslateTextResult result;
try {
result = translate.translateText(request);
} catch (AmazonClientException serviceException) {
throw new PredictionsException("AWS Translate encountered an error while translating text.", serviceException, "See attached service exception for more details.");
}
String translation = result.getTranslatedText();
String targetCode = result.getTargetLanguageCode();
LanguageType language = LanguageType.from(targetCode);
return TranslateTextResult.builder().translatedText(translation).targetLanguage(language).build();
}
use of com.amplifyframework.predictions.models.LanguageType in project amplify-android by aws-amplify.
the class AWSPredictionsTranslateTest method testTranslationWithLanguageOverride.
/**
* Assert that category falls back to configured default language.
* @throws Exception if prediction fails
*/
@Test
public void testTranslationWithLanguageOverride() throws Exception {
final String sampleText = "¡Hola mundo!";
// Translate english text and assert non-null result.
// Use configured default languages
TranslateTextResult result = predictions.translateText(sampleText, LanguageType.SPANISH, LanguageType.ENGLISH);
assertNotNull(result);
// Assert translated language is in English
LanguageType language = result.getTargetLanguage();
assertEquals(LanguageType.ENGLISH, language);
// Assert translation
String translation = result.getTranslatedText();
assertEquals("Hello world!", translation);
}
Aggregations