-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.js
38 lines (30 loc) · 1.15 KB
/
function.js
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
const CognitiveServicesCredentials = require("@azure/ms-rest-js");
const TextAnalyticsAPIClient = require("@azure/cognitiveservices-textanalytics");
async function analyzeContent(text) {
const subscription_key = '43ef3ee75b274baca613fce15bafe9f9';
const endpoint = 'https://wetryconf.cognitiveservices.azure.com/';
const creds = new CognitiveServicesCredentials.ApiKeyCredentials({ inHeader: { 'Ocp-Apim-Subscription-Key': subscription_key } });
const textAnalyticsClient = new TextAnalyticsAPIClient.TextAnalyticsClient(creds, endpoint);
return await sentimentAnalysis(textAnalyticsClient, text);
};
async function sentimentAnalysis(client, text) {
const sentimentInput = {
documents: [
{ language: "fr", id: "1", text: text }
]
};
const sentimentResult = await client.sentiment({
multiLanguageBatchInput: sentimentInput
});
return sentimentResult;
};
module.exports = {
analyze: function(event, context) {
if (!event.data.text) {
const { response } = event.extensions;
response.writeHead(400);
return;
}
return analyzeContent(event.data.text);
}
};