Search in sources :

Example 1 with LanguageType

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();
}
Also used : DominantLanguage(com.amazonaws.services.comprehend.model.DominantLanguage) DetectDominantLanguageResult(com.amazonaws.services.comprehend.model.DetectDominantLanguageResult) AmazonClientException(com.amazonaws.AmazonClientException) DetectDominantLanguageRequest(com.amazonaws.services.comprehend.model.DetectDominantLanguageRequest) PredictionsException(com.amplifyframework.predictions.PredictionsException) LanguageType(com.amplifyframework.predictions.models.LanguageType)

Example 2 with LanguageType

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);
}
Also used : JSONObject(org.json.JSONObject) NetworkPolicy(com.amplifyframework.predictions.aws.NetworkPolicy) LanguageType(com.amplifyframework.predictions.models.LanguageType) Nullable(androidx.annotation.Nullable)

Example 3 with LanguageType

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);
    }
}
Also used : Entity(com.amplifyframework.predictions.models.Entity) DominantLanguage(com.amazonaws.services.comprehend.model.DominantLanguage) Language(com.amplifyframework.predictions.models.Language) PredictionsException(com.amplifyframework.predictions.PredictionsException) Syntax(com.amplifyframework.predictions.models.Syntax) LanguageType(com.amplifyframework.predictions.models.LanguageType) Sentiment(com.amplifyframework.predictions.models.Sentiment) KeyPhrase(com.amplifyframework.predictions.models.KeyPhrase)

Example 4 with LanguageType

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();
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) TranslateTextRequest(com.amazonaws.services.translate.model.TranslateTextRequest) PredictionsException(com.amplifyframework.predictions.PredictionsException) LanguageType(com.amplifyframework.predictions.models.LanguageType)

Example 5 with LanguageType

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);
}
Also used : TranslateTextResult(com.amplifyframework.predictions.result.TranslateTextResult) LanguageType(com.amplifyframework.predictions.models.LanguageType) Test(org.junit.Test)

Aggregations

LanguageType (com.amplifyframework.predictions.models.LanguageType)6 PredictionsException (com.amplifyframework.predictions.PredictionsException)3 AmazonClientException (com.amazonaws.AmazonClientException)2 DominantLanguage (com.amazonaws.services.comprehend.model.DominantLanguage)2 TranslateTextResult (com.amplifyframework.predictions.result.TranslateTextResult)2 Test (org.junit.Test)2 Nullable (androidx.annotation.Nullable)1 DetectDominantLanguageRequest (com.amazonaws.services.comprehend.model.DetectDominantLanguageRequest)1 DetectDominantLanguageResult (com.amazonaws.services.comprehend.model.DetectDominantLanguageResult)1 TranslateTextRequest (com.amazonaws.services.translate.model.TranslateTextRequest)1 NetworkPolicy (com.amplifyframework.predictions.aws.NetworkPolicy)1 Entity (com.amplifyframework.predictions.models.Entity)1 KeyPhrase (com.amplifyframework.predictions.models.KeyPhrase)1 Language (com.amplifyframework.predictions.models.Language)1 Sentiment (com.amplifyframework.predictions.models.Sentiment)1 Syntax (com.amplifyframework.predictions.models.Syntax)1 JSONObject (org.json.JSONObject)1