Search in sources :

Example 1 with NetworkPolicy

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

the class IdentifyEntitiesConfiguration method fromJson.

/**
 * Construct an instance of {@link IdentifyEntitiesConfiguration} from
 * plugin configuration JSON object.
 * @param configurationJson the plugin configuration
 * @return the configuration for entities detection
 * @throws JSONException if identify configuration is malformed
 */
@Nullable
public static IdentifyEntitiesConfiguration fromJson(@NonNull JSONObject configurationJson) throws JSONException {
    if (!configurationJson.has(CONFIG_NAME)) {
        return null;
    }
    // Required fields
    JSONObject identifyEntitiesJson = configurationJson.getJSONObject(CONFIG_NAME);
    String celebEnabledString = identifyEntitiesJson.getString("celebrityDetectionEnabled");
    String networkPolicyString = identifyEntitiesJson.getString("defaultNetworkPolicy");
    boolean celebEnabled = Boolean.parseBoolean(celebEnabledString);
    NetworkPolicy networkPolicy = NetworkPolicy.fromKey(networkPolicyString);
    // Optional fields
    int maxEntities;
    boolean isGeneralEntityDetection;
    String collectionId;
    try {
        String maxEntitiesString = identifyEntitiesJson.getString("maxEntities");
        collectionId = identifyEntitiesJson.getString("collectionId");
        maxEntities = Integer.parseInt(maxEntitiesString);
        isGeneralEntityDetection = maxEntities < 1 || maxEntities > MAX_VALID_ENTITIES;
    } catch (JSONException | IllegalArgumentException exception) {
        collectionId = "";
        maxEntities = 0;
        isGeneralEntityDetection = true;
    }
    return new IdentifyEntitiesConfiguration(maxEntities, collectionId, isGeneralEntityDetection, celebEnabled, networkPolicy);
}
Also used : JSONObject(org.json.JSONObject) NetworkPolicy(com.amplifyframework.predictions.aws.NetworkPolicy) JSONException(org.json.JSONException) Nullable(androidx.annotation.Nullable)

Example 2 with NetworkPolicy

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

the class InterpretTextConfiguration method fromJson.

/**
 * Construct an instance of {@link InterpretTextConfiguration} from
 * plugin configuration JSON object.
 * @param configurationJson the plugin configuration
 * @return the configuration for text interpretation
 * @throws JSONException if interpret configuration is malformed
 */
@Nullable
public static InterpretTextConfiguration fromJson(@NonNull JSONObject configurationJson) throws JSONException {
    if (!configurationJson.has(CONFIG_NAME)) {
        return null;
    }
    JSONObject interpretTextJson = configurationJson.getJSONObject(CONFIG_NAME);
    String typeString = interpretTextJson.getString("type");
    String networkPolicyString = interpretTextJson.getString("defaultNetworkPolicy");
    final InterpretType type = InterpretType.valueOf(typeString);
    final NetworkPolicy networkPolicy = NetworkPolicy.fromKey(networkPolicyString);
    return new InterpretTextConfiguration(type, networkPolicy);
}
Also used : JSONObject(org.json.JSONObject) NetworkPolicy(com.amplifyframework.predictions.aws.NetworkPolicy) Nullable(androidx.annotation.Nullable)

Example 3 with NetworkPolicy

use of com.amplifyframework.predictions.aws.NetworkPolicy 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 4 with NetworkPolicy

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

the class IdentifyLabelsConfiguration method fromJson.

/**
 * Construct an instance of {@link IdentifyLabelsConfiguration} from
 * plugin configuration JSON object.
 * @param configurationJson the plugin configuration
 * @return the configuration for label identification
 * @throws JSONException if identify configuration is malformed
 */
@Nullable
public static IdentifyLabelsConfiguration fromJson(@NonNull JSONObject configurationJson) throws JSONException {
    if (!configurationJson.has(CONFIG_NAME)) {
        return null;
    }
    JSONObject identifyLabelsJson = configurationJson.getJSONObject(CONFIG_NAME);
    String typeString = identifyLabelsJson.getString("type");
    String networkPolicyString = identifyLabelsJson.getString("defaultNetworkPolicy");
    final LabelType type = LabelType.valueOf(typeString);
    final NetworkPolicy networkPolicy = NetworkPolicy.fromKey(networkPolicyString);
    return new IdentifyLabelsConfiguration(type, networkPolicy);
}
Also used : JSONObject(org.json.JSONObject) NetworkPolicy(com.amplifyframework.predictions.aws.NetworkPolicy) LabelType(com.amplifyframework.predictions.models.LabelType) Nullable(androidx.annotation.Nullable)

Example 5 with NetworkPolicy

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

the class IdentifyTextConfiguration method fromJson.

/**
 * Construct an instance of {@link IdentifyTextConfiguration} from
 * plugin configuration JSON object.
 * @param configurationJson the plugin configuration
 * @return the configuration for text identification
 * @throws JSONException if identify configuration is malformed
 */
@Nullable
public static IdentifyTextConfiguration fromJson(@NonNull JSONObject configurationJson) throws JSONException {
    if (!configurationJson.has(CONFIG_NAME)) {
        return null;
    }
    JSONObject identifyLabelsJson = configurationJson.getJSONObject(CONFIG_NAME);
    String formatString = identifyLabelsJson.getString("format");
    String networkPolicyString = identifyLabelsJson.getString("defaultNetworkPolicy");
    final TextFormatType format = TextFormatType.valueOf(formatString);
    final NetworkPolicy networkPolicy = NetworkPolicy.fromKey(networkPolicyString);
    return new IdentifyTextConfiguration(format, networkPolicy);
}
Also used : TextFormatType(com.amplifyframework.predictions.models.TextFormatType) JSONObject(org.json.JSONObject) NetworkPolicy(com.amplifyframework.predictions.aws.NetworkPolicy) Nullable(androidx.annotation.Nullable)

Aggregations

Nullable (androidx.annotation.Nullable)6 NetworkPolicy (com.amplifyframework.predictions.aws.NetworkPolicy)6 JSONObject (org.json.JSONObject)6 LabelType (com.amplifyframework.predictions.models.LabelType)1 LanguageType (com.amplifyframework.predictions.models.LanguageType)1 TextFormatType (com.amplifyframework.predictions.models.TextFormatType)1 JSONException (org.json.JSONException)1