@azure/ai-text-analytics
Version:
An isomorphic client library for the Azure Text Analytics service.
78 lines • 3.03 kB
JavaScript
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { sortResponseIdObjects } from "./util";
/**
* Helper function for converting nested service error into
* the unified TextAnalyticsError
* @internal
*/
export function intoTextAnalyticsError(errorModel) {
// Return the deepest error. This will always be at most
// one level for TextAnalytics
if (errorModel.innererror !== undefined) {
return intoTextAnalyticsError(errorModel.innererror);
}
return {
code: errorModel.code,
message: errorModel.message,
target: errorModel.target
};
}
/**
* @internal
*/
export function makeTextAnalyticsSuccessResult(id, warnings, statistics) {
return {
id,
statistics,
warnings
};
}
/**
* @internal
*/
export function makeTextAnalyticsErrorResult(id, error) {
return {
id,
error: intoTextAnalyticsError(error)
};
}
/**
* @internal
* combines successful and erroneous results into a single array of results and
* sort them so that the IDs order match that of the input documents array.
* @param input - the array of documents sent to the service for processing.
* @param response - the response received from the service.
*/
export function combineSuccessfulAndErroneousDocuments(input, response) {
return processAndCombineSuccessfulAndErroneousDocuments(input, response, (x) => x, makeTextAnalyticsErrorResult);
}
/**
* @internal
* combines successful and erroneous results into a single array of results and
* sort them so that the IDs order match that of the input documents array.
* @param input - the array of documents sent to the service for processing.
* @param response - the response received from the service.
* @param process - a function to convert the results from one type to another.
*/
export function processAndCombineSuccessfulAndErroneousDocuments(input, response, processSuccess, processError) {
const successResults = response.documents.map(processSuccess);
const unsortedResults = successResults.concat(response.errors.map((error) => processError(error.id, error.error)));
return sortResponseIdObjects(input, unsortedResults);
}
/**
* @internal
* combines successful and erroneous results into a single array of results and
* sort them so that the IDs order match that of the input documents array. It
* also attaches statistics and modelVersion to the returned array.
* @param input - the array of documents sent to the service for processing.
* @param response - the response received from the service.
*/
export function combineSuccessfulAndErroneousDocumentsWithStatisticsAndModelVersion(input, response, processSuccess, processError) {
const sorted = processAndCombineSuccessfulAndErroneousDocuments(input, response, processSuccess, processError);
return Object.assign(sorted, {
statistics: response.statistics,
modelVersion: response.modelVersion
});
}
//# sourceMappingURL=textAnalyticsResult.js.map