Search in sources :

Example 1 with DominantLanguage

use of com.amazonaws.services.comprehend.model.DominantLanguage 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 DominantLanguage

use of com.amazonaws.services.comprehend.model.DominantLanguage 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 3 with DominantLanguage

use of com.amazonaws.services.comprehend.model.DominantLanguage in project knime-cloud by knime.

the class LanguageOperation method processChunk.

/**
 * Method to process one chunk with given texts.
 *
 * @param out RowOutput to push new rows to
 * @param comprehendClient Comprehend client to send the requests
 * @param numInputColumns Number of input columns
 * @param rowBatch List containing rows
 * @param texts Texts to process
 * @param validRows List containing indices of valid rows
 * @throws InterruptedException Thrown if execution is canceled
 */
@SuppressWarnings("null")
private static final void processChunk(final RowOutput out, final AmazonComprehend comprehendClient, final int numInputColumns, final List<DataRow> rowBatch, final List<String> texts, final Set<Integer> validRows) throws InterruptedException {
    final BatchDetectDominantLanguageRequest detectDominantLanguageRequest;
    final BatchDetectDominantLanguageResult detectDominantLanguageResult;
    Iterator<BatchDetectDominantLanguageItemResult> results = null;
    if (!texts.isEmpty()) {
        detectDominantLanguageRequest = new BatchDetectDominantLanguageRequest().withTextList(texts);
        detectDominantLanguageResult = comprehendClient.batchDetectDominantLanguage(detectDominantLanguageRequest);
        results = detectDominantLanguageResult.getResultList().iterator();
    }
    final DataCell[] cells = new DataCell[numInputColumns + 3];
    for (int i = 0; i < rowBatch.size(); i++) {
        final DataRow row = rowBatch.get(i);
        for (int j = 0; j < numInputColumns; j++) {
            cells[j] = row.getCell(j);
        }
        if (validRows.contains(i)) {
            long outputRowIndex = 0;
            // Push rows (one per language) to the output.
            for (final DominantLanguage dominantLang : results.next().getLanguages()) {
                // Copy the results to the new columns in the output.
                cells[numInputColumns] = new StringCell(code2Name(dominantLang.getLanguageCode()));
                cells[numInputColumns + 1] = new StringCell(dominantLang.getLanguageCode());
                cells[numInputColumns + 2] = new DoubleCell(dominantLang.getScore());
                // Create a new data row and push it to the output container.
                out.push(new DefaultRow(new RowKey(row.getKey().getString() + "_" + outputRowIndex++), cells));
            }
        } else {
            Arrays.fill(cells, numInputColumns, numInputColumns + 3, DataType.getMissingCell());
            out.push(new DefaultRow(new RowKey(row.getKey().getString() + "_" + 0), cells));
        }
    }
    // Clean up
    rowBatch.clear();
    texts.clear();
    validRows.clear();
}
Also used : RowKey(org.knime.core.data.RowKey) DoubleCell(org.knime.core.data.def.DoubleCell) BatchDetectDominantLanguageRequest(com.amazonaws.services.comprehend.model.BatchDetectDominantLanguageRequest) BatchDetectDominantLanguageItemResult(com.amazonaws.services.comprehend.model.BatchDetectDominantLanguageItemResult) BatchDetectDominantLanguageResult(com.amazonaws.services.comprehend.model.BatchDetectDominantLanguageResult) DataRow(org.knime.core.data.DataRow) DominantLanguage(com.amazonaws.services.comprehend.model.DominantLanguage) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Aggregations

DominantLanguage (com.amazonaws.services.comprehend.model.DominantLanguage)3 PredictionsException (com.amplifyframework.predictions.PredictionsException)2 LanguageType (com.amplifyframework.predictions.models.LanguageType)2 AmazonClientException (com.amazonaws.AmazonClientException)1 BatchDetectDominantLanguageItemResult (com.amazonaws.services.comprehend.model.BatchDetectDominantLanguageItemResult)1 BatchDetectDominantLanguageRequest (com.amazonaws.services.comprehend.model.BatchDetectDominantLanguageRequest)1 BatchDetectDominantLanguageResult (com.amazonaws.services.comprehend.model.BatchDetectDominantLanguageResult)1 DetectDominantLanguageRequest (com.amazonaws.services.comprehend.model.DetectDominantLanguageRequest)1 DetectDominantLanguageResult (com.amazonaws.services.comprehend.model.DetectDominantLanguageResult)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 DataCell (org.knime.core.data.DataCell)1 DataRow (org.knime.core.data.DataRow)1 RowKey (org.knime.core.data.RowKey)1 DefaultRow (org.knime.core.data.def.DefaultRow)1 DoubleCell (org.knime.core.data.def.DoubleCell)1 StringCell (org.knime.core.data.def.StringCell)1