@azure/ai-text-analytics
Version:
An isomorphic client library for the Azure Text Analytics service.
74 lines • 3.09 kB
JavaScript
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { makeTextAnalyticsSuccessResult, makeTextAnalyticsErrorResult } from "./textAnalyticsResult";
import { parseAssessmentIndex } from "./util";
/**
* @param document - A document result coming from the service.
* @internal
*/
export function makeAnalyzeSentimentResult(result) {
const { id, sentiment, confidenceScores, sentenceSentiments: sentences, warnings, statistics } = result;
return Object.assign(Object.assign({}, makeTextAnalyticsSuccessResult(id, warnings, statistics)), { sentiment,
confidenceScores, sentences: sentences.map((sentence) => convertGeneratedSentenceSentiment(sentence, result)) });
}
/**
* @internal
*/
export function makeAnalyzeSentimentErrorResult(id, error) {
return makeTextAnalyticsErrorResult(id, error);
}
/**
* Converts a sentence sentiment object returned by the service to another that
* is user-friendly.
*
* @param sentence - The sentence sentiment object to be converted.
* @param response - The entire response returned by the service.
* @returns The user-friendly sentence sentiment object.
* @internal
*/
function convertGeneratedSentenceSentiment(sentence, result) {
return {
confidenceScores: sentence.confidenceScores,
sentiment: sentence.sentiment,
text: sentence.text,
offset: sentence.offset,
length: sentence.length,
opinions: sentence.targets
? sentence.targets.map((target) => ({
target: {
confidenceScores: target.confidenceScores,
sentiment: target.sentiment,
text: target.text,
offset: target.offset,
length: target.length
},
assessments: target.relations
.filter((relation) => relation.relationType === "assessment")
.map((relation) => convertTargetRelationToAssessmentSentiment(relation, result))
}))
: []
};
}
/**
* Converts a target relation object returned by the service to an assessment
* sentiment object where JSON pointers in the former are realized in the
* latter.
*
* @param targetRelation - The target relation object to be converted.
* @param response - The entire response returned by the service.
* @returns The user-friendly assessment sentiment object.
* @internal
*/
function convertTargetRelationToAssessmentSentiment(targetRelation, result) {
var _a, _b;
const assessmentPtr = targetRelation.ref;
const assessmentIndex = parseAssessmentIndex(assessmentPtr);
const assessment = (_b = (_a = result.sentenceSentiments) === null || _a === void 0 ? void 0 : _a[assessmentIndex.sentence].assessments) === null || _b === void 0 ? void 0 : _b[assessmentIndex.assessment];
if (assessment !== undefined) {
return assessment;
}
else {
throw new Error(`Pointer "${assessmentPtr}" is not a valid Assessment pointer`);
}
}
//# sourceMappingURL=analyzeSentimentResult.js.map