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();
}
Aggregations