Search in sources :

Example 1 with SentimentScore

use of com.amazonaws.services.comprehend.model.SentimentScore in project amplify-android by aws-amplify.

the class AWSComprehendService method fetchSentiment.

private Sentiment fetchSentiment(String text, LanguageType language) throws PredictionsException {
    // Skip if configuration specifies NOT sentiment
    if (!isResourceConfigured(InterpretTextConfiguration.InterpretType.SENTIMENT)) {
        return null;
    }
    DetectSentimentRequest request = new DetectSentimentRequest().withText(text).withLanguageCode(language.getLanguageCode());
    // Detect sentiment from given text via AWS Comprehend
    final DetectSentimentResult result;
    try {
        result = comprehend.detectSentiment(request);
    } catch (AmazonClientException serviceException) {
        throw new PredictionsException("AWS Comprehend encountered an error while detecting sentiment.", serviceException, "See attached service exception for more details.");
    }
    // Convert AWS Comprehend's detection result to Amplify-compatible format
    String comprehendSentiment = result.getSentiment();
    SentimentScore sentimentScore = result.getSentimentScore();
    SentimentType predominantSentiment = SentimentTypeAdapter.fromComprehend(comprehendSentiment);
    final float score;
    switch(predominantSentiment) {
        case POSITIVE:
            score = sentimentScore.getPositive();
            break;
        case NEGATIVE:
            score = sentimentScore.getNegative();
            break;
        case NEUTRAL:
            score = sentimentScore.getNeutral();
            break;
        case MIXED:
            score = sentimentScore.getMixed();
            break;
        default:
            score = 0f;
    }
    return Sentiment.builder().value(predominantSentiment).confidence(score * PERCENT).build();
}
Also used : DetectSentimentResult(com.amazonaws.services.comprehend.model.DetectSentimentResult) DetectSentimentRequest(com.amazonaws.services.comprehend.model.DetectSentimentRequest) AmazonClientException(com.amazonaws.AmazonClientException) PredictionsException(com.amplifyframework.predictions.PredictionsException) SentimentScore(com.amazonaws.services.comprehend.model.SentimentScore) SentimentType(com.amplifyframework.predictions.models.SentimentType)

Example 2 with SentimentScore

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

the class SentimentOperation 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 rows 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 final void processChunk(final RowOutput out, final AmazonComprehend comprehendClient, final int numInputColumns, final List<DataRow> rows, final List<String> texts, final Set<Integer> validRows) throws InterruptedException {
    final BatchDetectSentimentRequest detectSentimentRequest;
    final BatchDetectSentimentResult detectSentimentResult;
    Iterator<BatchDetectSentimentItemResult> results = null;
    if (!texts.isEmpty()) {
        detectSentimentRequest = new BatchDetectSentimentRequest().withTextList(texts).withLanguageCode(ComprehendUtils.LANG_MAP.getOrDefault(m_sourceLanguage, "en"));
        detectSentimentResult = comprehendClient.batchDetectSentiment(detectSentimentRequest);
        results = detectSentimentResult.getResultList().iterator();
    }
    final DataCell[] cells = new DataCell[numInputColumns + 5];
    for (int i = 0; i < rows.size(); i++) {
        final DataRow row = rows.get(i);
        for (int j = 0; j < numInputColumns; j++) {
            cells[j] = row.getCell(j);
        }
        if (validRows.contains(i)) {
            // Grab scores for each sentiment category.
            final BatchDetectSentimentItemResult result = results.next();
            final SentimentScore score = result.getSentimentScore();
            // Copy the results to the new columns in the output.
            cells[numInputColumns] = new StringCell(result.getSentiment());
            cells[numInputColumns + 1] = new DoubleCell(score.getMixed());
            cells[numInputColumns + 2] = new DoubleCell(score.getPositive());
            cells[numInputColumns + 3] = new DoubleCell(score.getNeutral());
            cells[numInputColumns + 4] = new DoubleCell(score.getNegative());
        } else {
            Arrays.fill(cells, numInputColumns, numInputColumns + 5, DataType.getMissingCell());
        }
        // Create a new data row and push it to the output container.
        out.push(new DefaultRow(row.getKey(), cells));
    }
    // Clean up
    rows.clear();
    texts.clear();
    validRows.clear();
}
Also used : BatchDetectSentimentResult(com.amazonaws.services.comprehend.model.BatchDetectSentimentResult) StringCell(org.knime.core.data.def.StringCell) DoubleCell(org.knime.core.data.def.DoubleCell) DataCell(org.knime.core.data.DataCell) SentimentScore(com.amazonaws.services.comprehend.model.SentimentScore) BatchDetectSentimentRequest(com.amazonaws.services.comprehend.model.BatchDetectSentimentRequest) BatchDetectSentimentItemResult(com.amazonaws.services.comprehend.model.BatchDetectSentimentItemResult) DefaultRow(org.knime.core.data.def.DefaultRow) DataRow(org.knime.core.data.DataRow)

Aggregations

SentimentScore (com.amazonaws.services.comprehend.model.SentimentScore)2 AmazonClientException (com.amazonaws.AmazonClientException)1 BatchDetectSentimentItemResult (com.amazonaws.services.comprehend.model.BatchDetectSentimentItemResult)1 BatchDetectSentimentRequest (com.amazonaws.services.comprehend.model.BatchDetectSentimentRequest)1 BatchDetectSentimentResult (com.amazonaws.services.comprehend.model.BatchDetectSentimentResult)1 DetectSentimentRequest (com.amazonaws.services.comprehend.model.DetectSentimentRequest)1 DetectSentimentResult (com.amazonaws.services.comprehend.model.DetectSentimentResult)1 PredictionsException (com.amplifyframework.predictions.PredictionsException)1 SentimentType (com.amplifyframework.predictions.models.SentimentType)1 DataCell (org.knime.core.data.DataCell)1 DataRow (org.knime.core.data.DataRow)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