use of com.amazonaws.services.comprehend.model.DominantLanguage in project amplify-android by aws-amplify.
the class AWSComprehendService method fetchPredominantLanguage.
private Language fetchPredominantLanguage(String text) throws PredictionsException {
// Language is a required field for other detections.
// Always fetch language regardless of what configuration says.
isResourceConfigured(InterpretTextConfiguration.InterpretType.LANGUAGE);
DetectDominantLanguageRequest request = new DetectDominantLanguageRequest().withText(text);
// Detect dominant language from given text via AWS Comprehend
final DetectDominantLanguageResult result;
try {
result = comprehend.detectDominantLanguage(request);
} catch (AmazonClientException serviceException) {
throw new PredictionsException("AWS Comprehend encountered an error while detecting dominant language.", serviceException, "See attached service exception for more details.");
}
// Find the most dominant language from the list
DominantLanguage dominantLanguage = null;
for (DominantLanguage language : result.getLanguages()) {
if (dominantLanguage == null || language.getScore() > dominantLanguage.getScore()) {
dominantLanguage = language;
}
}
// Confirm that there was at least one detected language
if (dominantLanguage == null) {
throw new PredictionsException("AWS Comprehend did not detect any dominant language.", "Please verify the integrity of text being analyzed.");
}
String languageCode = dominantLanguage.getLanguageCode();
LanguageType language = LanguageType.from(languageCode);
Float score = dominantLanguage.getScore();
return Language.builder().value(language).confidence(score * PERCENT).build();
}
use of com.amazonaws.services.comprehend.model.DominantLanguage in project amplify-android by aws-amplify.
the class AWSComprehendService method comprehend.
void comprehend(@NonNull String text, @NonNull Consumer<InterpretResult> onSuccess, @NonNull Consumer<PredictionsException> onError) {
try {
// First obtain the dominant language to begin analysis
final Language dominantLanguage = fetchPredominantLanguage(text);
final LanguageType language = dominantLanguage.getValue();
// Actually analyze text in the context of dominant language
final Sentiment sentiment = fetchSentiment(text, language);
final List<KeyPhrase> keyPhrases = fetchKeyPhrases(text, language);
final List<Entity> entities = fetchEntities(text, language);
final List<Syntax> syntax = fetchSyntax(text, language);
onSuccess.accept(InterpretResult.builder().language(dominantLanguage).sentiment(sentiment).keyPhrases(keyPhrases).entities(entities).syntax(syntax).build());
} catch (PredictionsException exception) {
onError.accept(exception);
}
}
use of com.amazonaws.services.comprehend.model.DominantLanguage in project knime-cloud by knime.
the class LanguageOperation 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 rowBatch 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 static final void processChunk(final RowOutput out, final AmazonComprehend comprehendClient, final int numInputColumns, final List<DataRow> rowBatch, final List<String> texts, final Set<Integer> validRows) throws InterruptedException {
final BatchDetectDominantLanguageRequest detectDominantLanguageRequest;
final BatchDetectDominantLanguageResult detectDominantLanguageResult;
Iterator<BatchDetectDominantLanguageItemResult> results = null;
if (!texts.isEmpty()) {
detectDominantLanguageRequest = new BatchDetectDominantLanguageRequest().withTextList(texts);
detectDominantLanguageResult = comprehendClient.batchDetectDominantLanguage(detectDominantLanguageRequest);
results = detectDominantLanguageResult.getResultList().iterator();
}
final DataCell[] cells = new DataCell[numInputColumns + 3];
for (int i = 0; i < rowBatch.size(); i++) {
final DataRow row = rowBatch.get(i);
for (int j = 0; j < numInputColumns; j++) {
cells[j] = row.getCell(j);
}
if (validRows.contains(i)) {
long outputRowIndex = 0;
// Push rows (one per language) to the output.
for (final DominantLanguage dominantLang : results.next().getLanguages()) {
// Copy the results to the new columns in the output.
cells[numInputColumns] = new StringCell(code2Name(dominantLang.getLanguageCode()));
cells[numInputColumns + 1] = new StringCell(dominantLang.getLanguageCode());
cells[numInputColumns + 2] = new DoubleCell(dominantLang.getScore());
// Create a new data row and push it to the output container.
out.push(new DefaultRow(new RowKey(row.getKey().getString() + "_" + outputRowIndex++), cells));
}
} else {
Arrays.fill(cells, numInputColumns, numInputColumns + 3, DataType.getMissingCell());
out.push(new DefaultRow(new RowKey(row.getKey().getString() + "_" + 0), cells));
}
}
// Clean up
rowBatch.clear();
texts.clear();
validRows.clear();
}
Aggregations