-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtestApi3.js
106 lines (81 loc) · 3.01 KB
/
testApi3.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
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
require('dotenv').config();
// Imports the Google Cloud client library
const language = require(`@google-cloud/language`);
// imports IBM watson auth package
const { IamTokenManager } = require('ibm-watson/auth');
// Imports IBM watson tone analyzer
const ToneAnalyzerV3 = require('ibm-watson/tone-analyzer/v3');
// for logging
const beautify = require("json-beautify");
// check for IBM Watson API key, exit if not found
if (!process.env.TONE_ANALYZER_APIKEY) {
console.log('This example requires the TONE_ANALYZER_APIKEY environment variable');
process.exit(1);
}
// Maybe needed for IBM watson Auth ????? TODO
const toneAuthenticator = new IamTokenManager({
apikey: process.env.TONE_ANALYZER_APIKEY,
});
// Maybe needed for IBM watson Auth ????? TODO
const test = function (req, res) {
return toneAuthenticator
.requestToken()
.then(({ result }) => {
// console.log({ accessToken: result.access_token, url: process.env.TONE_ANALYZER_URL });
})
.catch(console.error);
};
test();
// The text to analyze
const text = "Hello, my name is Inigo Montoya. You killed my father. Prepare to die.";
googleCloud(text);
async function googleCloud(text) {
// SET UP FOR GOOGLE LANGAUGE
// Instantiates a Google langauge client
const client = new language.LanguageServiceClient();
// create document in google language format
const document = {
content: text,
type: `PLAIN_TEXT`,
};
// SET UP FOR IBM WATSON
// Instantiates an IBM Watson tone analyzer
const toneAnalyzer = new ToneAnalyzerV3({
// See: https://github.com/watson-developer-cloud/node-sdk#authentication
version: '2017-09-21',
});
// hit all Google APIs at same time, don't proceed until all have responded
const [analyzeSentiment, analyzeEntities, analyzeSyntax, analyzeEntitySentiment] = await Promise.all([
client.analyzeSentiment({document: document}),
client.analyzeEntities({document: document}),
client.analyzeSyntax({document: document}),
client.analyzeEntitySentiment({document: document}),
])
// Array of 'utterances' to send to IBM watson
let utterances = []
// pull each sentence out of the analyzeSentiment response from Google
analyzeSentiment[0].sentences.forEach(sentence => {
let textContent = sentence.text.content;
// format utterances for IBM watson
utterances.push({ text: textContent, user: 'user' });
});
// Contact IBM watson
toneAnalyzer.toneChat({utterances: utterances})
.then(response => {
// load up an object with data from the APIs
let payload = {
analyzeSentiment,
analyzeEntities,
analyzeSyntax,
analyzeEntitySentiment,
analyzeTone: response.result
}
// TODO: Format anaylsis from APIs
// TODO: Write anaylsis/question to database
// TODO send analysis/question to client
// make it pretty to explore in console
print = beautify(payload, null, 2, 10);
console.log(print)
})
.catch(error => console.error(error));
}