aboutsummaryrefslogtreecommitdiff
path: root/src/client/cognitive_services/CognitiveServices.ts
diff options
context:
space:
mode:
authorbobzel <zzzman@gmail.com>2023-11-12 14:34:09 -0500
committerbobzel <zzzman@gmail.com>2023-11-12 14:34:09 -0500
commit7fcf4c54c42b7eaa427ea88c0b8586a78d7f1859 (patch)
treefb049b6364456e3b70c325c59efcee00c64d5557 /src/client/cognitive_services/CognitiveServices.ts
parentfb4521e4275248ba463164e40aaaed04df65b050 (diff)
cleaning up freeformview code.
Diffstat (limited to 'src/client/cognitive_services/CognitiveServices.ts')
-rw-r--r--src/client/cognitive_services/CognitiveServices.ts195
1 files changed, 84 insertions, 111 deletions
diff --git a/src/client/cognitive_services/CognitiveServices.ts b/src/client/cognitive_services/CognitiveServices.ts
index 2b2931a97..408903324 100644
--- a/src/client/cognitive_services/CognitiveServices.ts
+++ b/src/client/cognitive_services/CognitiveServices.ts
@@ -1,29 +1,29 @@
-import * as request from "request-promise";
-import { Doc, Field } from "../../fields/Doc";
-import { Cast } from "../../fields/Types";
-import { Utils } from "../../Utils";
-import { InkData } from "../../fields/InkField";
-import { UndoManager } from "../util/UndoManager";
-import requestPromise = require("request-promise");
-import { List } from "../../fields/List";
-
-type APIManager<D> = { converter: BodyConverter<D>, requester: RequestExecutor };
+import * as request from 'request-promise';
+import { Doc, Field } from '../../fields/Doc';
+import { Cast } from '../../fields/Types';
+import { Utils } from '../../Utils';
+import { InkData } from '../../fields/InkField';
+import { UndoManager } from '../util/UndoManager';
+import requestPromise = require('request-promise');
+import { List } from '../../fields/List';
+
+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) => Field;
-type TextConverter = (results: any, data: string) => Promise<{ keyterms: Field, external_recommendations: any, kp_string: string[] }>;
-type BingConverter = (results: any) => Promise<{ title_vals: string[], url_vals: string[] }>;
+type TextConverter = (results: any, data: string) => Promise<{ keyterms: Field; 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 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"
+ ComputerVision = 'vision',
+ Face = 'face',
+ Handwriting = 'handwriting',
+ Text = 'text',
+ Bing = 'bing',
}
export enum Confidence {
@@ -32,7 +32,7 @@ export enum Confidence {
Poor = 0.4,
Fair = 0.6,
Good = 0.8,
- Excellent = 0.95
+ Excellent = 0.95,
}
/**
@@ -41,13 +41,8 @@ export enum Confidence {
* various media types.
*/
export namespace CognitiveServices {
-
const ExecuteQuery = async <D>(service: Service, manager: APIManager<D>, data: D): Promise<any> => {
let apiKey = process.env[service.toUpperCase()];
- // A HACK FOR A DEMO VIDEO - syip2
- if (service === "handwriting") {
- apiKey = "61088486d76c4b12ba578775a5f55422";
- }
if (!apiKey) {
console.log(`No API key found for ${service}: ensure youe root directory has .env file with _CLIENT_${service.toUpperCase()}.`);
return undefined;
@@ -64,9 +59,7 @@ export namespace CognitiveServices {
};
export namespace Image {
-
export const Manager: APIManager<string> = {
-
converter: (imageUrl: string) => JSON.stringify({ url: imageUrl }),
requester: async (apiKey: string, body: string, service: Service) => {
@@ -77,18 +70,17 @@ export namespace CognitiveServices {
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'
+ 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',
+ visualFeatures: 'Categories,Description,Color,Objects,Tags,Adult',
+ details: 'Celebrities,Landmarks',
+ language: 'en',
};
break;
}
@@ -99,69 +91,63 @@ export namespace CognitiveServices {
body: body,
headers: {
'Content-Type': 'application/json',
- 'Ocp-Apim-Subscription-Key': apiKey
- }
+ 'Ocp-Apim-Subscription-Key': apiKey,
+ },
};
return request.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 batch = UndoManager.StartBatch('Image Analysis');
const storageKey = keys[0];
- if (!url || await Cast(target[storageKey], Doc)) {
+ 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.";
+ 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.";
+ toStore = results.length > 0 ? converter(results) : 'Empty list returned.';
}
}
target[storageKey] = toStore;
batch.end();
};
-
}
- export type Face = { faceAttributes: any, faceId: string, faceRectangle: Rectangle };
-
+ 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"
+ points: points.map(({ X: x, Y: y }) => `${x},${y}`).join(','),
+ language: 'en-US',
}));
return JSON.stringify({
version: 1,
- language: "en-US",
- unit: "mm",
- strokes
+ 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";
+ const serverAddress = 'https://api.cognitive.microsoft.com';
+ const endpoint = serverAddress + '/inkrecognizer/v1.0-preview/recognize';
return new Promise<string>((resolve, reject) => {
xhttp.onreadystatechange = function () {
@@ -177,7 +163,7 @@ export namespace CognitiveServices {
}
};
- xhttp.open("PUT", endpoint, true);
+ xhttp.open('PUT', endpoint, true);
xhttp.setRequestHeader('Ocp-Apim-Subscription-Key', apiKey);
xhttp.setRequestHeader('Content-Type', 'application/json');
xhttp.send(body);
@@ -186,18 +172,17 @@ export namespace CognitiveServices {
};
export namespace Appliers {
-
export const ConcatenateHandwriting: AnalysisApplier<InkData[]> = async (target: Doc, keys: string[], inkData: InkData[]) => {
- const batch = UndoManager.StartBatch("Ink Analysis");
+ 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" });
+ 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(", ");
+ const individualWords = recognizedText.filter((text: string) => text && text.split(' ').length === 1);
+ target[keys[1]] = individualWords.length ? individualWords.join(' ') : recognizedObjects.join(', ');
}
batch.end();
@@ -224,7 +209,6 @@ export namespace CognitiveServices {
unit: string;
strokes: AzureStrokeData[];
}
-
}
export namespace BingSearch {
@@ -234,7 +218,7 @@ export namespace CognitiveServices {
},
requester: async (apiKey: string, query: string) => {
const xhttp = new XMLHttpRequest();
- const serverAddress = "https://api.cognitive.microsoft.com";
+ 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 () {
@@ -251,29 +235,26 @@ export namespace CognitiveServices {
};
if (apiKey) {
- xhttp.open("GET", endpoint, true);
+ 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");
+ } 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);
+ console.log('Bing results: ', results);
const { title_vals, url_vals } = await converter(results);
return { title_vals, url_vals };
};
}
-
}
export namespace HathiTrust {
@@ -283,7 +264,7 @@ export namespace CognitiveServices {
},
requester: async (apiKey: string, query: string) => {
const xhttp = new XMLHttpRequest();
- const serverAddress = "https://babel.hathitrust.org/cgi/htd/​";
+ 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 () {
@@ -300,54 +281,52 @@ export namespace CognitiveServices {
};
if (apiKey) {
- xhttp.open("GET", endpoint, true);
+ 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");
+ } 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);
+ 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) => {
return JSON.stringify({
- documents: [{
- id: 1,
- language: "en",
- text: data
- }]
+ 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 serverAddress = 'https://eastus.api.cognitive.microsoft.com';
+ const endpoint = serverAddress + '/text/analytics/v2.1/keyPhrases';
const sampleBody = {
- "documents": [
+ documents: [
{
- "language": "en",
- "id": 1,
- "text": "Hello world. This is some input text that I love."
- }
- ]
+ language: 'en',
+ id: 1,
+ text: 'Hello world. This is some input text that I love.',
+ },
+ ],
};
const actualBody = body;
const options = {
@@ -355,25 +334,23 @@ export namespace CognitiveServices {
body: actualBody,
headers: {
'Content-Type': 'application/json',
- 'Ocp-Apim-Subscription-Key': apiKey
- }
-
+ 'Ocp-Apim-Subscription-Key': apiKey,
+ },
};
return request.post(options);
- }
+ },
};
export namespace Appliers {
-
export async function vectorize(keyterms: any, dataDoc: Doc, mainDoc: boolean = false) {
- console.log("vectorizing...");
+ console.log('vectorizing...');
//keyterms = ["father", "king"];
- const args = { method: 'POST', uri: Utils.prepend("/recommender"), body: { keyphrases: keyterms }, json: true };
- await requestPromise.post(args).then(async (wordvecs) => {
+ const args = { method: 'POST', uri: Utils.prepend('/recommender'), body: { keyphrases: keyterms }, json: true };
+ await requestPromise.post(args).then(async wordvecs => {
if (wordvecs) {
const indices = Object.keys(wordvecs);
- console.log("successful vectorization!");
+ console.log('successful vectorization!');
const vectorValues = new List<number>();
indices.forEach((ind: any) => {
vectorValues.push(wordvecs[ind]);
@@ -381,15 +358,14 @@ export namespace CognitiveServices {
//ClientRecommender.Instance.processVector(vectorValues, dataDoc, mainDoc);
} // adds document to internal doc set
else {
- console.log("unsuccessful :( word(s) not in vocabulary");
+ 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);
+ console.log('Cognitive Services keyphrases: ', results);
const { keyterms, external_recommendations, kp_string } = await converter(results, data);
target[keys[0]] = keyterms;
if (isInternal) {
@@ -400,10 +376,7 @@ export namespace CognitiveServices {
}
};
- // export async function countFrequencies()
+ // export async function countFrequencies()
}
-
}
-
-
-} \ No newline at end of file
+}