Search in sources :

Example 1 with AmazonTranslate

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));
    }
}
Also used : TranslateTextRequest(com.amazonaws.services.translate.model.TranslateTextRequest) Document(org.knime.ext.textprocessing.data.Document) DataRow(org.knime.core.data.DataRow) TranslateTextResult(com.amazonaws.services.translate.model.TranslateTextResult) DocumentValue(org.knime.ext.textprocessing.data.DocumentValue) AmazonTranslate(com.amazonaws.services.translate.AmazonTranslate) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DataType(org.knime.core.data.DataType) DefaultRow(org.knime.core.data.def.DefaultRow)

Aggregations

AmazonTranslate (com.amazonaws.services.translate.AmazonTranslate)1 TranslateTextRequest (com.amazonaws.services.translate.model.TranslateTextRequest)1 TranslateTextResult (com.amazonaws.services.translate.model.TranslateTextResult)1 DataCell (org.knime.core.data.DataCell)1 DataRow (org.knime.core.data.DataRow)1 DataType (org.knime.core.data.DataType)1 DefaultRow (org.knime.core.data.def.DefaultRow)1 StringCell (org.knime.core.data.def.StringCell)1 Document (org.knime.ext.textprocessing.data.Document)1 DocumentValue (org.knime.ext.textprocessing.data.DocumentValue)1