Search in sources :

Example 1 with BatchDetectSentimentRequest

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

BatchDetectSentimentItemResult (com.amazonaws.services.comprehend.model.BatchDetectSentimentItemResult)1 BatchDetectSentimentRequest (com.amazonaws.services.comprehend.model.BatchDetectSentimentRequest)1 BatchDetectSentimentResult (com.amazonaws.services.comprehend.model.BatchDetectSentimentResult)1 SentimentScore (com.amazonaws.services.comprehend.model.SentimentScore)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