/
util.ts
172 lines (160 loc) · 3.97 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { LanguageDetectionInput, TextDocumentInput } from "./generated";
import { TextAnalysisOperationOptions } from "./models";
import { logger } from "./logger";
interface IdObject {
id: string;
}
/**
* 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 sortedArray - An array of entries sorted by `id`
* @param unsortedArray - An array of entries that contain `id` but are not sorted
*/
export function sortResponseIdObjects<U extends IdObject>(
sortedArray: IdObject[],
unsortedArray: U[]
): U[] {
const unsortedMap = new Map<string, U>();
for (const item of unsortedArray) {
unsortedMap.set(item.id, item);
}
if (unsortedArray.length !== sortedArray.length) {
const ordinal = unsortedArray.length > sortedArray.length ? "more" : "fewer";
logger.warning(
`The service returned ${ordinal} responses than inputs. Some errors may be treated as fatal.`
);
}
const result: U[] = [];
/**
* 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 sortedItem of sortedArray) {
const item = unsortedMap.get(sortedItem.id);
if (item) {
result.push(item);
}
}
return result;
}
/**
* @internal
*/
export interface AssessmentIndex {
document: number;
sentence: number;
assessment: number;
}
/**
* @internal
*/
export function parseAssessmentIndex(pointer: string): AssessmentIndex {
const regex = new RegExp(/#\/documents\/(\d+)\/sentences\/(\d+)\/assessments\/(\d+)/);
const res = regex.exec(pointer);
if (res !== null) {
const assessmentIndex: 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: string): number {
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: unknown[]): documents is string[] {
return typeof documents[0] === "string";
}
/**
* @internal
*/
export function convertToTextDocumentInput(
inputs: string[],
language?: string
): TextDocumentInput[] {
return inputs.map((text: string, index): TextDocumentInput => {
return {
id: String(index),
language,
text,
};
});
}
/**
* @internal
*/
export function convertToLanguageDetectionInput(
inputs: string[],
countryHint?: string
): LanguageDetectionInput[] {
return inputs.map((text: string, index): LanguageDetectionInput => {
return {
id: String(index),
countryHint,
text,
};
});
}
/**
* @internal
*/
export function getOperationOptions<OptionsT extends TextAnalysisOperationOptions>(
options: OptionsT
): {
options: TextAnalysisOperationOptions;
rest: Omit<
OptionsT,
| "onResponse"
| "abortSignal"
| "apiVersion"
| "includeStatistics"
| "requestOptions"
| "serializerOptions"
| "tracingOptions"
>;
} {
const {
abortSignal,
apiVersion,
includeStatistics,
onResponse,
requestOptions,
serializerOptions,
tracingOptions,
...rest
} = options;
return {
options: {
abortSignal,
apiVersion,
includeStatistics,
onResponse,
requestOptions,
serializerOptions,
tracingOptions,
},
rest,
};
}