UNPKG

@azure/ai-language-text

Version:

An isomorphic client library for the text analysis features in the Azure Cognitive Language Service.

131 lines 4.03 kB
// Copyright (c) Microsoft Corporation. // Licensed under the MIT license. import { __rest } from "tslib"; import { logger } from "./logger"; /** * Given a sorted array of input objects (with a unique ID) and an unsorted array of results, * return a sorted array of results. * * @internal * @param sortedIds - An array of sorted IDs * @param unsortedArray - An array of entries that contain `id` but are not sorted */ export function sortResponseIdObjects(sortedIds, unsortedArray) { const unsortedMap = new Map(); for (const item of unsortedArray) { unsortedMap.set(item.id, item); } if (unsortedArray.length !== sortedIds.length) { const ordinal = unsortedArray.length > sortedIds.length ? "more" : "fewer"; logger.warning(`The service returned ${ordinal} responses than inputs. Some errors may be treated as fatal.`); } const result = []; /** * When the results are returned in pages, sortedArray will probably have more * items than unsortedArray so it is ok to ignore the case when a sorted item * ID is not found in `unsortedMap`. */ for (const id of sortedIds) { const item = unsortedMap.get(id); if (item) { result.push(item); } } return result; } /** * @internal */ export function parseAssessmentIndex(pointer) { const regex = new RegExp(/#\/documents\/(\d+)\/sentences\/(\d+)\/assessments\/(\d+)/); const res = regex.exec(pointer); if (res !== null) { const assessmentIndex = { document: parseInt(res[1]), sentence: parseInt(res[2]), assessment: parseInt(res[3]), }; return assessmentIndex; } else { throw new Error(`Pointer "${pointer}" is not a valid Assessment pointer`); } } /** * Parses the index of the healthcare entity from a JSON pointer. * @param pointer - a JSON pointer representing an entity * @internal */ export function parseHealthcareEntityIndex(pointer) { const regex = new RegExp(/#\/results\/documents\/(\d+)\/entities\/(\d+)/); const res = regex.exec(pointer); if (res !== null) { return parseInt(res[2]); } else { throw new Error(`Pointer "${pointer}" is not a valid healthcare entity pointer`); } } /** * @internal */ export function isStringArray(documents) { return typeof documents[0] === "string"; } /** * @internal */ export function convertToTextDocumentInput(inputs, language) { return inputs.map((text, index) => { return { id: String(index), language, text, }; }); } /** * @internal */ export function convertToLanguageDetectionInput(inputs, countryHint) { return inputs.map((text, index) => { return { id: String(index), countryHint, text, }; }); } /** * @internal */ export function getOperationOptions(options) { const { abortSignal, includeStatistics, onResponse, requestOptions, serializerOptions, tracingOptions } = options, rest = __rest(options, ["abortSignal", "includeStatistics", "onResponse", "requestOptions", "serializerOptions", "tracingOptions"]); return { options: { abortSignal, includeStatistics, onResponse, requestOptions, serializerOptions, tracingOptions, }, rest, }; } /** * * @param error - error with the target in the JSON error pointer format "#/items/0 * @returns number: the position of the task with error */ export function extractErrorPointerIndex(error) { if (!error.target) { throw new Error("Unexpected response from service - no target present"); } const position = parseInt(error.target.split("/").pop()); if (isNaN(position)) { throw new Error(`Unexpected response from service - action pointer "${error.target}" is not a valid action pointer.`); } return position; } //# sourceMappingURL=util.js.map