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