use of com.amazonaws.services.translate.AmazonTranslate in project knime-cloud by knime.
the class TranslateOperation method compute.
void compute(final RowInput in, final RowOutput out, final ExecutionContext exec, final long rowCount) throws Exception {
// Create a connection to the Translate service in the provided region
final TranslateConnection conn = new TranslateConnection(m_cxnInfo);
final AmazonTranslate translate = conn.getClient();
int textColumnIdx = in.getDataTableSpec().findColumnIndex(m_textColumnName);
long rowCounter = 0;
// For each input row, grab the text column, make the call to Translate
// and push the input plus the translation to the output.
DataRow inputRow = null;
while ((inputRow = in.poll()) != null) {
// Check for cancel and update the row progress
++rowCounter;
exec.checkCanceled();
if (rowCount > 0) {
exec.setProgress(rowCounter / (double) rowCount, "Processing row " + rowCounter + " of " + rowCount);
}
// Grab the text to evaluate
String textValue = null;
final DataCell cell = inputRow.getCell(textColumnIdx);
// Create cells containing the output data.
// Copy the input data to the output
final int numInputColumns = inputRow.getNumCells();
DataCell[] cells = Stream.generate(DataType::getMissingCell).limit(numInputColumns + 1).toArray(DataCell[]::new);
for (int i = 0; i < numInputColumns; i++) {
cells[i] = inputRow.getCell(i);
}
if (!cell.isMissing()) {
if (cell.getType().isCompatible(DocumentValue.class)) {
final Document doc = ((DocumentValue) cell).getDocument();
textValue = doc.getTitle() + " " + doc.getDocumentBodyText();
} else {
textValue = cell.toString();
}
final TranslateTextRequest request = new TranslateTextRequest().withText(textValue).withSourceLanguageCode(m_sourceLangCode).withTargetLanguageCode(m_targetLangCode);
final TranslateTextResult result = translate.translateText(request);
cells[numInputColumns] = new StringCell(result.getTranslatedText());
}
// Create a new data row and push it to the output container.
out.push(new DefaultRow(inputRow.getKey(), cells));
}
}
Aggregations