diff options
Diffstat (limited to 'src/client/cognitive_services/CognitiveServices.ts')
| -rw-r--r-- | src/client/cognitive_services/CognitiveServices.ts | 46 |
1 files changed, 24 insertions, 22 deletions
diff --git a/src/client/cognitive_services/CognitiveServices.ts b/src/client/cognitive_services/CognitiveServices.ts index 4ad03aab4..9808b6a01 100644 --- a/src/client/cognitive_services/CognitiveServices.ts +++ b/src/client/cognitive_services/CognitiveServices.ts @@ -1,9 +1,14 @@ +/* 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>; @@ -40,7 +45,7 @@ export enum Confidence { */ export namespace CognitiveServices { const ExecuteQuery = async <D>(service: Service, manager: APIManager<D>, data: D): Promise<any> => { - let apiKey = process.env[service.toUpperCase()]; + 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; @@ -51,7 +56,6 @@ export namespace CognitiveServices { results = await manager.requester(apiKey, manager.converter(data), service).then(json => JSON.parse(json)); } catch (e) { throw e; - results = undefined; } return results; }; @@ -70,7 +74,7 @@ export namespace CognitiveServices { parameters = { returnFaceId: 'true', returnFaceLandmarks: 'false', - returnFaceAttributes: 'age,gender,headPose,smile,facialHair,glasses,' + 'emotion,hair,makeup,occlusion,accessories,blur,exposure,noise', + returnFaceAttributes: 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise', }; break; case Service.ComputerVision: @@ -81,6 +85,7 @@ export namespace CognitiveServices { language: 'en', }; break; + default: } const options = { @@ -109,12 +114,10 @@ export namespace CognitiveServices { 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 { - 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; @@ -148,7 +151,7 @@ export namespace CognitiveServices { const endpoint = serverAddress + '/inkrecognizer/v1.0-preview/recognize'; return new Promise<string>((resolve, reject) => { - xhttp.onreadystatechange = function () { + xhttp.onreadystatechange = function (this: XMLHttpRequest) { if (this.readyState === 4) { const result = xhttp.responseText; switch (this.status) { @@ -159,6 +162,7 @@ export namespace CognitiveServices { return reject(result); } } + return undefined; }; xhttp.open('PUT', endpoint, true); @@ -211,15 +215,13 @@ export namespace CognitiveServices { export namespace BingSearch { export const Manager: APIManager<string> = { - converter: (data: string) => { - return data; - }, + 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 () { + xhttp.onreadystatechange = function (this: XMLHttpRequest) { if (this.readyState === 4) { const result = xhttp.responseText; switch (this.status) { @@ -230,6 +232,7 @@ export namespace CognitiveServices { return reject(result); } } + return undefined; }; if (apiKey) { @@ -257,15 +260,13 @@ export namespace CognitiveServices { export namespace HathiTrust { export const Manager: APIManager<string> = { - converter: (data: string) => { - return data; - }, + 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 () { + xhttp.onreadystatechange = function (this: XMLHttpRequest) { if (this.readyState === 4) { const result = xhttp.responseText; switch (this.status) { @@ -276,6 +277,7 @@ export namespace CognitiveServices { return reject(result); } } + return undefined; }; if (apiKey) { @@ -303,8 +305,8 @@ export namespace CognitiveServices { export namespace Text { export const Manager: APIManager<string> = { - converter: (data: string) => { - return JSON.stringify({ + converter: (data: string) => + JSON.stringify({ documents: [ { id: 1, @@ -312,8 +314,7 @@ export namespace CognitiveServices { 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'; @@ -367,11 +368,12 @@ export namespace CognitiveServices { const { keyterms, external_recommendations, kp_string } = await converter(results, data); target[keys[0]] = keyterms; if (isInternal) { - //await vectorize([data], dataDoc, isMainDoc); + // await vectorize([data], dataDoc, isMainDoc); await vectorize(kp_string, dataDoc, isMainDoc); } else { return { recs: external_recommendations, keyterms: keyterms }; } + return undefined; }; // export async function countFrequencies() |
