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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable camelcase */
/* eslint-disable no-useless-catch */
/* eslint-disable no-use-before-define */
import * as rp from 'request-promise';
import { Doc, FieldType } from '../../fields/Doc';
import { InkData } from '../../fields/InkField';
import { List } from '../../fields/List';
import { Cast } from '../../fields/Types';
import { UndoManager } from '../util/UndoManager';
import { ClientUtils } from '../../ClientUtils';
type APIManager<D> = { converter: BodyConverter<D>; requester: RequestExecutor };
type RequestExecutor = (apiKey: string, body: string, service: Service) => Promise<string>;
type AnalysisApplier<D> = (target: Doc, relevantKeys: string[], data: D, ...args: any) => any;
type BodyConverter<D> = (data: D) => string;
type Converter = (results: any) => FieldType;
type TextConverter = (results: any, data: string) => Promise<{ keyterms: FieldType; external_recommendations: any; kp_string: string[] }>;
type BingConverter = (results: any) => Promise<{ title_vals: string[]; url_vals: string[] }>;
export type Tag = { name: string; confidence: number };
export type Rectangle = { top: number; left: number; width: number; height: number };
export enum Service {
ComputerVision = 'vision',
Face = 'face',
Handwriting = 'handwriting',
Text = 'text',
Bing = 'bing',
}
export enum Confidence {
Yikes = 0.0,
Unlikely = 0.2,
Poor = 0.4,
Fair = 0.6,
Good = 0.8,
Excellent = 0.95,
}
/**
* A file that handles all interactions with Microsoft Azure's Cognitive
* Services APIs. These machine learning endpoints allow basic data analytics for
* various media types.
*/
export namespace CognitiveServices {
const ExecuteQuery = async <D>(service: Service, manager: APIManager<D>, data: D): Promise<any> => {
const apiKey = process.env[service.toUpperCase()];
if (!apiKey) {
console.log(`No API key found for ${service}: ensure youe root directory has .env file with _CLIENT_${service.toUpperCase()}.`);
return undefined;
}
let results: any;
try {
results = await manager.requester(apiKey, manager.converter(data), service).then(json => JSON.parse(json));
} catch (e) {
throw e;
}
return results;
};
export namespace Image {
export const Manager: APIManager<string> = {
converter: (imageUrl: string) => JSON.stringify({ url: imageUrl }),
requester: async (apiKey: string, body: string, service: Service) => {
let uriBase;
let parameters;
switch (service) {
case Service.Face:
uriBase = 'face/v1.0/detect';
parameters = {
returnFaceId: 'true',
returnFaceLandmarks: 'false',
returnFaceAttributes: 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',
};
break;
case Service.ComputerVision:
uriBase = 'vision/v2.0/analyze';
parameters = {
visualFeatures: 'Categories,Description,Color,Objects,Tags,Adult',
details: 'Celebrities,Landmarks',
language: 'en',
};
break;
default:
}
const options = {
uri: 'https://eastus.api.cognitive.microsoft.com/' + uriBase,
qs: parameters,
body: body,
headers: {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': apiKey,
},
};
return rp.post(options);
},
};
export namespace Appliers {
export const ProcessImage: AnalysisApplier<string> = async (target: Doc, keys: string[], url: string, service: Service, converter: Converter) => {
const batch = UndoManager.StartBatch('Image Analysis');
const storageKey = keys[0];
if (!url || (await Cast(target[storageKey], Doc))) {
return;
}
let toStore: any;
const results = await ExecuteQuery(service, Manager, url);
if (!results) {
toStore = 'Cognitive Services could not process the given image URL.';
} else if (!results.length) {
toStore = converter(results);
} else {
toStore = results.length > 0 ? converter(results) : 'Empty list returned.';
}
target[storageKey] = toStore;
batch.end();
};
}
export type Face = { faceAttributes: any; faceId: string; faceRectangle: Rectangle };
}
export namespace Inking {
export const Manager: APIManager<InkData[]> = {
converter: (inkData: InkData[]): string => {
let id = 0;
const strokes: AzureStrokeData[] = inkData.map(points => ({
id: id++,
points: points.map(({ X: x, Y: y }) => `${x},${y}`).join(','),
language: 'en-US',
}));
return JSON.stringify({
version: 1,
language: 'en-US',
unit: 'mm',
strokes,
});
},
requester: async (apiKey: string, body: string) => {
const xhttp = new XMLHttpRequest();
const serverAddress = 'https://api.cognitive.microsoft.com';
const endpoint = serverAddress + '/inkrecognizer/v1.0-preview/recognize';
return new Promise<string>((resolve, reject) => {
xhttp.onreadystatechange = function (this: XMLHttpRequest) {
if (this.readyState === 4) {
const result = xhttp.responseText;
switch (this.status) {
case 200:
return resolve(result);
case 400:
default:
return reject(result);
}
}
return undefined;
};
xhttp.open('PUT', endpoint, true);
xhttp.setRequestHeader('Ocp-Apim-Subscription-Key', apiKey);
xhttp.setRequestHeader('Content-Type', 'application/json');
xhttp.send(body);
});
},
};
export namespace Appliers {
export const ConcatenateHandwriting: AnalysisApplier<InkData[]> = async (target: Doc, keys: string[], inkData: InkData[]) => {
const batch = UndoManager.StartBatch('Ink Analysis');
let results = await ExecuteQuery(Service.Handwriting, Manager, inkData);
if (results) {
results.recognitionUnits && (results = results.recognitionUnits);
target[keys[0]] = Doc.Get.FromJson({ data: results, title: 'Ink Analysis' });
const recognizedText = results.map((item: any) => item.recognizedText);
const recognizedObjects = results.map((item: any) => item.recognizedObject);
const individualWords = recognizedText.filter((text: string) => text && text.split(' ').length === 1);
target[keys[1]] = individualWords.length ? individualWords.join(' ') : recognizedObjects.join(', ');
}
batch.end();
};
export const InterpretStrokes = async (strokes: InkData[]) => {
let results = await ExecuteQuery(Service.Handwriting, Manager, strokes);
if (results) {
results.recognitionUnits && (results = results.recognitionUnits);
}
return results;
};
}
export interface AzureStrokeData {
id: number;
points: string;
language?: string;
}
export interface HandwritingUnit {
version: number;
language: string;
unit: string;
strokes: AzureStrokeData[];
}
}
export namespace BingSearch {
export const Manager: APIManager<string> = {
converter: (data: string) => data,
requester: async (apiKey: string, query: string) => {
const xhttp = new XMLHttpRequest();
const serverAddress = 'https://api.cognitive.microsoft.com';
const endpoint = serverAddress + '/bing/v5.0/search?q=' + encodeURIComponent(query);
const promisified = (resolve: any, reject: any) => {
xhttp.onreadystatechange = function (this: XMLHttpRequest) {
if (this.readyState === 4) {
const result = xhttp.responseText;
switch (this.status) {
case 200:
return resolve(result);
case 400:
default:
return reject(result);
}
}
return undefined;
};
if (apiKey) {
xhttp.open('GET', endpoint, true);
xhttp.setRequestHeader('Ocp-Apim-Subscription-Key', apiKey);
xhttp.setRequestHeader('Content-Type', 'application/json');
xhttp.send();
} else {
console.log('API key for BING unavailable');
}
};
return new Promise<any>(promisified);
},
};
export namespace Appliers {
export const analyzer = async (query: string, converter: BingConverter) => {
const results = await ExecuteQuery(Service.Bing, Manager, query);
console.log('Bing results: ', results);
const { title_vals, url_vals } = await converter(results);
return { title_vals, url_vals };
};
}
}
export namespace HathiTrust {
export const Manager: APIManager<string> = {
converter: (data: string) => data,
requester: async (apiKey: string, query: string) => {
const xhttp = new XMLHttpRequest();
const serverAddress = 'https://babel.hathitrust.org/cgi/htd/';
const endpoint = serverAddress + '/bing/v5.0/search?q=' + encodeURIComponent(query);
const promisified = (resolve: any, reject: any) => {
xhttp.onreadystatechange = function (this: XMLHttpRequest) {
if (this.readyState === 4) {
const result = xhttp.responseText;
switch (this.status) {
case 200:
return resolve(result);
case 400:
default:
return reject(result);
}
}
return undefined;
};
if (apiKey) {
xhttp.open('GET', endpoint, true);
xhttp.setRequestHeader('Ocp-Apim-Subscription-Key', apiKey);
xhttp.setRequestHeader('Content-Type', 'application/json');
xhttp.send();
} else {
console.log('API key for BING unavailable');
}
};
return new Promise<any>(promisified);
},
};
export namespace Appliers {
export const analyzer = async (query: string, converter: BingConverter) => {
const results = await ExecuteQuery(Service.Bing, Manager, query);
console.log('Bing results: ', results);
const { title_vals, url_vals } = await converter(results);
return { title_vals, url_vals };
};
}
}
export namespace Text {
export const Manager: APIManager<string> = {
converter: (data: string) =>
JSON.stringify({
documents: [
{
id: 1,
language: 'en',
text: data,
},
],
}),
requester: async (apiKey: string, body: string, service: Service) => {
const serverAddress = 'https://eastus.api.cognitive.microsoft.com';
const endpoint = serverAddress + '/text/analytics/v2.1/keyPhrases';
const sampleBody = {
documents: [
{
language: 'en',
id: 1,
text: 'Hello world. This is some input text that I love.',
},
],
};
const actualBody = body;
const options = {
uri: endpoint,
body: actualBody,
headers: {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': apiKey,
},
};
return rp.post(options);
},
};
export namespace Appliers {
export async function vectorize(keyterms: any, dataDoc: Doc, mainDoc: boolean = false) {
console.log('vectorizing...');
// keyterms = ["father", "king"];
const args = { method: 'POST', uri: ClientUtils.prepend('/recommender'), body: { keyphrases: keyterms }, json: true };
await rp.post(args).then(async wordvecs => {
if (wordvecs) {
const indices = Object.keys(wordvecs);
console.log('successful vectorization!');
const vectorValues = new List<number>();
indices.forEach((ind: any) => {
vectorValues.push(wordvecs[ind]);
});
// ClientRecommender.Instance.processVector(vectorValues, dataDoc, mainDoc);
} // adds document to internal doc set
else {
console.log('unsuccessful :( word(s) not in vocabulary');
}
});
}
export const analyzer = async (dataDoc: Doc, target: Doc, keys: string[], data: string, converter: TextConverter, isMainDoc: boolean = false, isInternal: boolean = true) => {
const results = await ExecuteQuery(Service.Text, Manager, data);
console.log('Cognitive Services keyphrases: ', results);
const { keyterms, external_recommendations, kp_string } = await converter(results, data);
target[keys[0]] = keyterms;
if (isInternal) {
// await vectorize([data], dataDoc, isMainDoc);
await vectorize(kp_string, dataDoc, isMainDoc);
} else {
return { recs: external_recommendations, keyterms: keyterms };
}
return undefined;
};
// export async function countFrequencies()
}
}
}
|